Skip to content

Commit 8dfd12f

Browse files
committed
CxxPreprocessor: unitilize com.sonar.sslr.api.Preprocessor::init()
* the check, if CxxPreprocessor was about to start preprocessing a new file was previously done in CxxPreprocessor::process() * Preprocessor::process() is called multiple time per file * in the words case it's called for each Token * there is a special initialization method Preprocessor:init(), which is called once per file. See com.sonar.sslr.impl.Lexer::lex(Reader reader) * override this method and put the is-new-file logic to CxxPreprocessor::init() * we don't break any logic we still rely on the current context file, which is set in the proper order 1. org.sonar.squidbridge.AstScanner::scanFiles() set the current context file 2. parser.parse(file) -> lexer.lex(file) -> ... lex(reader) 3. preprocessor.init();
1 parent 2ff6f41 commit 8dfd12f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CxxPreprocessor.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,10 @@ public Collection<Include> getMissingIncludeFiles(File file) {
445445
}
446446

447447
@Override
448-
public PreprocessorAction process(List<Token> tokens) { //TODO: deprecated PreprocessorAction
449-
Token token = tokens.get(0);
450-
TokenType ttype = token.getType();
451-
452-
final String rootFilePath = getFileUnderAnalysis().getAbsolutePath();
453-
454-
// CxxPreprocessor::process() can be called a) while construction,
455-
// b) for a new "physical" file or c) for #include directive.
448+
public void init() {
449+
// CxxPreprocessor::init() can be called a) while construction,
450+
// b) for a new "physical" file or c) while processing of
451+
// #include directive.
456452
// Make sure, that the following code is executed for a new "physical" file
457453
// only.
458454
final boolean processingNewSourceFile = !ctorInProgress && (context.getFile() != currentContextFile);
@@ -465,12 +461,12 @@ public PreprocessorAction process(List<Token> tokens) { //TODO: deprecated Prepr
465461
compilationUnitSettings = conf.getCompilationUnitSettings(currentContextFile.getAbsolutePath());
466462

467463
if (compilationUnitSettings != null) {
468-
LOG.debug("compilation unit settings for: '{}'", rootFilePath);
464+
LOG.debug("compilation unit settings for: '{}'", currentContextFile);
469465
} else {
470466
compilationUnitSettings = conf.getGlobalCompilationUnitSettings();
471467

472468
if (compilationUnitSettings != null) {
473-
LOG.debug("global compilation unit settings for: '{}'", rootFilePath);
469+
LOG.debug("global compilation unit settings for: '{}'", currentContextFile);
474470
}
475471
}
476472

@@ -526,7 +522,7 @@ public PreprocessorAction process(List<Token> tokens) { //TODO: deprecated Prepr
526522
}
527523
} else {
528524
// Use global settings
529-
LOG.debug("global settings for: '{}'", rootFilePath);
525+
LOG.debug("global settings for: '{}'", currentContextFile);
530526
if (isCFile(currentContextFile.getAbsolutePath())) {
531527
//Create macros to replace C++ keywords when parsing C files
532528
getMacros().putAll(Macro.COMPATIBILITY_MACROS);
@@ -536,6 +532,13 @@ public PreprocessorAction process(List<Token> tokens) { //TODO: deprecated Prepr
536532
}
537533
}
538534
}
535+
}
536+
537+
@Override
538+
public PreprocessorAction process(List<Token> tokens) { //TODO: deprecated PreprocessorAction
539+
final Token token = tokens.get(0);
540+
final TokenType ttype = token.getType();
541+
final String rootFilePath = getFileUnderAnalysis().getAbsolutePath();
539542

540543
if (ttype.equals(PREPROCESSOR)) {
541544

0 commit comments

Comments
 (0)