Commit fba923e8 authored by Olivier de Gaalon's avatar Olivier de Gaalon
Browse files

Allow template parameters to have default arguments without having

identifiers.
Add cpp file test to test default argument behavior (in template and
function contexts).
Add some new cpp json tests to support (templateContext,
templateParameterDefault)
BUG: 309329
parent 53c31c3b
......@@ -1892,26 +1892,25 @@ bool Parser::parseTypeParameter(TypeParameterAST *&node)
}
// parse optional name
if(parseName(ast->name, AcceptTemplate))
parseName(ast->name, AcceptTemplate);
if (session->token_stream->lookAhead() == '=')
{
if (session->token_stream->lookAhead() == '=')
{
advance();
advance();
if(!parseTypeId(ast->type_id))
{
//syntaxError();
rewind(start);
return false;
}
}
else if (session->token_stream->lookAhead() != ','
&& session->token_stream->lookAhead() != '>') ///TODO: right-shift also OK? see spec 14.2/3
if(!parseTypeId(ast->type_id))
{
//syntaxError();
rewind(start);
return false;
}
}
else if (session->token_stream->lookAhead() != ','
&& session->token_stream->lookAhead() != '>') ///TODO: right-shift also OK? see spec 14.2/3
{
rewind(start);
return false;
}
}
break;
......
......@@ -23,6 +23,7 @@
#include <typeutils.h>
#include <cppduchain.h>
#include <templatedeclaration.h>
#include <templateparameterdeclaration.h>
#include <tests/json/jsontesthelpers.h>
......@@ -37,6 +38,8 @@
* instantiations : InstantiationObject
* realType : TypeTestObject
* shortenedType : TypeTestObject
* templateContext : CtxtTestObject
* templateParameterDefault : string
*/
namespace Cpp
......@@ -117,6 +120,26 @@ DeclarationTest(shortenedType)
{
return testObject(shortenTypeForViewing(decl->abstractType()), value, "Declaration's shortened type");
}
///JSON type: CtxtTestObject
///@returns whether the tests for the declaration's templateContext pass
DeclarationTest(templateContext)
{
const QString NOT_A_TEMPLATE_DECL = "Requested template context, but declaration is not a template declaration.";
TemplateDeclaration *templDecl = dynamic_cast<TemplateDeclaration*>(decl);
if (!templDecl)
return NOT_A_TEMPLATE_DECL;
return testObject(templDecl->templateContext(decl->topContext()), value, "Declaration's template context");
}
///JSON type: string
///@returns whether the template parameter declaration's default argument matches the given value
DeclarationTest(templateParameterDefault)
{
const QString NOT_A_TEMPLATE_PARAM_DECL = "Requested template parameters's default parameter, but declaration is not a template parameter declaration.";
TemplateParameterDeclaration *templDecl = dynamic_cast<TemplateParameterDeclaration*>(decl);
if (!templDecl)
return NOT_A_TEMPLATE_PARAM_DECL;
return compareValues(templDecl->defaultParameter().toString(), value, "Template parameter declaration's default parameter");
}
}
......
/* This file is part of KDevelop
Copyright 2012 Olivier de Gaalon <olivier.jg@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
class ZERO {};
// "templateContext" : { "type" : "Template", "localDeclarationCount" : 1, "findDeclarations" : { "T" : { "templateParameterDefault" : "ZERO" } } }
template<class T = ZERO>
class A{};
// "templateContext" : { "type" : "Template", "localDeclarationCount" : 2,
// "declarations" : { "0" : { "templateParameterDefault" : "" }, "1" : { "templateParameterDefault" : "A< T >" } } }
template<class T, class = A<T>>
class B {};
// "templateContext" : { "localDeclarationCount" : 2, "declarations" : {
// "0" : { "templateParameterDefault" : "A" },
// "1" : { "templateParameterDefault" : "C< B >" }
// } }
template<class B = A, typename C = C<B>> class C;
// # NOTE: Template defaults get copied from found forward declarations
// "templateContext" : { "localDeclarationCount" : 2, "declarations" : {
// "0" : { "templateParameterDefault" : "A" },
// "1" : { "templateParameterDefault" : "C< B >" }
// } }
template<class B, typename C> class C { };
// "internalFunctionContext" : { "localDeclarationCount" : 1, "findDeclarations" : { "a" : { "defaultParameter" : "1" } } }
int foo(int a = 1);
// "internalFunctionContext" : { "localDeclarationCount" : 3, "findDeclarations" : {
// "a" : { "defaultParameter" : "4" },
// "b" : { "defaultParameter" : "3" }
// } }
int foo(int sure, int a = 4, int b = 3);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment