From 139e82268b50fed91faee0e6ee3d0b6179939c99 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Thu, 26 Aug 2021 01:58:10 -0400 Subject: [PATCH] Bug 575622 - Problems in jsoncdb parser for compilers that do not support built-in detection Change-Id: I04ff6ed03c135d23d9b6ef7c83411a1fffbd9f8d Signed-off-by: Marc-Andre Laperle --- .../core/CompileCommandsJsonParser.java | 82 +++++++++++-------- .../DefaultToolCommandlineParser.java | 2 +- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/CompileCommandsJsonParser.java b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/CompileCommandsJsonParser.java index 87c6dcb0db7..95a36778eef 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/CompileCommandsJsonParser.java +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/CompileCommandsJsonParser.java @@ -37,7 +37,6 @@ import org.eclipse.cdt.jsoncdb.core.participant.IToolCommandlineParser; import org.eclipse.cdt.jsoncdb.core.participant.IToolCommandlineParser.IResult; import org.eclipse.cdt.jsoncdb.core.participant.IToolDetectionParticipant; import org.eclipse.cdt.jsoncdb.core.participant.builtins.IBuiltinsDetectionBehavior; -import org.eclipse.cdt.core.settings.model.ICLanguageSettingEntry; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; @@ -200,8 +199,8 @@ public class CompileCommandsJsonParser { } /** - * Processes an entry from a {@code compile_commands.json} file and stores a - * {@link ICLanguageSettingEntry} for the file given the specified map. + * Processes an entry from a {@code compile_commands.json} file and remembers a + * {@link IRawSourceFileInfo} for the given sourceFileInfo. * * @param sourceFileInfo parsed command entry of a compile_commands.json file * @param jsonFile the JSON file being parsed (for marker creation only) @@ -257,51 +256,64 @@ public class CompileCommandsJsonParser { createMarker(jsonFile, msg); } - /** + /** Runs detection of compiler built-ins if supported and notifies the + * {@code ISourceFileInfoConsumer} that was specified in the constructor for each source file. + * * @param monitor * @throws CoreException */ private void detectBuiltins(IProgressMonitor monitor) throws CoreException { - if (builtinDetectorsToRun.isEmpty()) - return; - monitor.setTaskName(Messages.CompileCommandsJsonParser_msg_detecting_builtins); + if (builtinDetectorsToRun.isEmpty()) { + // compiler does not support built-in detection: + // just feed the paths and defines with the file name to the indexer.. + for (Entry fileResultPair : fileResults.entrySet()) { + String sourceFileName = fileResultPair.getKey(); + IRawSourceFileInfo fileResult = fileResultPair.getValue(); + parseRequest.getSourceFileInfoConsumer().acceptSourceFileInfo(sourceFileName, + fileResult.getSystemIncludePaths(), fileResult.getDefines(), fileResult.getIncludePaths(), + fileResult.getMacroFiles(), fileResult.getIncludeFiles()); + } + } else { + // run detection of compiler built-ins... + monitor.setTaskName(Messages.CompileCommandsJsonParser_msg_detecting_builtins); - final IFile jsonFile = parseRequest.getFile(); - final IContainer buildRootFolder = jsonFile.getParent(); + final IFile jsonFile = parseRequest.getFile(); + final IContainer buildRootFolder = jsonFile.getParent(); - java.nio.file.Path buildDir = java.nio.file.Path.of(buildRootFolder.getLocationURI()); - // run each built-in detector and collect the results.. - Map builtinDetectorsResults = new HashMap<>(); - for (Entry entry : builtinDetectorsToRun.entrySet()) { - IRawSourceFileInfo result = entry.getValue().detectBuiltins(jsonFile.getProject(), buildDir, - parseRequest.getLauncher(), parseRequest.getConsole(), monitor); - // store detector key with result - builtinDetectorsResults.put(entry.getKey(), result); - } - // all built-in detectors have been run at this point, reduce memory footprint - builtinDetectorsToRun.clear(); + java.nio.file.Path buildDir = java.nio.file.Path.of(buildRootFolder.getLocationURI()); + // run each built-in detector and collect the results.. + Map builtinDetectorsResults = new HashMap<>(); + for (Entry entry : builtinDetectorsToRun.entrySet()) { + IRawSourceFileInfo result = entry.getValue().detectBuiltins(jsonFile.getProject(), buildDir, + parseRequest.getLauncher(), parseRequest.getConsole(), monitor); + // store detector key with result + builtinDetectorsResults.put(entry.getKey(), result); + } + // all built-in detectors have been run at this point, reduce memory footprint + builtinDetectorsToRun.clear(); - // most of the time we get different String objects for different source files - // that have the same sequence of characters. So reduce the number of String - // objects by pooling them.. - Map strPool = new HashMap<>(); - Function stringPooler = v -> { - String old = strPool.putIfAbsent(v, v); - return old == null ? v : old; - }; + // most of the time we get different String objects for different source files + // that have the same sequence of characters. So reduce the number of String + // objects by pooling them.. + Map strPool = new HashMap<>(); + Function stringPooler = v -> { + String old = strPool.putIfAbsent(v, v); + return old == null ? v : old; + }; - // merge built-in results with source file results - for (Entry link : fileToBuiltinDetectorLinks.entrySet()) { - String sourceFileName = link.getKey(); - IRawSourceFileInfo fileResult = fileResults.get(sourceFileName); - IRawSourceFileInfo builtinDetectorsResult = builtinDetectorsResults.get(link.getValue()); - mergeResultsForFile(stringPooler, sourceFileName, fileResult, builtinDetectorsResult); + // merge built-in results with source file results + for (Entry link : fileToBuiltinDetectorLinks.entrySet()) { + String sourceFileName = link.getKey(); + IRawSourceFileInfo fileResult = fileResults.get(sourceFileName); + IRawSourceFileInfo builtinDetectorsResult = builtinDetectorsResults.get(link.getValue()); + mergeResultsForFile(stringPooler, sourceFileName, fileResult, builtinDetectorsResult); + } } } /** * Merges preprocessor symbols and macros for a source file with compiler - * built-in preprocessor symbols and macros and passes the to the + * built-in preprocessor symbols and macros and passes them to the * {@code ISourceFileInfoConsumer} that was specified in the constructor. * * @param fileResult source file preprocessor symbols and macros diff --git a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/participant/DefaultToolCommandlineParser.java b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/participant/DefaultToolCommandlineParser.java index 8e92a0bc42a..6bab4815dc7 100644 --- a/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/participant/DefaultToolCommandlineParser.java +++ b/jsoncdb/org.eclipse.cdt.jsoncdb.core/src/org/eclipse/cdt/jsoncdb/core/participant/DefaultToolCommandlineParser.java @@ -70,7 +70,7 @@ public class DefaultToolCommandlineParser implements IToolCommandlineParser { @Override public Optional getIBuiltinsDetectionBehavior() { - return Optional.of(builtinsDetection); + return Optional.ofNullable(builtinsDetection); } @SuppressWarnings("nls")