Skip to content

Commit e984eea

Browse files
committed
apply review comments (2)
* refactor CxxPreprocessor::CxxPreprocessor()
1 parent 08b548e commit e984eea

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

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

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public class CxxPreprocessor extends Preprocessor {
137137
* Pre-parsed defines from the global compilation unit settings, see
138138
* {@link CxxConfiguration#getGlobalCompilationUnitSettings()}.
139139
*/
140-
private final Map<String, Macro> globalCUDefines;
140+
private final Map<String, Macro> globalUnitMacros;
141141
private final Set<File> analysedFiles = new HashSet<>();
142142
private final SourceCodeProvider codeProvider;
143143
private SourceCodeProvider unitCodeProvider;
@@ -173,18 +173,28 @@ public CxxPreprocessor(SquidAstVisitorContext<Grammar> context,
173173

174174
pplineParser = CppParser.create(conf);
175175

176+
final Map<String, Macro> configuredMacros = parseConfiguredMacros();
177+
fillFixedMacros(configuredMacros);
178+
predefinedUnitMacros = parsePredefinedUnitMacros(configuredMacros);
179+
globalUnitMacros = parseGlobalUnitMacros();
180+
181+
ctorInProgress = false;
182+
}
183+
184+
private Map<String, Macro> parseConfiguredMacros() {
176185
final List<String> configuredDefines = conf.getDefines();
177-
Map<String, Macro> configuredMacros = Collections.emptyMap();
178-
if (!configuredDefines.isEmpty()) {
179-
LOG.debug("parsing configured defines");
180-
configuredMacros = parseMacroDefinitions(configuredDefines);
186+
if (configuredDefines.isEmpty()) {
187+
return Collections.emptyMap();
181188
}
189+
LOG.debug("parsing configured defines");
190+
return parseMacroDefinitions(configuredDefines);
191+
}
182192

183-
// fill CxxPreprocessor.fixedMacros
184-
// getMacros() returns CxxPreprocessor.fixedMacros
185-
if (getMacros() != fixedMacros) {
186-
throw new IllegalStateException("expected fixedMacros as active macros map");
193+
private void fillFixedMacros(Map<String, Macro> configuredMacros) {
194+
if (!ctorInProgress || (getMacros() != fixedMacros) || !fixedMacros.getHighPrioMap().isEmpty()) {
195+
throw new IllegalStateException("Preconditions for initial fill-out of fixedMacros were violated");
187196
}
197+
188198
try {
189199
getMacros().setHighPrio(true);
190200
getMacros().putAll(Macro.STANDARD_MACROS);
@@ -193,36 +203,50 @@ public CxxPreprocessor(SquidAstVisitorContext<Grammar> context,
193203
} finally {
194204
getMacros().setHighPrio(false);
195205
}
206+
}
196207

197-
// fill CxxPreprocessor.predefinedUnitMacros if relevant
198-
final CxxCompilationUnitSettings globalCUSettings = conf.getGlobalCompilationUnitSettings();
199-
if (!conf.getCompilationUnitSourceFiles().isEmpty() || (globalCUSettings != null)) {
200-
unitMacros = new MapChain<>();
201-
if (getMacros() != unitMacros) {
202-
throw new IllegalStateException("expected unitMacros as active macros map");
203-
}
204-
try {
205-
getMacros().setHighPrio(true);
206-
getMacros().putAll(Macro.UNIT_MACROS);
207-
getMacros().putAll(configuredMacros);
208-
parseForcedIncludes();
209-
predefinedUnitMacros = new HashMap<>(unitMacros.getHighPrioMap());
210-
} finally {
211-
unitMacros = null;
212-
}
213-
} else {
214-
predefinedUnitMacros = Collections.emptyMap();
208+
/**
209+
* Create temporary unitMacros map; This map will be used as an active macros'
210+
* storage while parsing of forced includes. After parsing was over extract
211+
* resulting macros and destroy the unitMacros. fixedMacros will be set as
212+
* active macros again.
213+
*/
214+
private Map<String, Macro> parsePredefinedUnitMacros(Map<String, Macro> configuredMacros) {
215+
if (!ctorInProgress || (unitMacros != null)) {
216+
throw new IllegalStateException("Preconditions for initial fill-out of predefinedUnitMacros were violated");
215217
}
216218

217-
// fill CxxPreprocessor.globalCUDefines if relevant
218-
if (globalCUSettings != null) {
219-
LOG.debug("parsing global compilation unit defines");
220-
globalCUDefines = parseMacroDefinitions(globalCUSettings.getDefines());
221-
} else {
222-
globalCUDefines = Collections.emptyMap();
219+
if (conf.getCompilationUnitSourceFiles().isEmpty() && (conf.getGlobalCompilationUnitSettings() == null)) {
220+
// configuration doesn't contain any settings settings for compilation
221+
// units. CxxPreprocessor will use fixedMacros only
222+
return Collections.emptyMap();
223223
}
224224

225-
ctorInProgress = false;
225+
unitMacros = new MapChain<>();
226+
if (getMacros() != unitMacros) {
227+
throw new IllegalStateException("expected unitMacros as active macros map");
228+
}
229+
230+
try {
231+
getMacros().setHighPrio(true);
232+
getMacros().putAll(Macro.UNIT_MACROS);
233+
getMacros().putAll(configuredMacros);
234+
parseForcedIncludes();
235+
final HashMap<String, Macro> result = new HashMap<>(unitMacros.getHighPrioMap());
236+
return result;
237+
} finally {
238+
getMacros().setHighPrio(false); // just for the symmetry
239+
unitMacros = null; // remove unitMacros, switch getMacros() to fixedMacros
240+
}
241+
}
242+
243+
private Map<String, Macro> parseGlobalUnitMacros() {
244+
final CxxCompilationUnitSettings globalCUSettings = conf.getGlobalCompilationUnitSettings();
245+
if (globalCUSettings == null) {
246+
return Collections.emptyMap();
247+
}
248+
LOG.debug("parsing global compilation unit defines");
249+
return parseMacroDefinitions(globalCUSettings.getDefines());
226250
}
227251

228252
public static void finalReport() {
@@ -551,7 +575,7 @@ public void init() {
551575

552576
// rest of defines comes from compilation unit settings
553577
if (useGlobalCUSettings) {
554-
getMacros().putAll(globalCUDefines);
578+
getMacros().putAll(globalUnitMacros);
555579
} else {
556580
getMacros().putAll(parseMacroDefinitions(compilationUnitSettings.getDefines()));
557581
}

0 commit comments

Comments
 (0)