-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect compiler default include paths
- Loading branch information
1 parent
cd00fe6
commit 0720a15
Showing
7 changed files
with
224 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// This is a derivative work. originally part of the LLVM Project. | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
// Copyright (c) 2023 Vinnie Falco ([email protected]) | ||
// | ||
// Official repository: https://github.com/cppalliance/mrdocs | ||
// | ||
|
||
#include "CompilerInfo.hpp" | ||
|
||
#include <mrdocs/Support/Error.hpp> | ||
|
||
#include <llvm/Support/Program.h> | ||
|
||
namespace clang { | ||
namespace mrdocs { | ||
|
||
std::optional<std::string> | ||
getCompilerVerboseOutput(llvm::StringRef compilerPath) | ||
{ | ||
if ( ! llvm::sys::fs::exists(compilerPath)) { | ||
return std::nullopt; | ||
} | ||
|
||
llvm::SmallString<128> outputPath; | ||
if (auto ec = llvm::sys::fs::createTemporaryFile("compiler-info", "txt", outputPath)) | ||
{ | ||
return std::nullopt; | ||
} | ||
|
||
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), outputPath.str()}; | ||
std::vector<llvm::StringRef> const args = {compilerPath, "-v", "-E", "-x", "c++", "-"}; | ||
llvm::ArrayRef<llvm::StringRef> emptyEnv; | ||
int const result = llvm::sys::ExecuteAndWait(compilerPath, args, emptyEnv, redirects); | ||
if (result != 0) | ||
{ | ||
llvm::sys::fs::remove(outputPath); | ||
return std::nullopt; | ||
} | ||
|
||
auto bufferOrError = llvm::MemoryBuffer::getFile(outputPath); | ||
llvm::sys::fs::remove(outputPath); | ||
if ( ! bufferOrError) | ||
{ | ||
return std::nullopt; | ||
} | ||
|
||
return bufferOrError.get()->getBuffer().str(); | ||
} | ||
|
||
std::vector<std::string> | ||
parseIncludePaths(std::string const& compilerOutput) | ||
{ | ||
std::vector<std::string> includePaths; | ||
std::istringstream stream(compilerOutput); | ||
std::string line; | ||
bool capture = false; | ||
|
||
while (std::getline(stream, line)) | ||
{ | ||
if (line.find("#include <...> search starts here:") != std::string::npos) | ||
{ | ||
capture = true; | ||
continue; | ||
} | ||
if (line.find("End of search list.") != std::string::npos) | ||
{ | ||
break; | ||
} | ||
if (capture) | ||
{ | ||
line.erase(0, line.find_first_not_of(" ")); | ||
includePaths.push_back(line); | ||
} | ||
} | ||
|
||
return includePaths; | ||
} | ||
|
||
std::unordered_map<std::string, std::vector<std::string>> | ||
getCompilersDefaultIncludeDir(clang::tooling::CompilationDatabase const& compDb) | ||
{ | ||
std::unordered_map<std::string, std::vector<std::string>> res; | ||
auto const allCommands = compDb.getAllCompileCommands(); | ||
|
||
for (auto const& cmd : allCommands) { | ||
if ( ! cmd.CommandLine.empty()) { | ||
auto const& compilerPath = cmd.CommandLine[0]; | ||
if (res.contains(compilerPath)) { | ||
continue; | ||
} | ||
|
||
auto const compilerOutput = getCompilerVerboseOutput(compilerPath); | ||
if ( ! compilerOutput) { | ||
report::warn("Warning: could not get compiler info for \"{}\"", compilerPath); | ||
continue; | ||
} | ||
std::vector<std::string> includePaths = parseIncludePaths(*compilerOutput); | ||
res.emplace(compilerPath, std::move(includePaths)); | ||
} | ||
} | ||
|
||
return res; | ||
} | ||
|
||
} // mrdocs | ||
} // clang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
// Copyright (c) 2023 Vinnie Falco ([email protected]) | ||
// | ||
// Official repository: https://github.com/cppalliance/mrdocs | ||
// | ||
|
||
#ifndef MRDOCS_TOOL_COMPILER_HPP | ||
#define MRDOCS_TOOL_COMPILER_HPP | ||
|
||
#include <optional> | ||
#include <string> | ||
#include <vector> | ||
#include <unordered_map> | ||
|
||
#include <clang/Tooling/CompilationDatabase.h> | ||
#include <llvm/ADT/StringRef.h> | ||
|
||
namespace clang { | ||
namespace mrdocs { | ||
|
||
/** | ||
* @brief Get the compiler verbose output. | ||
* | ||
* @param compilerPath The compiler path. | ||
* @return std::optional<std::string> The compiler verbose output. | ||
*/ | ||
std::optional<std::string> | ||
getCompilerVerboseOutput(llvm::StringRef compilerPath); | ||
|
||
/** | ||
* @brief Parse the include paths. | ||
* | ||
* @param compilerOutput The compiler output. | ||
* @return std::vector<std::string> The include paths. | ||
*/ | ||
std::vector<std::string> | ||
parseIncludePaths(std::string const& compilerOutput); | ||
|
||
/** | ||
* @brief Get the compiler default include dir. | ||
* | ||
* @param compDb The compilation database. | ||
* @return std::unordered_map<std::string, std::vector<std::string>> The compiler default include dir. | ||
*/ | ||
std::unordered_map<std::string, std::vector<std::string>> | ||
getCompilersDefaultIncludeDir(clang::tooling::CompilationDatabase const& compDb); | ||
|
||
} // mrdocs | ||
} // clang | ||
|
||
#endif |
Oops, something went wrong.