|
32 | 32 |
|
33 | 33 | namespace clang::mrdocs {
|
34 | 34 |
|
| 35 | +namespace { |
| 36 | + |
| 37 | +std::optional<std::string> |
| 38 | +getCompilerVerboseOutput(llvm::StringRef compilerPath) |
| 39 | +{ |
| 40 | + if ( ! llvm::sys::fs::exists(compilerPath)) |
| 41 | + { |
| 42 | + return std::nullopt; |
| 43 | + } |
| 44 | + |
| 45 | + llvm::SmallString<128> outputPath; |
| 46 | + if (auto ec = llvm::sys::fs::createTemporaryFile("compiler-info", "txt", outputPath)) |
| 47 | + { |
| 48 | + return std::nullopt; |
| 49 | + } |
| 50 | + |
| 51 | + std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), outputPath.str()}; |
| 52 | + std::vector<llvm::StringRef> const args = {compilerPath, "-v", "-E", "-x", "c++", "-"}; |
| 53 | + llvm::ArrayRef<llvm::StringRef> emptyEnv; |
| 54 | + int const result = ExecuteAndWaitWithLogging(compilerPath, args, emptyEnv, redirects); |
| 55 | + if (result != 0) |
| 56 | + { |
| 57 | + llvm::sys::fs::remove(outputPath); |
| 58 | + return std::nullopt; |
| 59 | + } |
| 60 | + |
| 61 | + auto bufferOrError = llvm::MemoryBuffer::getFile(outputPath); |
| 62 | + llvm::sys::fs::remove(outputPath); |
| 63 | + if (!bufferOrError) |
| 64 | + { |
| 65 | + return std::nullopt; |
| 66 | + } |
| 67 | + |
| 68 | + return bufferOrError.get()->getBuffer().str(); |
| 69 | +} |
| 70 | + |
| 71 | +std::vector<std::string> |
| 72 | +parseIncludePaths(std::string const& compilerOutput) |
| 73 | +{ |
| 74 | + std::vector<std::string> includePaths; |
| 75 | + std::istringstream stream(compilerOutput); |
| 76 | + std::string line; |
| 77 | + bool capture = false; |
| 78 | + |
| 79 | + while (std::getline(stream, line)) |
| 80 | + { |
| 81 | + if (line.find("#include <...> search starts here:") != std::string::npos) |
| 82 | + { |
| 83 | + capture = true; |
| 84 | + continue; |
| 85 | + } |
| 86 | + if (line.find("End of search list.") != std::string::npos) |
| 87 | + { |
| 88 | + break; |
| 89 | + } |
| 90 | + if (capture) |
| 91 | + { |
| 92 | + line.erase(0, line.find_first_not_of(" ")); |
| 93 | + includePaths.push_back(line); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return includePaths; |
| 98 | +} |
| 99 | + |
| 100 | +std::unordered_map<std::string, std::vector<std::string>> |
| 101 | +getCompilersDefaultIncludeDir(clang::tooling::CompilationDatabase const& compDb) |
| 102 | +{ |
| 103 | + std::unordered_map<std::string, std::vector<std::string>> res; |
| 104 | + auto const allCommands = compDb.getAllCompileCommands(); |
| 105 | + |
| 106 | + for (auto const& cmd : allCommands) |
| 107 | + { |
| 108 | + if (!cmd.CommandLine.empty()) |
| 109 | + { |
| 110 | + auto const& compilerPath = cmd.CommandLine[0]; |
| 111 | + if (res.contains(compilerPath)) |
| 112 | + { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + std::vector<std::string> includePaths; |
| 117 | + auto const compilerOutput = getCompilerVerboseOutput(compilerPath); |
| 118 | + if (!compilerOutput) |
| 119 | + { |
| 120 | + res.emplace(compilerPath, includePaths); |
| 121 | + continue; |
| 122 | + } |
| 123 | + includePaths = parseIncludePaths(*compilerOutput); |
| 124 | + res.emplace(compilerPath, std::move(includePaths)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return res; |
| 129 | +} |
| 130 | +} |
| 131 | + |
35 | 132 | TestRunner::
|
36 | 133 | TestRunner(std::string_view generator)
|
37 | 134 | : diffCmdPath_(llvm::sys::findProgramByName("diff"))
|
@@ -114,9 +211,11 @@ handleFile(
|
114 | 211 |
|
115 | 212 | // Create an adjusted MrDocsDatabase
|
116 | 213 | auto parentDir = files::getParentDir(filePath);
|
117 |
| - std::unordered_map<std::string, std::vector<std::string>> defaultIncludePaths; |
| 214 | + auto compilationDB = SingleFileDB(filePath); |
| 215 | + auto const defaultIncludePaths = getCompilersDefaultIncludeDir( |
| 216 | + compilationDB); |
118 | 217 | MrDocsCompilationDatabase compilations(
|
119 |
| - llvm::StringRef(parentDir), SingleFileDB(filePath), config, defaultIncludePaths); |
| 218 | + llvm::StringRef(parentDir), compilationDB, config, defaultIncludePaths); |
120 | 219 |
|
121 | 220 | report::debug("Building Corpus", filePath);
|
122 | 221 | auto corpus = CorpusImpl::build(config, compilations);
|
|
0 commit comments