Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for trailing return types #587

Merged
merged 1 commit into from
Jul 23, 2015
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
11 changes: 8 additions & 3 deletions cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,11 @@ public enum CxxGrammarImpl implements GrammarRuleKey {

storageClassSpecifier,
functionSpecifier,
typedefName,
typedefName,
typeSpecifier,
trailingTypeSpecifier,
typeSpecifierSeq,
trailingTypeSpecifier,
trailingTypeSpecifierSeq,
simpleTypeSpecifier,
typeName,
decltypeSpecifier,
Expand Down Expand Up @@ -820,6 +821,8 @@ private static void declarations(LexerfulGrammarBuilder b) {
)
);

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

b.rule(trailingTypeSpecifier).is(
b.firstOf(
simpleTypeSpecifier,
Expand Down Expand Up @@ -1028,7 +1031,9 @@ private static void declarators(LexerfulGrammarBuilder b) {
b.optional(exceptionSpecification)
);

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

b.rule(ptrOperator).is(
b.firstOf(
Expand Down
32 changes: 27 additions & 5 deletions cxx-squid/src/test/java/org/sonar/cxx/parser/DeclarationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,38 @@ public void simpleDeclaration_reallife() {
assertThat(p).matches("bool operator==(const lhs&, const rhs&);");
assertThat(p).matches("bool operator==<B>(A const&, A const&);");

assertThat(p).matches("int foo();");
assertThat(p).matches("const int foo();");
assertThat(p).matches("int* foo();");
assertThat(p).matches("const int* foo();");
assertThat(p).matches("int& foo();");
assertThat(p).matches("const int& foo();");
assertThat(p).matches("long long foo();");
assertThat(p).matches("const long long foo();");
assertThat(p).matches("long long* foo();");
assertThat(p).matches("const long long* foo();");
assertThat(p).matches("long long& foo();");
assertThat(p).matches("const long long& foo();");
assertThat(p).matches("MyClass foo();");
assertThat(p).matches("const MyClass foo();");
assertThat(p).matches("MyClass* foo();");
assertThat(p).matches("const MyClass* foo();");
assertThat(p).matches("MyClass* foo();");
assertThat(p).matches("const MyClass* foo();");

assertThat(p).matches("extern int foo();");

assertThat(p).matches("auto to_string(int value) -> int;");
assertThat(p).matches("auto to_string(int value) -> long long;");
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>;");

// assertThat(p).matches("int property;");
// assertThat(p).matches("int property = 0;");
// 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&;");
assertThat(p).matches("auto str() const -> const char*;");
assertThat(p).matches("auto std::map::at(const key_type& key) -> mapped_type&;");
assertThat(p).matches("auto f() -> int(*)[4];");
assertThat(p).matches("auto fpif(int) -> int(*)(int);");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void lambdaExpression_reallife() {
assertThat(p).matches("[&foo1,&foo2,&foo3] (int n, int y, int z) { }");
assertThat(p).matches("[] () throw () { }");
assertThat(p).matches("[] () -> int { return 1; }");
assertThat(p).matches("[] () -> long long { return 1; }");
assertThat(p).matches("[] (const string& addr) { return addr.find( \".org\" ) != string::npos; }");
assertThat(p).matches("[this] () { cout << _x; }");
assertThat(p).matches("[] (int x, int y) -> int { return x + y; }");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ void func2(long long int p) {}
long long func3() { return 0; }
long long int func4() { return 0; }

//@todo
//auto func5(long long p) -> long long { return 0; }
//auto func6(long long int p) -> long long int { return 0; }
auto func5(long long p) -> long long { return 0; }
auto func6(long long int p) -> long long int { return 0; }