2016-08-04 15:26:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <sourcelocationscontainer.h>
|
2016-11-15 15:38:12 +01:00
|
|
|
#include <sourcerangescontainer.h>
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include <clang/Basic/SourceManager.h>
|
|
|
|
|
#include <llvm/Support/FileSystem.h>
|
|
|
|
|
#include <llvm/Support/FileUtilities.h>
|
|
|
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
#include <cctype>
|
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
|
|
|
|
inline
|
2016-08-08 15:58:48 +02:00
|
|
|
llvm::SmallString<256> absolutePath(const char *path)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
llvm::SmallString<256> absolutePath(path);
|
|
|
|
|
|
|
|
|
|
if (!llvm::sys::path::is_absolute(absolutePath))
|
|
|
|
|
llvm::sys::fs::make_absolute(absolutePath);
|
|
|
|
|
|
2016-08-08 15:58:48 +02:00
|
|
|
return absolutePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Container>
|
|
|
|
|
Utils::SmallString fromNativePath(Container container)
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallString path(container.data(), container.size());
|
|
|
|
|
|
2016-11-11 14:26:52 +01:00
|
|
|
#ifdef _WIN32
|
2016-08-08 15:58:48 +02:00
|
|
|
std::replace(path.begin(), path.end(), '\\', '/');
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return path;
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
inline
|
|
|
|
|
void appendSourceLocationsToSourceLocationsContainer(
|
|
|
|
|
ClangBackEnd::SourceLocationsContainer &sourceLocationsContainer,
|
|
|
|
|
const std::vector<clang::SourceLocation> &sourceLocations,
|
|
|
|
|
const clang::SourceManager &sourceManager)
|
|
|
|
|
{
|
|
|
|
|
sourceLocationsContainer.reserve(sourceLocations.size());
|
|
|
|
|
|
|
|
|
|
for (const auto &sourceLocation : sourceLocations) {
|
|
|
|
|
clang::FullSourceLoc fullSourceLocation(sourceLocation, sourceManager);
|
2016-11-15 15:38:12 +01:00
|
|
|
const auto decomposedLoction = fullSourceLocation.getDecomposedLoc();
|
|
|
|
|
const auto fileId = decomposedLoction.first;
|
|
|
|
|
const auto offset = decomposedLoction.second;
|
|
|
|
|
const auto fileEntry = sourceManager.getFileEntryForID(fileId);
|
2016-08-08 15:58:48 +02:00
|
|
|
auto filePath = absolutePath(fileEntry->getName());
|
2016-11-15 15:38:12 +01:00
|
|
|
const auto fileName = llvm::sys::path::filename(filePath);
|
2016-08-08 15:58:48 +02:00
|
|
|
llvm::sys::path::remove_filename(filePath);
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
sourceLocationsContainer.insertFilePath(fileId.getHashValue(),
|
2016-08-08 15:58:48 +02:00
|
|
|
fromNativePath(filePath),
|
|
|
|
|
fromNativePath(fileName));
|
2016-08-04 15:26:53 +02:00
|
|
|
sourceLocationsContainer.insertSourceLocation(fileId.getHashValue(),
|
|
|
|
|
fullSourceLocation.getSpellingLineNumber(),
|
2016-11-15 15:38:12 +01:00
|
|
|
fullSourceLocation.getSpellingColumnNumber(),
|
|
|
|
|
offset);
|
2016-08-04 15:26:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 15:38:12 +01:00
|
|
|
|
|
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
} // namespace ClangBackEnd
|