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

@@ -50,10 +50,9 @@ ClangQueryGatherer::createSourceRangesForSource(
{
ClangQuery clangQuery(*filePathCache, std::move(query));
clangQuery.addFile(std::string(source.filePath.directory()),
std::string(source.filePath.name()),
std::string(source.unsavedFileContent),
std::vector<std::string>(source.commandLineArguments));
clangQuery.addFile(std::move(source.filePath),
std::move(source.unsavedFileContent),
std::move(source.commandLineArguments));
clangQuery.addUnsavedFiles(unsaved);

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)

View File

@@ -30,6 +30,7 @@
#include <clangrefactoringbackend_global.h>
#include <filecontainerv2.h>
#include <nativefilepath.h>
#include <sourcelocationscontainer.h>
#include <clang/Tooling/Refactoring.h>
@@ -43,50 +44,34 @@ namespace ClangBackEnd {
struct FileContent
{
FileContent(const std::string &directory,
const std::string &fileName,
const std::string &content,
const std::vector<std::string> &commandLine)
: directory(directory),
fileName(fileName),
filePath(directory + nativeSeparator + fileName),
content(content),
commandLine(commandLine)
FileContent(NativeFilePath &&filePath, const Utils::SmallString &content)
: filePath(std::move(filePath))
, content(std::move(content))
{}
std::string directory;
std::string fileName;
std::string filePath;
NativeFilePath filePath;
std::string content;
std::vector<std::string> commandLine;
};
struct UnsavedFileContent
{
UnsavedFileContent(Utils::PathString &&filePath,
Utils::SmallString &&content)
: filePath(std::move(filePath)),
content(std::move(content))
UnsavedFileContent(NativeFilePath &&filePath, Utils::SmallString &&content)
: filePath(std::move(filePath))
, content(std::move(content))
{}
Utils::PathString filePath;
NativeFilePath filePath;
Utils::SmallString content;
};
class ClangTool
{
public:
void addFile(std::string &&directory,
std::string &&fileName,
std::string &&content,
std::vector<std::string> &&commandLine);
template <typename Container>
void addFiles(const Container &filePaths,
const Utils::SmallStringVector &arguments);
void addFiles(const FilePaths &filePaths,
const Utils::SmallStringVector &arguments);
void addFile(FilePath &&filePath,
Utils::SmallString &&content,
Utils::SmallStringVector &&commandLine);
void addFiles(const FilePaths &filePaths, const Utils::SmallStringVector &arguments);
void addUnsavedFiles(const V2::FileContainers &unsavedFiles);
@@ -102,11 +87,4 @@ private:
std::vector<UnsavedFileContent> m_unsavedFileContents;
};
extern template
void ClangTool::addFiles<Utils::SmallStringVector>(const Utils::SmallStringVector &filePaths,
const Utils::SmallStringVector &arguments);
extern template
void ClangTool::addFiles<Utils::PathStringVector>(const Utils::PathStringVector &filePaths,
const Utils::SmallStringVector &arguments);
} // namespace ClangBackEnd

View File

@@ -27,20 +27,15 @@
#include "clangrefactoringbackend_global.h"
#include <filepath.h>
#include <nativefilepath.h>
namespace ClangBackEnd {
RefactoringCompilationDatabase::RefactoringCompilationDatabase()
{
}
namespace {
std::string concatFilePath(const clang::tooling::CompileCommand &compileCommand)
{
return compileCommand.Directory + nativeSeparator + compileCommand.Filename;
}
}
std::vector<clang::tooling::CompileCommand>
RefactoringCompilationDatabase::getCompileCommands(llvm::StringRef filePath) const
{
@@ -50,7 +45,7 @@ RefactoringCompilationDatabase::getCompileCommands(llvm::StringRef filePath) con
m_compileCommands.end(),
std::back_inserter(foundCommands),
[&] (const clang::tooling::CompileCommand &compileCommand) {
return filePath == concatFilePath(compileCommand);
return filePath == compileCommand.Filename;
});
return foundCommands;
@@ -66,7 +61,7 @@ RefactoringCompilationDatabase::getAllFiles() const
m_compileCommands.end(),
std::back_inserter(filePaths),
[&] (const clang::tooling::CompileCommand &compileCommand) {
return concatFilePath(compileCommand);
return compileCommand.Filename;
});
return filePaths;
@@ -78,12 +73,13 @@ RefactoringCompilationDatabase::getAllCompileCommands() const
return m_compileCommands;
}
void RefactoringCompilationDatabase::addFile(const std::string &directory,
const std::string &fileName,
const std::vector<std::string> &commandLine)
void RefactoringCompilationDatabase::addFile(NativeFilePathView filePath,
Utils::SmallStringVector &&commandLine)
{
m_compileCommands.emplace_back(directory, fileName, commandLine, llvm::StringRef());
m_compileCommands.emplace_back(std::string(filePath.directory()),
std::string(filePath),
std::vector<std::string>(commandLine),
llvm::StringRef());
}
} // namespace ClangBackEnd

View File

@@ -27,6 +27,8 @@
#include "clang/Tooling/CompilationDatabase.h"
#include <nativefilepath.h>
namespace ClangBackEnd {
@@ -39,9 +41,7 @@ public:
std::vector<std::string> getAllFiles() const override;
std::vector<clang::tooling::CompileCommand> getAllCompileCommands() const override;
void addFile(const std::string &directory,
const std::string &fileName,
const std::vector<std::string> &commandLine);
void addFile(NativeFilePathView filePath, Utils::SmallStringVector &&commandLine);
private:
std::vector<clang::tooling::CompileCommand> m_compileCommands;

View File

@@ -62,10 +62,9 @@ void RefactoringServer::requestSourceLocationsForRenamingMessage(RequestSourceLo
{
SymbolFinder symbolFinder(message.line, message.column, m_filePathCache);
symbolFinder.addFile(std::string(message.filePath.directory()),
std::string(message.filePath.name()),
std::string(message.unsavedContent),
std::vector<std::string>(message.commandLine));
symbolFinder.addFile(std::move(message.filePath),
std::move(message.unsavedContent),
std::move(message.commandLine));
symbolFinder.findSymbol();
@@ -79,10 +78,9 @@ void RefactoringServer::requestSourceRangesAndDiagnosticsForQueryMessage(
{
ClangQuery clangQuery(m_filePathCache, message.takeQuery());
clangQuery.addFile(std::string(message.source.filePath.directory()),
std::string(message.source.filePath.name()),
std::string(message.source.unsavedFileContent),
std::vector<std::string>(message.source.commandLineArguments));
clangQuery.addFile(std::move(message.source.filePath),
std::move(message.source.unsavedFileContent),
std::move(message.source.commandLineArguments));
clangQuery.findLocations();