Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ public enum CxxGrammarImpl implements GrammarRuleKey {
typeSpecifier,
trailingTypeSpecifier,
typeSpecifierSeq,
trailingTypeSpecifierSeq,
simpleTypeSpecifier,
typeName,
decltypeSpecifier,
Expand Down Expand Up @@ -783,8 +782,6 @@ private static void declarations(LexerfulGrammarBuilder b) {

b.rule(typeSpecifierSeq).is(b.oneOrMore(typeSpecifier), b.optional(attributeSpecifierSeq));

b.rule(trailingTypeSpecifierSeq).is(b.oneOrMore(trailingTypeSpecifier), b.optional(attributeSpecifierSeq));

b.rule(simpleTypeSpecifier).is(
b.firstOf(
"char", "char16_t", "char32_t", "wchar_t", "bool", "short", "int", "long", "signed", "unsigned", "float", "double", "void", "auto",
Expand Down Expand Up @@ -954,7 +951,7 @@ private static void declarators(LexerfulGrammarBuilder b) {
b.rule(declarator).is(
b.firstOf(
ptrDeclarator,
b.sequence(noptrDeclarator, parametersAndQualifiers, trailingReturnType)
noptrDeclarator
)
);

Expand All @@ -972,15 +969,21 @@ private static void declarators(LexerfulGrammarBuilder b) {
),
b.zeroOrMore(
b.firstOf(
parametersAndQualifiers,
parametersAndQualifiers,
b.sequence("[", b.optional(constantExpression), "]", b.optional(attributeSpecifierSeq))
)
)
);

b.rule(parametersAndQualifiers).is("(", parameterDeclarationClause, ")", b.optional(attributeSpecifierSeq), b.optional(cvQualifierSeq), b.optional(refQualifier), b.optional(exceptionSpecification));
b.rule(parametersAndQualifiers).is("(", parameterDeclarationClause, ")" ,
b.optional(attributeSpecifierSeq),
b.optional(cvQualifierSeq),
b.optional(refQualifier),
b.optional(trailingReturnType),
b.optional(exceptionSpecification)
);

b.rule(trailingReturnType).is("->", trailingTypeSpecifierSeq, b.optional(abstractDeclarator));
b.rule(trailingReturnType).is("->", simpleTypeSpecifier);

b.rule(ptrOperator).is(
b.firstOf(
Expand Down Expand Up @@ -1121,7 +1124,7 @@ private static void classes(LexerfulGrammarBuilder b) {
b.rule(className).is(
b.firstOf(
simpleTemplateId,
IDENTIFIER
b.sequence(b.optional("::"), IDENTIFIER)
)
);

Expand Down Expand Up @@ -1207,7 +1210,7 @@ private static void derivedClasses(LexerfulGrammarBuilder b) {

b.rule(classOrDecltype).is(
b.firstOf(
b.sequence(b.optional(nestedNameSpecifier), className),
classHeadName,
decltypeSpecifier)
);

Expand Down
29 changes: 15 additions & 14 deletions cxx-squid/src/test/java/org/sonar/cxx/parser/DeclarationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public void declaration_reallife() {
// assertThat(p).matches("mpl<N/M>();");

assertThat(p).matches("bool operator==<B>(A const&, A const&);");
assertThat(p).matches("sometype foo(int& var1);");
assertThat(p).matches("auto foo(int& var1) -> int;");
assertThat(p).matches("auto fp11() -> void(*)(const std::string&);");
}

@Test
Expand Down Expand Up @@ -122,6 +125,16 @@ public void simpleDeclaration_reallife() {
assertThat(p).matches("friend class ::SMLCGroupHierarchyImpl;");
assertThat(p).matches("void foo(int, type[]);");
assertThat(p).matches("bool operator==<B>(A const&, A const&);");

assertThat(p).matches("auto to_string(int value) -> std::string;");
assertThat(p).matches("auto size() const -> std::size_t;");
assertThat(p).matches("auto str() const;");
assertThat(p).matches("auto equal_range(ForwardIterator first, ForwardIterator last, const Type& value) -> std::pair<ForwardIterator, ForwardIterator>;");

// ToDo : make this work
// assertThat(p).matches("auto str() const -> const char*;");
// assertThat(p).matches("auto std::map::at(const key_type& key) -> mapped_type&;");

}

@Test
Expand All @@ -140,7 +153,7 @@ public void declSpecifier_reallife() {
assertThat(p).matches("register"); // a storage class
assertThat(p).matches("inline"); // a function specifier
assertThat(p).matches("friend"); // a function specifier
assertThat(p).matches("void"); // a builtin type
assertThat(p).matches("void"); // a built-in type

// declSpecifier
assertThat(p).matches("friend");
Expand All @@ -153,6 +166,7 @@ public void declSpecifier_reallife() {
// class specifier
assertThat(p).matches("class foo final : bar { }");
assertThat(p).matches("class foo final : bar { int foo(); }");
assertThat(p).matches("class foo final : public ::bar { int foo(); }");

// type names
assertThat(p).matches("class_foo"); // className->identifier
Expand Down Expand Up @@ -194,19 +208,6 @@ public void typeSpecifierSeq_reallife() {
assertThat(p).matches("templatetype<T> int");
}

@Test
public void trailingTypeSpecifierSeq() {
p.setRootRule(g.rule(CxxGrammarImpl.trailingTypeSpecifierSeq));

g.rule(CxxGrammarImpl.trailingTypeSpecifier).mock();
g.rule(CxxGrammarImpl.attributeSpecifierSeq).mock();

assertThat(p).matches("trailingTypeSpecifier");
assertThat(p).matches("trailingTypeSpecifier attributeSpecifierSeq");
assertThat(p).matches("trailingTypeSpecifier trailingTypeSpecifier");
assertThat(p).matches("trailingTypeSpecifier trailingTypeSpecifier attributeSpecifierSeq");
}

@Test
public void simpleTypeSpecifier() {
p.setRootRule(g.rule(CxxGrammarImpl.simpleTypeSpecifier));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void declarator() {
g.rule(CxxGrammarImpl.trailingReturnType).mock();

assertThat(p).matches("ptrDeclarator");
assertThat(p).matches("noptrDeclarator parametersAndQualifiers trailingReturnType");
assertThat(p).matches("noptrDeclarator");
}

@Test
Expand All @@ -74,6 +74,8 @@ public void declarator_reallife() {
assertThat(p).matches("tword[20]");
assertThat(p).matches("*what() throw()");
assertThat(p).matches("foo(string, bool)");
assertThat(p).matches("foo(const string p1, bool p2)");
assertThat(p).matches("foo(const string &p1, bool p2)");
}

@Test
Expand Down Expand Up @@ -406,6 +408,12 @@ public void functionDefinition_reallife() {
assertThat(p).matches("virtual const char* what() const throw() { return \"read empty stack\"; }");
assertThat(p).matches("void foo() override {}");
assertThat(p).matches("void foo(::P& c) {}");
assertThat(p).matches("auto equal_range(ForwardIterator first, ForwardIterator last, const Type& value) -> std::pair<ForwardIterator, ForwardIterator> { return pair; }");
assertThat(p).matches("auto to_string(int value) -> std::string { return \"\"; }");
assertThat(p).matches("auto size() const -> std::size_t { return 0; }");
// ToDo : make this work
// assertThat(p).matches("auto str() const -> const char* { return nullptr; }");
// assertThat(p).matches("auto std::map::at(const key_type& key) -> mapped_type& { return value; }");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void lambdaExpression_reallife() {
assertThat(p).matches("[this] () { cout << _x; }");
// function pointers c++11, TODO: make this work
// assertThat(p).matches("[] () -> { return 2; }");
// assertThat(p).matches("[] (int x, int y) -> int { return x + y; }");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public void statement_reallife() {
assertThat(p).matches("void foo(string, bool);");
assertThat(p).matches("foo(int param, int=2);");

// 'bracket operator isnt welcome here' problem
// 'bracket operator isn't welcome here' problem
assertThat(p).matches("foo(param1, instance()[1]);");

// 'decraring friend a class in the global namespace' problem
// 'declaring friend a class in the global namespace' problem
assertThat(p).matches("friend class ::SMLCGroupHierarchyImpl;");

// "'bitwise not' applied to a mask inside a namespace" problem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public void templateDeclaration_reallife() {
assertThat(p).matches("template <class T> ostream& operator<< (const auto_ptr<T>& p);");
assertThat(p).matches("template <class T> ostream& operator<< (ostream& strm, const auto_ptr<T>& p);");
assertThat(p).matches("template<bool (A::*bar)(void)> void foo();");
assertThat(p).matches("template<class T> auto mul(T a, T b) -> decltype(a*b) {return a*b;}");
}

@Test
Expand Down