Clang: Use full paths in compilation database for symbol collector

We we FilePath and NativeFilePath so that compiler warns us if we mix them
up.

Change-Id: I33d7abc7e4e724dff2a9b2b9b23deea8b358ccfd
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-01-31 10:30:58 +01:00
parent 874dde6863
commit dd778bcb23
27 changed files with 355 additions and 304 deletions

View File

@@ -27,6 +27,8 @@
#include <iostream>
#include <nativefilepath.h>
namespace ClangBackEnd {
namespace {
@@ -44,67 +46,37 @@ String toNativePath(String &&path)
}
}
void ClangTool::addFile(std::string &&directory,
std::string &&fileName,
std::string &&content,
std::vector<std::string> &&commandLine)
void ClangTool::addFile(FilePath &&filePath,
Utils::SmallString &&content,
Utils::SmallStringVector &&commandLine)
{
m_fileContents.emplace_back(toNativePath(std::move(directory)),
std::move(fileName),
std::move(content),
std::move(commandLine));
NativeFilePath nativeFilePath{filePath};
const auto &fileContent = m_fileContents.back();
m_compilationDatabase.addFile(nativeFilePath, std::move(commandLine));
m_sourceFilePaths.push_back(Utils::SmallStringView{nativeFilePath});
m_compilationDatabase.addFile(fileContent.directory, fileContent.fileName, fileContent.commandLine);
m_sourceFilePaths.push_back(fileContent.filePath);
m_fileContents.emplace_back(std::move(nativeFilePath), std::move(content));
}
void ClangTool::addFiles(const FilePaths &filePaths, const Utils::SmallStringVector &arguments)
{
for (const FilePath &filePath : filePaths) {
std::vector<std::string> commandLine(arguments.begin(), arguments.end());
commandLine.push_back(std::string(filePath.name()));
std::string filePathStr(filePath.path());
auto commandLine = arguments;
NativeFilePath nativeFilePath{filePath};
addFile(filePath.directory(),
filePath.name(),
{},
std::move(commandLine));
commandLine.push_back(nativeFilePath.path());
addFile(filePath.clone(), {}, std::move(commandLine));
}
}
template <typename Container>
void ClangTool::addFiles(const Container &filePaths,
const Utils::SmallStringVector &arguments)
{
for (const typename Container::value_type &filePath : filePaths) {
auto found = std::find(filePath.rbegin(), filePath.rend(), '/');
auto fileNameBegin = found.base();
std::vector<std::string> commandLine(arguments.begin(), arguments.end());
commandLine.push_back(std::string(filePath));
addFile({filePath.begin(), std::prev(fileNameBegin)},
{fileNameBegin, filePath.end()},
{},
std::move(commandLine));
}
}
template
void ClangTool::addFiles<Utils::SmallStringVector>(const Utils::SmallStringVector &filePaths,
const Utils::SmallStringVector &arguments);
template
void ClangTool::addFiles<Utils::PathStringVector>(const Utils::PathStringVector &filePaths,
const Utils::SmallStringVector &arguments);
void ClangTool::addUnsavedFiles(const V2::FileContainers &unsavedFiles)
{
m_unsavedFileContents.reserve(m_unsavedFileContents.size() + unsavedFiles.size());
auto convertToUnsavedFileContent = [] (const V2::FileContainer &unsavedFile) {
return UnsavedFileContent{toNativePath(unsavedFile.filePath.path().clone()),
auto convertToUnsavedFileContent = [](const V2::FileContainer &unsavedFile) {
return UnsavedFileContent{NativeFilePath{unsavedFile.filePath},
unsavedFile.unsavedFileContent.clone()};
};
@@ -120,7 +92,12 @@ llvm::StringRef toStringRef(const String &string)
{
return llvm::StringRef(string.data(), string.size());
}
llvm::StringRef toStringRef(const NativeFilePath &path)
{
return llvm::StringRef(path.path().data(), path.path().size());
}
} // namespace
clang::tooling::ClangTool ClangTool::createTool() const
{
@@ -128,7 +105,7 @@ clang::tooling::ClangTool ClangTool::createTool() const
for (const auto &fileContent : m_fileContents) {
if (!fileContent.content.empty())
tool.mapVirtualFile(fileContent.filePath, fileContent.content);
tool.mapVirtualFile(toStringRef(fileContent.filePath), fileContent.content);
}
for (const auto &unsavedFileContent : m_unsavedFileContents)