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

Support C99 designed initializers. #236

Merged
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
15 changes: 11 additions & 4 deletions cxx-squid/src/main/java/org/sonar/cxx/parser/CxxGrammarImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1072,14 +1072,21 @@ private static void declarators(LexerfulGrammarBuilder b) {
);

b.rule(initializerClause).is(
b.firstOf(
assignmentExpression,
bracedInitList
b.sequence(
// C-COMPATIBILITY: C99 designated initializers
b.optional(b.firstOf(b.sequence(".", IDENTIFIER, "="),
// EXTENSION: gcc's designated initializers range
b.sequence("[", constantExpression, "...", constantExpression, "]", "="),
b.sequence("[", constantExpression, "]", "="))),
b.firstOf(
assignmentExpression,
bracedInitList
)
)
);

b.rule(initializerList).is(initializerClause, b.optional("..."), b.zeroOrMore(",", initializerClause, b.optional("...")));

b.rule(bracedInitList).is("{", b.optional(initializerList), b.optional(","), "}");
}

Expand Down
31 changes: 31 additions & 0 deletions cxx-squid/src/test/java/org/sonar/cxx/parser/DeclaratorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,37 @@ public void initializerClause_reallife() {

assertThat(p).matches("(istream_iterator<string>(cin))");
assertThat(p).matches("istream_iterator<string>()");

// C-COMPATIBILITY: C99 designated initializers
assertThat(p).matches(".name = string(\"Something\")");
assertThat(p).matches("[5] = {}");
assertThat(p).matches(".values = { [4] = 5, [5 ... 7] = 1, [2] = 0 }");
}

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

g.rule(CxxGrammarImpl.assignmentExpression).mock();
g.rule(CxxGrammarImpl.initializerList).mock();
g.rule(CxxGrammarImpl.constantExpression).mock();

assertThat(p).matches("{}");
assertThat(p).matches("{ initializerList }");
assertThat(p).matches("assignmentExpression");

// C-COMPATIBILITY: C99 designated initializers
assertThat(p).matches(".fieldName = {}");
assertThat(p).matches(".fieldName = { initializerList }");
assertThat(p).matches(".fieldName = assignmentExpression");
assertThat(p).matches("[constantExpression] = {}");
assertThat(p).matches("[constantExpression] = { initializerList }");
assertThat(p).matches("[constantExpression] = assignmentExpression");

// C-COMPATIBILITY: EXTENSION: gcc's designated initializers range
assertThat(p).matches("[constantExpression ... constantExpression] = {}");
assertThat(p).matches("[constantExpression ... constantExpression] = { initializerList }");
assertThat(p).matches("[constantExpression ... constantExpression] = assignmentExpression");
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions cxx-squid/src/test/resources/parser/own/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ int main(int argc, char** argv)

return foo()->i;
}

//C-COMPATIBILITY: C99 designated initializers
struct Point { int x, y; };
Point p = { .y = 45, .x = 72 };
int a[6] = { [4] = 29, [2] = 15 };
int widths[200] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };