2017-10-26 13:23:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 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
|
|
|
|
|
|
2018-02-01 18:29:45 +01:00
|
|
|
#include "filestatus.h"
|
2018-01-22 14:21:01 +01:00
|
|
|
#include "symbolsvisitorbase.h"
|
2018-01-31 13:53:57 +01:00
|
|
|
#include "sourcedependency.h"
|
2017-10-26 13:23:27 +02:00
|
|
|
#include "sourcelocationsutils.h"
|
2018-01-22 14:21:01 +01:00
|
|
|
#include "sourcelocationentry.h"
|
|
|
|
|
#include "symbolentry.h"
|
2018-01-25 15:20:47 +01:00
|
|
|
#include "usedmacro.h"
|
2017-10-26 13:23:27 +02:00
|
|
|
|
|
|
|
|
#include <filepath.h>
|
|
|
|
|
#include <filepathid.h>
|
|
|
|
|
|
|
|
|
|
#include <clang/Lex/MacroInfo.h>
|
|
|
|
|
#include <clang/Lex/PPCallbacks.h>
|
|
|
|
|
#include <clang/Lex/Preprocessor.h>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2018-01-22 14:21:01 +01:00
|
|
|
class CollectMacrosPreprocessorCallbacks final : public clang::PPCallbacks,
|
|
|
|
|
public SymbolsVisitorBase
|
2017-10-26 13:23:27 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2018-01-22 14:21:01 +01:00
|
|
|
CollectMacrosPreprocessorCallbacks(SymbolEntries &symbolEntries,
|
|
|
|
|
SourceLocationEntries &sourceLocationEntries,
|
|
|
|
|
FilePathIds &sourceFiles,
|
2018-01-25 15:20:47 +01:00
|
|
|
UsedMacros &usedMacros,
|
2018-02-01 18:29:45 +01:00
|
|
|
FileStatuses &fileStatuses,
|
2018-01-31 13:53:57 +01:00
|
|
|
SourceDependencies &sourceDependencies,
|
2018-01-22 14:21:01 +01:00
|
|
|
FilePathCachingInterface &filePathCache,
|
2018-01-23 14:10:17 +01:00
|
|
|
const clang::SourceManager &sourceManager,
|
|
|
|
|
std::shared_ptr<clang::Preprocessor> &&preprocessor)
|
2018-01-22 14:21:01 +01:00
|
|
|
: SymbolsVisitorBase(filePathCache, sourceManager),
|
2018-01-23 14:10:17 +01:00
|
|
|
m_preprocessor(std::move(preprocessor)),
|
2018-02-02 10:29:48 +03:00
|
|
|
m_sourceDependencies(sourceDependencies),
|
2018-01-22 14:21:01 +01:00
|
|
|
m_symbolEntries(symbolEntries),
|
|
|
|
|
m_sourceLocationEntries(sourceLocationEntries),
|
|
|
|
|
m_sourceFiles(sourceFiles),
|
2018-01-29 15:47:53 +01:00
|
|
|
m_usedMacros(usedMacros),
|
2018-02-01 18:29:45 +01:00
|
|
|
m_fileStatuses(fileStatuses)
|
2017-10-26 13:23:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-29 15:47:53 +01:00
|
|
|
void FileChanged(clang::SourceLocation sourceLocation,
|
|
|
|
|
clang::PPCallbacks::FileChangeReason reason,
|
|
|
|
|
clang::SrcMgr::CharacteristicKind,
|
|
|
|
|
clang::FileID)
|
|
|
|
|
{
|
|
|
|
|
if (reason == clang::PPCallbacks::EnterFile)
|
|
|
|
|
{
|
|
|
|
|
const clang::FileEntry *fileEntry = m_sourceManager.getFileEntryForID(
|
|
|
|
|
m_sourceManager.getFileID(sourceLocation));
|
|
|
|
|
if (fileEntry) {
|
2018-02-01 18:29:45 +01:00
|
|
|
m_fileStatuses.emplace_back(filePathId(fileEntry),
|
2018-02-14 14:06:33 +01:00
|
|
|
fileEntry->getSize(),
|
|
|
|
|
fileEntry->getModificationTime());
|
2018-01-31 13:53:57 +01:00
|
|
|
addSourceFile(fileEntry);
|
2018-01-29 15:47:53 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-31 13:53:57 +01:00
|
|
|
void InclusionDirective(clang::SourceLocation hashLocation,
|
2017-10-26 13:23:27 +02:00
|
|
|
const clang::Token &/*includeToken*/,
|
|
|
|
|
llvm::StringRef /*fileName*/,
|
|
|
|
|
bool /*isAngled*/,
|
|
|
|
|
clang::CharSourceRange /*fileNameRange*/,
|
|
|
|
|
const clang::FileEntry *file,
|
|
|
|
|
llvm::StringRef /*searchPath*/,
|
|
|
|
|
llvm::StringRef /*relativePath*/,
|
|
|
|
|
const clang::Module * /*imported*/) override
|
|
|
|
|
{
|
|
|
|
|
if (!m_skipInclude && file)
|
2018-01-31 13:53:57 +01:00
|
|
|
addSourceDependency(file, hashLocation);
|
2017-10-26 13:23:27 +02:00
|
|
|
|
|
|
|
|
m_skipInclude = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileNotFound(clang::StringRef /*fileNameRef*/, clang::SmallVectorImpl<char> &/*recoveryPath*/) override
|
|
|
|
|
{
|
|
|
|
|
m_skipInclude = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-22 14:21:01 +01:00
|
|
|
void Ifndef(clang::SourceLocation,
|
|
|
|
|
const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition) override
|
|
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
2018-01-22 14:21:01 +01:00
|
|
|
addMacroAsSymbol(macroNameToken,
|
|
|
|
|
firstMacroInfo(macroDefinition.getLocalDirective()),
|
|
|
|
|
SymbolType::MacroUsage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ifdef(clang::SourceLocation,
|
|
|
|
|
const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition) override
|
|
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro( macroNameToken, macroDefinition);
|
2018-01-22 14:21:01 +01:00
|
|
|
addMacroAsSymbol(macroNameToken,
|
|
|
|
|
firstMacroInfo(macroDefinition.getLocalDirective()),
|
|
|
|
|
SymbolType::MacroUsage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Defined(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
clang::SourceRange) override
|
|
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
2018-01-22 14:21:01 +01:00
|
|
|
addMacroAsSymbol(macroNameToken,
|
|
|
|
|
firstMacroInfo(macroDefinition.getLocalDirective()),
|
|
|
|
|
SymbolType::MacroUsage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroDefined(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDirective *macroDirective) override
|
|
|
|
|
{
|
2018-01-23 14:10:17 +01:00
|
|
|
addMacroAsSymbol(macroNameToken, firstMacroInfo(macroDirective), SymbolType::MacroDefinition);
|
2018-01-22 14:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroUndefined(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
const clang::MacroDirective *) override
|
|
|
|
|
{
|
|
|
|
|
addMacroAsSymbol(macroNameToken,
|
|
|
|
|
firstMacroInfo(macroDefinition.getLocalDirective()),
|
|
|
|
|
SymbolType::MacroUndefinition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroExpands(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
clang::SourceRange,
|
|
|
|
|
const clang::MacroArgs *) override
|
|
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
2018-01-22 14:21:01 +01:00
|
|
|
addMacroAsSymbol(macroNameToken,
|
|
|
|
|
firstMacroInfo(macroDefinition.getLocalDirective()),
|
|
|
|
|
SymbolType::MacroUsage);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-23 14:10:17 +01:00
|
|
|
void EndOfMainFile() override
|
|
|
|
|
{
|
|
|
|
|
filterOutHeaderGuards();
|
2018-01-25 15:20:47 +01:00
|
|
|
mergeUsedMacros();
|
2018-01-23 14:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void filterOutHeaderGuards()
|
|
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
auto partitionPoint = std::stable_partition(m_maybeUsedMacros.begin(),
|
|
|
|
|
m_maybeUsedMacros.end(),
|
|
|
|
|
[&] (const UsedMacro &usedMacro) {
|
|
|
|
|
llvm::StringRef id{usedMacro.macroName.data(), usedMacro.macroName.size()};
|
2018-01-23 14:10:17 +01:00
|
|
|
clang::IdentifierInfo &identifierInfo = m_preprocessor->getIdentifierTable().get(id);
|
|
|
|
|
clang::MacroInfo *macroInfo = m_preprocessor->getMacroInfo(&identifierInfo);
|
|
|
|
|
return !macroInfo || !macroInfo->isUsedForHeaderGuard();
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-25 15:20:47 +01:00
|
|
|
m_maybeUsedMacros.erase(partitionPoint, m_maybeUsedMacros.end());
|
2018-01-23 14:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 15:20:47 +01:00
|
|
|
void mergeUsedMacros()
|
2018-01-23 14:10:17 +01:00
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
m_usedMacros.reserve(m_usedMacros.size() + m_maybeUsedMacros.size());
|
|
|
|
|
auto insertionPoint = m_usedMacros.insert(m_usedMacros.end(),
|
|
|
|
|
m_maybeUsedMacros.begin(),
|
|
|
|
|
m_maybeUsedMacros.end());
|
|
|
|
|
std::inplace_merge(m_usedMacros.begin(), insertionPoint, m_usedMacros.end());
|
2018-01-23 14:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 15:20:47 +01:00
|
|
|
static void addUsedMacro(UsedMacro &&usedMacro, UsedMacros &usedMacros)
|
2018-01-23 14:10:17 +01:00
|
|
|
{
|
2018-01-25 15:20:47 +01:00
|
|
|
auto found = std::lower_bound(usedMacros.begin(),
|
|
|
|
|
usedMacros.end(), usedMacro);
|
2018-01-23 14:10:17 +01:00
|
|
|
|
2018-01-25 15:20:47 +01:00
|
|
|
if (found == usedMacros.end() || *found != usedMacro)
|
|
|
|
|
usedMacros.insert(found, std::move(usedMacro));
|
2018-01-23 14:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-25 15:20:47 +01:00
|
|
|
void addUsedMacro(const clang::Token ¯oNameToken,
|
2018-01-22 14:21:01 +01:00
|
|
|
const clang::MacroDefinition ¯oDefinition)
|
|
|
|
|
{
|
|
|
|
|
clang::MacroInfo *macroInfo = macroDefinition.getMacroInfo();
|
2018-01-25 15:20:47 +01:00
|
|
|
UsedMacro usedMacro{macroNameToken.getIdentifierInfo()->getName(),
|
2018-01-23 14:10:17 +01:00
|
|
|
filePathId(macroNameToken.getLocation())};
|
|
|
|
|
if (macroInfo)
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro(std::move(usedMacro), m_usedMacros);
|
2018-01-23 14:10:17 +01:00
|
|
|
else
|
2018-01-25 15:20:47 +01:00
|
|
|
addUsedMacro(std::move(usedMacro), m_maybeUsedMacros);
|
2018-01-22 14:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const clang::MacroInfo *firstMacroInfo(const clang::MacroDirective *macroDirective)
|
|
|
|
|
{
|
|
|
|
|
if (macroDirective) {
|
|
|
|
|
const clang::MacroDirective *previousDirective = macroDirective;
|
|
|
|
|
do {
|
|
|
|
|
macroDirective = previousDirective;
|
|
|
|
|
previousDirective = macroDirective->getPrevious();
|
|
|
|
|
} while (previousDirective);
|
|
|
|
|
|
|
|
|
|
return macroDirective->getMacroInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addMacroAsSymbol(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroInfo *macroInfo,
|
|
|
|
|
SymbolType symbolType)
|
|
|
|
|
{
|
|
|
|
|
clang::SourceLocation sourceLocation = macroNameToken.getLocation();
|
|
|
|
|
if (macroInfo && sourceLocation.isFileID()) {
|
|
|
|
|
FilePathId fileId = filePathId(sourceLocation);
|
|
|
|
|
if (fileId.isValid()) {
|
|
|
|
|
auto macroName = macroNameToken.getIdentifierInfo()->getName();
|
|
|
|
|
SymbolIndex globalId = toSymbolIndex(macroInfo);
|
|
|
|
|
|
|
|
|
|
auto found = m_symbolEntries.find(globalId);
|
|
|
|
|
if (found == m_symbolEntries.end()) {
|
|
|
|
|
Utils::optional<Utils::PathString> usr = generateUSR(macroName, sourceLocation);
|
|
|
|
|
if (usr) {
|
|
|
|
|
m_symbolEntries.emplace(std::piecewise_construct,
|
|
|
|
|
std::forward_as_tuple(globalId),
|
|
|
|
|
std::forward_as_tuple(std::move(usr.value()), macroName));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_sourceLocationEntries.emplace_back(globalId,
|
|
|
|
|
fileId,
|
|
|
|
|
lineColum(sourceLocation),
|
|
|
|
|
symbolType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-26 13:23:27 +02:00
|
|
|
void addSourceFile(const clang::FileEntry *file)
|
|
|
|
|
{
|
2018-01-31 13:53:57 +01:00
|
|
|
auto id = filePathId(file);
|
2017-10-26 13:23:27 +02:00
|
|
|
|
2018-01-31 13:53:57 +01:00
|
|
|
auto found = std::find(m_sourceFiles.begin(), m_sourceFiles.end(), id);
|
2017-10-26 13:23:27 +02:00
|
|
|
|
2018-01-31 13:53:57 +01:00
|
|
|
if (found == m_sourceFiles.end() || *found != id)
|
|
|
|
|
m_sourceFiles.insert(found, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addSourceDependency(const clang::FileEntry *file, clang::SourceLocation includeLocation)
|
|
|
|
|
{
|
|
|
|
|
auto includeFilePathId = filePathId(includeLocation);
|
|
|
|
|
auto includedFilePathId = filePathId(file);
|
|
|
|
|
|
|
|
|
|
m_sourceDependencies.emplace_back(includeFilePathId, includedFilePathId);
|
2017-10-26 13:23:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2018-01-25 15:20:47 +01:00
|
|
|
UsedMacros m_maybeUsedMacros;
|
2018-01-23 14:10:17 +01:00
|
|
|
std::shared_ptr<clang::Preprocessor> m_preprocessor;
|
2018-01-31 13:53:57 +01:00
|
|
|
SourceDependencies &m_sourceDependencies;
|
2018-01-22 14:21:01 +01:00
|
|
|
SymbolEntries &m_symbolEntries;
|
|
|
|
|
SourceLocationEntries &m_sourceLocationEntries;
|
2017-10-26 13:23:27 +02:00
|
|
|
FilePathIds &m_sourceFiles;
|
2018-01-25 15:20:47 +01:00
|
|
|
UsedMacros &m_usedMacros;
|
2018-02-01 18:29:45 +01:00
|
|
|
FileStatuses &m_fileStatuses;
|
2017-10-26 13:23:27 +02:00
|
|
|
bool m_skipInclude = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|