src: applied clang-format
This commit is contained in:
@@ -5,12 +5,12 @@
|
||||
#include <clang/Driver/Options.h>
|
||||
#include <clang/Frontend/CompilerInvocation.h>
|
||||
#include <clang/Tooling/Tooling.h>
|
||||
#include <llvm/Support/VirtualFileSystem.h>
|
||||
#include <llvm/Option/ArgList.h>
|
||||
#include <llvm/Support/TargetSelect.h>
|
||||
#include <llvm/Support/VirtualFileSystem.h>
|
||||
|
||||
#include "ApplicationSettings.h"
|
||||
#include "ASTAction.h"
|
||||
#include "ApplicationSettings.h"
|
||||
#include "CanonicalFilePathCache.h"
|
||||
#include "ClangInvocationInfo.h"
|
||||
#include "CxxCompilationDatabaseSingle.h"
|
||||
@@ -18,61 +18,65 @@
|
||||
#include "FilePath.h"
|
||||
#include "FileRegister.h"
|
||||
#include "IndexerCommandCxx.h"
|
||||
#include "logging.h"
|
||||
#include "ParserClient.h"
|
||||
#include "ResourcePaths.h"
|
||||
#include "SingleFrontendActionFactory.h"
|
||||
#include "TextAccess.h"
|
||||
#include "utilityString.h"
|
||||
#include "logging.h"
|
||||
#include "utility.h"
|
||||
#include "utilityString.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
std::vector<std::string> prependSyntaxOnlyToolArgs(const std::vector<std::string>& args)
|
||||
{
|
||||
return utility::concat(std::vector<std::string>({ "clang-tool", "-fsyntax-only" }), args);
|
||||
}
|
||||
|
||||
std::vector<std::string> appendFilePath(const std::vector<std::string>& args, llvm::StringRef filePath)
|
||||
{
|
||||
return utility::concat(args, { filePath.str() });
|
||||
}
|
||||
|
||||
// custom implementation of clang::runToolOnCodeWithArgs which also sets our custon DiagnosticConsumer
|
||||
bool runToolOnCodeWithArgs(
|
||||
clang::DiagnosticConsumer* DiagConsumer,
|
||||
clang::FrontendAction *ToolAction,
|
||||
const llvm::Twine &Code,
|
||||
const std::vector<std::string> &Args,
|
||||
const llvm::Twine &FileName = "input.cc",
|
||||
const clang::tooling::FileContentMappings &VirtualMappedFiles = clang::tooling::FileContentMappings()
|
||||
)
|
||||
{
|
||||
CxxParser::initializeLLVM();
|
||||
|
||||
llvm::SmallString<16> FileNameStorage;
|
||||
llvm::StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
|
||||
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem);
|
||||
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
||||
llvm::IntrusiveRefCntPtr<clang::FileManager> Files(new clang::FileManager(clang::FileSystemOptions(), OverlayFileSystem));
|
||||
|
||||
clang::tooling::ToolInvocation Invocation(prependSyntaxOnlyToolArgs(appendFilePath(Args, FileNameRef)), ToolAction, Files.get());
|
||||
|
||||
llvm::SmallString<1024> CodeStorage;
|
||||
llvm::StringRef CodeRef = Code.toNullTerminatedStringRef(CodeStorage);
|
||||
|
||||
InMemoryFileSystem->addFile(FileNameRef, 0,
|
||||
llvm::MemoryBuffer::getMemBufferCopy(CodeRef));
|
||||
|
||||
Invocation.setDiagnosticConsumer(DiagConsumer);
|
||||
|
||||
return Invocation.run();
|
||||
}
|
||||
std::vector<std::string> prependSyntaxOnlyToolArgs(const std::vector<std::string>& args)
|
||||
{
|
||||
return utility::concat(std::vector<std::string>({"clang-tool", "-fsyntax-only"}), args);
|
||||
}
|
||||
|
||||
std::vector<std::string> CxxParser::getCommandlineArgumentsEssential(const std::vector<std::wstring>& compilerFlags)
|
||||
std::vector<std::string> appendFilePath(const std::vector<std::string>& args, llvm::StringRef filePath)
|
||||
{
|
||||
return utility::concat(args, {filePath.str()});
|
||||
}
|
||||
|
||||
// custom implementation of clang::runToolOnCodeWithArgs which also sets our custon DiagnosticConsumer
|
||||
bool runToolOnCodeWithArgs(
|
||||
clang::DiagnosticConsumer* DiagConsumer,
|
||||
clang::FrontendAction* ToolAction,
|
||||
const llvm::Twine& Code,
|
||||
const std::vector<std::string>& Args,
|
||||
const llvm::Twine& FileName = "input.cc",
|
||||
const clang::tooling::FileContentMappings& VirtualMappedFiles =
|
||||
clang::tooling::FileContentMappings())
|
||||
{
|
||||
CxxParser::initializeLLVM();
|
||||
|
||||
llvm::SmallString<16> FileNameStorage;
|
||||
llvm::StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
|
||||
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
|
||||
new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
|
||||
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
|
||||
new llvm::vfs::InMemoryFileSystem);
|
||||
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
|
||||
llvm::IntrusiveRefCntPtr<clang::FileManager> Files(
|
||||
new clang::FileManager(clang::FileSystemOptions(), OverlayFileSystem));
|
||||
|
||||
clang::tooling::ToolInvocation Invocation(
|
||||
prependSyntaxOnlyToolArgs(appendFilePath(Args, FileNameRef)), ToolAction, Files.get());
|
||||
|
||||
llvm::SmallString<1024> CodeStorage;
|
||||
llvm::StringRef CodeRef = Code.toNullTerminatedStringRef(CodeStorage);
|
||||
|
||||
InMemoryFileSystem->addFile(FileNameRef, 0, llvm::MemoryBuffer::getMemBufferCopy(CodeRef));
|
||||
|
||||
Invocation.setDiagnosticConsumer(DiagConsumer);
|
||||
|
||||
return Invocation.run();
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::vector<std::string> CxxParser::getCommandlineArgumentsEssential(
|
||||
const std::vector<std::wstring>& compilerFlags)
|
||||
{
|
||||
std::vector<std::string> args;
|
||||
|
||||
@@ -80,7 +84,8 @@ std::vector<std::string> CxxParser::getCommandlineArgumentsEssential(const std::
|
||||
// be AST elements for unused template functions as well.
|
||||
args.push_back("-fno-delayed-template-parsing");
|
||||
|
||||
// The option -fexceptions signals that clang should watch out for exception-related code during indexing.
|
||||
// The option -fexceptions signals that clang should watch out for exception-related code during
|
||||
// indexing.
|
||||
args.push_back("-fexceptions");
|
||||
|
||||
// The option -c signals that no executable is built.
|
||||
@@ -92,7 +97,7 @@ std::vector<std::string> CxxParser::getCommandlineArgumentsEssential(const std::
|
||||
// This option tells clang just to continue parsing no matter how manny errors have been thrown.
|
||||
args.push_back("-ferror-limit=0");
|
||||
|
||||
for (const std::wstring& compilerFlag : compilerFlags)
|
||||
for (const std::wstring& compilerFlag: compilerFlags)
|
||||
{
|
||||
args.push_back(utility::encodeToUtf8(compilerFlag));
|
||||
}
|
||||
@@ -116,11 +121,8 @@ void CxxParser::initializeLLVM()
|
||||
CxxParser::CxxParser(
|
||||
std::shared_ptr<ParserClient> client,
|
||||
std::shared_ptr<FileRegister> fileRegister,
|
||||
std::shared_ptr<IndexerStateInfo> indexerStateInfo
|
||||
)
|
||||
: Parser(client)
|
||||
, m_fileRegister(fileRegister)
|
||||
, m_indexerStateInfo(indexerStateInfo)
|
||||
std::shared_ptr<IndexerStateInfo> indexerStateInfo)
|
||||
: Parser(client), m_fileRegister(fileRegister), m_indexerStateInfo(indexerStateInfo)
|
||||
{
|
||||
llvm::InitializeNativeTarget();
|
||||
llvm::InitializeNativeTargetAsmParser();
|
||||
@@ -143,34 +145,39 @@ void CxxParser::buildIndex(std::shared_ptr<IndexerCommandCxx> indexerCommand)
|
||||
runTool(&compilationDatabase, indexerCommand->getSourceFilePath());
|
||||
}
|
||||
|
||||
void CxxParser::buildIndex(const std::wstring& fileName, std::shared_ptr<TextAccess> fileContent, std::vector<std::wstring> compilerFlags)
|
||||
void CxxParser::buildIndex(
|
||||
const std::wstring& fileName,
|
||||
std::shared_ptr<TextAccess> fileContent,
|
||||
std::vector<std::wstring> compilerFlags)
|
||||
{
|
||||
std::shared_ptr<CanonicalFilePathCache> canonicalFilePathCache =
|
||||
std::make_shared<CanonicalFilePathCache>(m_fileRegister);
|
||||
|
||||
std::shared_ptr<CxxDiagnosticConsumer> diagnostics = getDiagnostics(FilePath(), canonicalFilePathCache, false);
|
||||
clang::ASTFrontendAction* action = new ASTAction(m_client, canonicalFilePathCache, m_indexerStateInfo);
|
||||
std::shared_ptr<CxxDiagnosticConsumer> diagnostics = getDiagnostics(
|
||||
FilePath(), canonicalFilePathCache, false);
|
||||
clang::ASTFrontendAction* action = new ASTAction(
|
||||
m_client, canonicalFilePathCache, m_indexerStateInfo);
|
||||
|
||||
std::vector<std::string> args = getCommandlineArgumentsEssential(compilerFlags);
|
||||
|
||||
runToolOnCodeWithArgs(
|
||||
diagnostics.get(),
|
||||
action,
|
||||
fileContent->getText(),
|
||||
args,
|
||||
utility::encodeToUtf8(fileName)
|
||||
);
|
||||
diagnostics.get(), action, fileContent->getText(), args, utility::encodeToUtf8(fileName));
|
||||
}
|
||||
|
||||
void CxxParser::runTool(clang::tooling::CompilationDatabase* compilationDatabase, const FilePath& sourceFilePath)
|
||||
void CxxParser::runTool(
|
||||
clang::tooling::CompilationDatabase* compilationDatabase, const FilePath& sourceFilePath)
|
||||
{
|
||||
initializeLLVM();
|
||||
|
||||
clang::tooling::ClangTool tool(*compilationDatabase, std::vector<std::string>(1, utility::encodeToUtf8(sourceFilePath.wstr())));
|
||||
clang::tooling::ClangTool tool(
|
||||
*compilationDatabase,
|
||||
std::vector<std::string>(1, utility::encodeToUtf8(sourceFilePath.wstr())));
|
||||
|
||||
std::shared_ptr<CanonicalFilePathCache> canonicalFilePathCache = std::make_shared<CanonicalFilePathCache>(m_fileRegister);
|
||||
std::shared_ptr<CanonicalFilePathCache> canonicalFilePathCache =
|
||||
std::make_shared<CanonicalFilePathCache>(m_fileRegister);
|
||||
|
||||
std::shared_ptr<CxxDiagnosticConsumer> diagnostics = getDiagnostics(sourceFilePath, canonicalFilePathCache, true);
|
||||
std::shared_ptr<CxxDiagnosticConsumer> diagnostics = getDiagnostics(
|
||||
sourceFilePath, canonicalFilePathCache, true);
|
||||
|
||||
tool.setDiagnosticConsumer(diagnostics.get());
|
||||
|
||||
@@ -178,7 +185,12 @@ void CxxParser::runTool(clang::tooling::CompilationDatabase* compilationDatabase
|
||||
if (LogManager::getInstance()->getLoggingEnabled())
|
||||
{
|
||||
info = ClangInvocationInfo::getClangInvocationString(compilationDatabase);
|
||||
LOG_INFO("Clang Invocation: " + info.invocation.substr(0, ApplicationSettings::getInstance()->getVerboseIndexerLoggingEnabled() ? std::string::npos : 20000));
|
||||
LOG_INFO(
|
||||
"Clang Invocation: " +
|
||||
info.invocation.substr(
|
||||
0,
|
||||
ApplicationSettings::getInstance()->getVerboseIndexerLoggingEnabled() ? std::string::npos
|
||||
: 20000));
|
||||
|
||||
if (!info.errors.empty())
|
||||
{
|
||||
@@ -186,7 +198,8 @@ void CxxParser::runTool(clang::tooling::CompilationDatabase* compilationDatabase
|
||||
}
|
||||
}
|
||||
|
||||
clang::ASTFrontendAction* action = new ASTAction(m_client, canonicalFilePathCache, m_indexerStateInfo);
|
||||
clang::ASTFrontendAction* action = new ASTAction(
|
||||
m_client, canonicalFilePathCache, m_indexerStateInfo);
|
||||
tool.run(new SingleFrontendActionFactory(action));
|
||||
|
||||
if (!m_client->hasContent())
|
||||
@@ -204,16 +217,17 @@ void CxxParser::runTool(clang::tooling::CompilationDatabase* compilationDatabase
|
||||
true,
|
||||
true,
|
||||
sourceFilePath,
|
||||
ParseLocation(fileId, 1, 1)
|
||||
);
|
||||
ParseLocation(fileId, 1, 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<CxxDiagnosticConsumer> CxxParser::getDiagnostics(const FilePath& sourceFilePath, std::shared_ptr<CanonicalFilePathCache> canonicalFilePathCache, bool logErrors) const
|
||||
std::shared_ptr<CxxDiagnosticConsumer> CxxParser::getDiagnostics(
|
||||
const FilePath& sourceFilePath,
|
||||
std::shared_ptr<CanonicalFilePathCache> canonicalFilePathCache,
|
||||
bool logErrors) const
|
||||
{
|
||||
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> options = new clang::DiagnosticOptions();
|
||||
return std::make_shared<CxxDiagnosticConsumer>(
|
||||
llvm::errs(), &*options, m_client, canonicalFilePathCache, sourceFilePath, logErrors
|
||||
);
|
||||
llvm::errs(), &*options, m_client, canonicalFilePathCache, sourceFilePath, logErrors);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user