-
Notifications
You must be signed in to change notification settings - Fork 362
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
CxxPreprocessor: cache forced includes, forced & global defines #1665
CxxPreprocessor: cache forced includes, forced & global defines #1665
Conversation
@guwirth I found out, that you marked
to add the includes and defines. SonarCFamily also uses some similar but proprietary solution: intercept the build and store the compiler arguments into the JSON database. |
@ivangalkin maybe that is a misunderstanding. Release notes:
The problem I see is that we have too many ways to handle macros and includes. Having different input formats is ok for me but internally we should handle it in an unique way. That was the reason for #1365, #1325 and #1326. There seems to be also some open questions like #1215. I had also less feedback that someone is using it. That’s the reason why the functionality is marked experimental. |
@guwirth right now the page "Supported configuration properties" looks as following:
According to you comment, the deprecation note "History: no more supported with v1.0.0+" should be moved from |
@ivangalkin done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivangalkin thanks a lot for this - think it's really an improvement. I added some comments. I*m not sure where the right place for a cache is: parser or configuration? See comments in #1365, #1325 and #1326.
cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java
Outdated
Show resolved
Hide resolved
cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java
Outdated
Show resolved
Hide resolved
cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
// set standard macros | ||
getMacros().putAll(forcedMacros); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the right order? First standard macros and then forced? Think should be possible to overwrite standard macros with forced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are absolutely right: we can use the following analogue behavior:
1. standard macro values (e.g. __LINE__)
2. configured defines (e.g. -D__LINE__=123)
3. forced include files (e.g. header contains #define __LINE__ 456)
-> 2 overrides 1
-> 3 overrides 2 and 1
I've reordered the priorities and have written the tests. See
- CxxLexerWithPreprocessingTest.defaultMacros
- CxxLexerWithPreprocessingTest.configuredDefinesOverrideDefaultMacros
- CxxLexerWithPreprocessingTest.forcedIncludesOverrideConfiguredDefines
cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java
Outdated
Show resolved
Hide resolved
//Create macros to replace C++ keywords when parsing C files | ||
getMacros().putAll(Macro.COMPATIBILITY_MACROS); | ||
fixedMacros.disable(CPLUSPLUS); | ||
getMacros().disable(Macro.CPLUSPLUS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think there are more macros different between C and C++?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the map Macro.STANDARD_MACROS_IMPL
contains only two C++ specific macros:
- __cplusplus
- __has_include (since C++17)
so theoretically we have to disable __has_include
to. BUT
- I tested GCC in the ANSI-C mode and it still provides this define (https://coliru.stacked-crooked.com/a/30a53f2c2a9a323d)
- Our implementation of
__has_include
is hard-wired in the grammar and the expansion of__has_include()
is not restricted to C++. So even if#ifdef __has_include
evaluates to0
, we still can't prevent__has_include(...)
from evaluation. - This C vs C++ was not scope of my change, so please let's consider to change it in other PR
* renaming * apply correct order macro definitions 1. standard macro values (e.g. __LINE__) 2. configured defines (e.g. -D__LINE__=123) 3. forced include files (e.g. header contains #define __LINE__ 456) -> 2 overrides 1 -> 3 overrides 2 and 1
parseForcedIncludes(); | ||
predefinedUnitMacros = new HashMap<>(unitMacros.getHighPrioMap()); | ||
} finally { | ||
unitMacros = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivangalkin is here getMacros().setHighPrio(false)
missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, in this case we don't have to disable the high-prio mode
- unitMacros = new MapChain<>(); -> make getMacro() return unitMacros
- fill the map getMacro() directly and (more important) indirectly through parseForcedIncludes()
- all precompiled macros for compile units are now in unitMacros.getHighPrioMap(); we copy them into predefinedUnitMacros
- unitMacros = null; -> destroy the unitMacros; they won't be filled from constructor anymore, no need to toggle the high-prio mode
@@ -127,34 +173,56 @@ public CxxPreprocessor(SquidAstVisitorContext<Grammar> context, | |||
|
|||
pplineParser = CppParser.create(conf); | |||
|
|||
final List<String> configuredDefines = conf.getDefines(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivangalkin ctor code is difficult to read. Maybe you can split it into smaller methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I'll do some refactoring;
* refactor CxxPreprocessor::CxxPreprocessor()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivangalkin thanks looks good now.
Also:
apply correct order macro definitions
__LINE__
)-D__LINE__=123
)#define __LINE__ 456
)-> 2 overrides 1
-> 3 overrides 2 and 1
This change is