Clang: Extend clang query

It's a first step to introduce clang query.

Change-Id: I4d001a8883f56066765ce6bc561fa3f49611c0a4
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Tim Jenssen
2016-11-23 13:13:38 +01:00
parent 52fc4a4ebd
commit 7f757884c5
70 changed files with 1921 additions and 653 deletions

View File

@@ -34,6 +34,7 @@
#endif
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Lexer.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FileUtilities.h>
@@ -41,6 +42,7 @@
#pragma GCC diagnostic pop
#endif
#include <iterator>
#include <cctype>
namespace ClangBackEnd {
@@ -68,75 +70,6 @@ Utils::SmallString fromNativePath(Container container)
return path;
}
inline Utils::SmallString getSourceText(const clang::FullSourceLoc &startFullSourceLocation,
uint startOffset,
uint endOffset)
{
auto startBuffer = startFullSourceLocation.getBufferData();
const auto bufferSize = endOffset - startOffset;
return Utils::SmallString(startBuffer.data() + startOffset, bufferSize + 1);
}
inline void makePrintable(Utils::SmallString &text)
{
text.replace("\n", " ");
text.replace("\t", " ");
auto end = std::unique(text.begin(), text.end(), [](char l, char r){
return std::isspace(l) && std::isspace(r) && l == r;
});
text.resize(std::distance(text.begin(), end));
}
inline void appendSourceRangeToSourceRangesContainer(
const clang::SourceRange &sourceRange,
ClangBackEnd::SourceRangesContainer &sourceRangesContainer,
const clang::SourceManager &sourceManager)
{
clang::FullSourceLoc startFullSourceLocation(sourceRange.getBegin(), sourceManager);
clang::FullSourceLoc endFullSourceLocation(sourceRange.getEnd(), sourceManager);
if (startFullSourceLocation.isFileID() && endFullSourceLocation.isFileID()) {
const auto startDecomposedLoction = startFullSourceLocation.getDecomposedLoc();
const auto endDecomposedLoction = endFullSourceLocation.getDecomposedLoc();
const auto fileId = startDecomposedLoction.first;
const auto startOffset = startDecomposedLoction.second;
const auto endOffset = endDecomposedLoction.second;
const auto fileEntry = sourceManager.getFileEntryForID(fileId);
auto filePath = absolutePath(fileEntry->getName());
const auto fileName = llvm::sys::path::filename(filePath);
llvm::sys::path::remove_filename(filePath);
Utils::SmallString content = getSourceText(startFullSourceLocation,
startOffset,
endOffset);
makePrintable(content);
sourceRangesContainer.insertFilePath(fileId.getHashValue(),
fromNativePath(filePath),
fromNativePath(fileName));
sourceRangesContainer.insertSourceRange(fileId.getHashValue(),
startFullSourceLocation.getSpellingLineNumber(),
startFullSourceLocation.getSpellingColumnNumber(),
startOffset,
endFullSourceLocation.getSpellingLineNumber(),
endFullSourceLocation.getSpellingColumnNumber(),
endOffset,
std::move(content));
}
}
inline
void appendSourceRangesToSourceRangesContainer(
ClangBackEnd::SourceRangesContainer &sourceRangesContainer,
const std::vector<clang::SourceRange> &sourceRanges,
const clang::SourceManager &sourceManager)
{
sourceRangesContainer.reserve(sourceRanges.size());
for (const auto &sourceRange : sourceRanges)
appendSourceRangeToSourceRangesContainer(sourceRange, sourceRangesContainer, sourceManager);
}
inline
void appendSourceLocationsToSourceLocationsContainer(
ClangBackEnd::SourceLocationsContainer &sourceLocationsContainer,