diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppGrammarImpl.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppGrammarImpl.java index 0eaef654eb..06a608a1d7 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppGrammarImpl.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppGrammarImpl.java @@ -477,8 +477,12 @@ private static void ifLineGrammar(LexerfulGrammarBuilder b) { b.rule(hasIncludeExpression).is( b.firstOf( - b.sequence("__has_include", "(", b.zeroOrMore(WS), includeBodyBracketed, b.zeroOrMore(WS), ")"), - b.sequence("__has_include", "(", b.zeroOrMore(WS), includeBodyQuoted, b.zeroOrMore(WS), ")") + b.sequence( + "__has_include", b.zeroOrMore(WS), "(", b.zeroOrMore(WS), includeBodyBracketed, b.zeroOrMore(WS), ")" + ), + b.sequence( + "__has_include", b.zeroOrMore(WS), "(", b.zeroOrMore(WS), includeBodyQuoted, b.zeroOrMore(WS), ")" + ) //todo: b.sequence("__has_include", "(", hasIncludeBodyFreeform, )") ) ); diff --git a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/Macro.java b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/Macro.java index c2fd53079e..f9e435547b 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/Macro.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/preprocessor/Macro.java @@ -96,7 +96,10 @@ public String toString() { } public boolean checkArgumentsCount(int count) { - return isVariadic ? count >= params.size() - 1 : count == params.size(); + if (params != null) { + return isVariadic ? count >= params.size() - 1 : count == params.size(); + } + return false; } } diff --git a/cxx-squid/src/test/java/org/sonar/cxx/parser/PreprocessorDirectivesTest.java b/cxx-squid/src/test/java/org/sonar/cxx/parser/PreprocessorDirectivesTest.java index 086eabc0c2..a47585f7d7 100644 --- a/cxx-squid/src/test/java/org/sonar/cxx/parser/PreprocessorDirectivesTest.java +++ b/cxx-squid/src/test/java/org/sonar/cxx/parser/PreprocessorDirectivesTest.java @@ -467,6 +467,16 @@ void has_include() { + "#endif\n" + "r = OK;")) .isEqualTo("r = 0 ; EOF"); + + assertThat(parse( + "#define EXISTS __has_include(\"optional\")\n" + + "#if EXISTS\n" + + "# define OK 1\n" + + "#else\n" + + "# define OK 0\n" + + "#endif\n" + + "r = OK;")) + .isEqualTo("r = 0 ; EOF"); } }