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
1 change: 0 additions & 1 deletion cxx-squid/src/main/java/org/sonar/cxx/api/CxxKeyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public enum CxxKeyword implements TokenType {
TRUE("true"),
TRY("try"),
TYPEDEF("typedef"),
TYPEID("typeid"),
TYPENAME("typename"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to remove it from the keyword list? Turns also syntax highlighting off.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some unit test fail as soon as the CxxKeyword.TYPEID exist.

Running org.sonar.cxx.parser.ExpressionTest
Tests run: 42, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.061 sec <<< FAILURE! - in org.sonar.cxx.parser.ExpressionTest
postfixExpression_reallife(org.sonar.cxx.parser.ExpressionTest)  Time elapsed: 0.001 sec  <<< FAILURE!
org.sonar.sslr.tests.ParsingResultComparisonFailure: Rule 'postfixExpression' should match:
G::typeid
Parse error at line 1 column 3:

  -->  G::typeidEOF

        at org.sonar.sslr.tests.ParserAssert.matches(ParserAssert.java:72)
        at org.sonar.cxx.parser.ExpressionTest.postfixExpression_reallife(ExpressionTest.java:190)

and

Tests run: 3, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.017 sec <<< FAILURE! - in org.sonar.cxx.parser.CxxParserTest
testParsingOnDiverseSourceFiles(org.sonar.cxx.parser.CxxParserTest)  Time elapsed: 0.016 sec  <<< ERROR!
com.sonar.sslr.api.RecognitionException: Parse error at line 9 column 9:

    3: using namespace System;
    4:
    5: ref struct G {
    6:   int i;
    7: };
    8:
  -->  int main() {
   10:   G ^ pG = gcnew G;
   11:   Type ^ pType = pG->GetType();
   12:   Type ^ pType2 = G::typeid;
   13:
   14:   if (pType

        at org.sonar.sslr.internal.vm.Machine.parse(Machine.java:74)
        at com.sonar.sslr.impl.Parser.parse(Parser.java:87)
        at com.sonar.sslr.impl.Parser.parse(Parser.java:72)
        at org.sonar.cxx.parser.CxxParserTest.testParsingOnDiverseSourceFiles(CxxParserTest.java:54)

UNION("union"),
UNSIGNED("unsigned"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,19 @@ private static void expressions(LexerfulGrammarBuilder b) {
b.firstOf(
b.sequence(simpleTypeSpecifier, "(", b.optional(expressionList), ")"),
b.sequence(simpleTypeSpecifier, bracedInitList),
b.sequence(simpleTypeSpecifier, "::", "typeid"),
b.sequence(typenameSpecifier, "(", b.optional(expressionList), ")"),
b.sequence(typenameSpecifier, bracedInitList),
b.sequence(typenameSpecifier, "::", "typeid"),

primaryExpression,

b.sequence(CxxKeyword.DYNAMIC_CAST, typeIdEnclosed, "(", expression, ")"),
b.sequence(CxxKeyword.STATIC_CAST, typeIdEnclosed, "(", expression, ")"),
b.sequence(CxxKeyword.REINTERPRET_CAST, typeIdEnclosed, "(", expression, ")"),
b.sequence(CxxKeyword.CONST_CAST, typeIdEnclosed, "(", expression, ")"),
b.sequence(CxxKeyword.TYPEID, "(", expression, ")"),
b.sequence(CxxKeyword.TYPEID, "(", typeId, ")")
b.sequence("typeid", "(", expression, ")"),
b.sequence("typeid", "(", typeId, ")")
),

// postfixExpression [ expression ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CxxKeywordTest {

@Test
public void test() {
assertThat(CxxKeyword.values()).hasSize(87);
assertThat(CxxKeyword.values()).hasSize(86);
assertThat(CxxKeyword.keywordValues()).hasSize(CxxKeyword.values().length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public void postfixExpression() {
assertThat(p).matches("const_cast < typeId > ( expression )");
assertThat(p).matches("typeid ( expression )");
assertThat(p).matches("typeid ( typeId )");

assertThat(p).matches("simpleTypeSpecifier :: typeid");
assertThat(p).matches("typenameSpecifier :: typeid");

}

@Test
Expand All @@ -182,6 +186,9 @@ public void postfixExpression_reallife() {
assertThat(p).matches("dynamic_cast<Type*>(myop)->op()");
assertThat(p).matches("::foo()");
assertThat(p).matches("obj.foo<int>()");

assertThat(p).matches("G::typeid");
assertThat(p).matches("int::typeid");
}

@Test
Expand Down
20 changes: 20 additions & 0 deletions cxx-squid/src/test/resources/parser/cli/typeid/keyword__typeid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// keyword__typeid.cpp
// compile with: /clr
using namespace System;

ref struct G {
int i;
};

int main() {
G ^ pG = gcnew G;
Type ^ pType = pG->GetType();
Type ^ pType2 = G::typeid;

if (pType == pType2)
Console::WriteLine("typeid and GetType returned the same System::Type");
Console::WriteLine(G::typeid);

typedef float* FloatPtr;
Console::WriteLine(FloatPtr::typeid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// keyword__typeid_2.cpp
// compile with: /clr
using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;

typedef int ^ handle_to_int;
typedef int * pointer_to_int;

public ref class MyClass {};

class MyClass2 {};

[attribute(AttributeTargets::All)]
ref class AtClass {
public:
AtClass(Type ^) {
Console::WriteLine("in AtClass Type ^ constructor");
}
};

[attribute(AttributeTargets::All)]
ref class AtClass2 {
public:
AtClass2() {
Console::WriteLine("in AtClass2 constructor");
}
};

// Apply the AtClass and AtClass2 attributes to class B
[AtClass(MyClass::typeid), AtClass2]
[AttributeUsage(AttributeTargets::All)]
ref class B : Attribute {};

int main() {
Type ^ MyType = B::typeid;

Console::WriteLine(MyType->IsClass);

array<Object^>^ MyArray = MyType->GetCustomAttributes(true);
for (int i = 0; i < MyArray->Length; i++)
Console::WriteLine(MyArray[i]);

if (int::typeid != pointer_to_int::typeid)
Console::WriteLine("int::typeid != pointer_to_int::typeid, as expected");

if (int::typeid == handle_to_int::typeid)
Console::WriteLine("int::typeid == handle_to_int::typeid, as expected");
}