2017-01-30 11:24:46 +01: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
|
|
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
#include "builddependency.h"
|
2018-10-18 17:22:47 +02:00
|
|
|
#include "collectmacrospreprocessorcallbacks.h"
|
2017-09-29 13:46:53 +02:00
|
|
|
#include "sourcelocationsutils.h"
|
2018-10-18 17:22:47 +02:00
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
#include <filepathcachinginterface.h>
|
|
|
|
|
#include <filepathid.h>
|
|
|
|
|
|
|
|
|
|
#include <utils/smallstringvector.h>
|
2017-01-30 11:24:46 +01:00
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
#include <llvm/Support/MemoryBuffer.h>
|
|
|
|
|
|
2017-02-20 17:14:17 +01:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTemporaryDir>
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
class CollectBuildDependencyPreprocessorCallbacks final : public clang::PPCallbacks,
|
2018-10-18 17:22:47 +02:00
|
|
|
public CollectUsedMacrosAndSourcesPreprocessorCallbacksBase
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2018-11-12 19:27:51 +01:00
|
|
|
CollectBuildDependencyPreprocessorCallbacks(BuildDependency &buildDependency,
|
2018-09-11 17:02:45 +02:00
|
|
|
const FilePathCachingInterface &filePathCache,
|
2017-09-21 11:43:59 +02:00
|
|
|
const std::vector<uint> &excludedIncludeUID,
|
2017-09-29 13:46:53 +02:00
|
|
|
std::vector<uint> &alreadyIncludedFileUIDs,
|
2018-11-12 19:27:51 +01:00
|
|
|
clang::SourceManager &sourceManager,
|
2018-10-18 17:22:47 +02:00
|
|
|
SourcesManager &sourcesManager,
|
2018-11-12 19:27:51 +01:00
|
|
|
std::shared_ptr<clang::Preprocessor> preprocessor)
|
|
|
|
|
: CollectUsedMacrosAndSourcesPreprocessorCallbacksBase(buildDependency.usedMacros,
|
2018-10-18 17:22:47 +02:00
|
|
|
filePathCache,
|
|
|
|
|
sourceManager,
|
|
|
|
|
sourcesManager,
|
|
|
|
|
preprocessor,
|
2018-11-12 19:27:51 +01:00
|
|
|
buildDependency.sourceDependencies,
|
|
|
|
|
buildDependency.sourceFiles,
|
|
|
|
|
buildDependency.fileStatuses),
|
|
|
|
|
m_buildDependency(buildDependency),
|
2017-01-30 11:24:46 +01:00
|
|
|
m_excludedIncludeUID(excludedIncludeUID),
|
2018-10-18 17:22:47 +02:00
|
|
|
m_alreadyIncludedFileUIDs(alreadyIncludedFileUIDs)
|
2017-01-30 11:24:46 +01:00
|
|
|
{}
|
|
|
|
|
|
2018-10-18 17:22:47 +02:00
|
|
|
void FileChanged(clang::SourceLocation sourceLocation,
|
|
|
|
|
clang::PPCallbacks::FileChangeReason reason,
|
|
|
|
|
clang::SrcMgr::CharacteristicKind,
|
|
|
|
|
clang::FileID) override
|
|
|
|
|
{
|
|
|
|
|
if (reason == clang::PPCallbacks::EnterFile)
|
|
|
|
|
{
|
|
|
|
|
const clang::FileEntry *fileEntry = m_sourceManager->getFileEntryForID(
|
|
|
|
|
m_sourceManager->getFileID(sourceLocation));
|
|
|
|
|
if (fileEntry) {
|
|
|
|
|
addFileStatus(fileEntry);
|
|
|
|
|
addSourceFile(fileEntry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-29 13:46:53 +02:00
|
|
|
void InclusionDirective(clang::SourceLocation hashLocation,
|
2018-11-12 19:27:51 +01:00
|
|
|
const clang::Token & /*includeToken*/,
|
2017-02-20 18:26:18 +01:00
|
|
|
llvm::StringRef /*fileName*/,
|
2017-01-30 11:24:46 +01:00
|
|
|
bool /*isAngled*/,
|
|
|
|
|
clang::CharSourceRange /*fileNameRange*/,
|
|
|
|
|
const clang::FileEntry *file,
|
2017-02-20 18:26:18 +01:00
|
|
|
llvm::StringRef /*searchPath*/,
|
2017-01-30 11:24:46 +01:00
|
|
|
llvm::StringRef /*relativePath*/,
|
2018-10-18 11:27:49 +02:00
|
|
|
const clang::Module * /*imported*/,
|
|
|
|
|
clang::SrcMgr::CharacteristicKind fileType) override
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
2017-02-20 17:14:17 +01:00
|
|
|
if (!m_skipInclude && file) {
|
2018-10-18 17:22:47 +02:00
|
|
|
addSourceDependency(file, hashLocation);
|
2017-01-31 14:21:05 +01:00
|
|
|
auto fileUID = file->getUID();
|
2018-11-12 19:27:51 +01:00
|
|
|
auto sourceFileUID = m_sourceManager
|
|
|
|
|
->getFileEntryForID(m_sourceManager->getFileID(hashLocation))
|
|
|
|
|
->getUID();
|
|
|
|
|
auto notAlreadyIncluded = isNotAlreadyIncluded(fileUID);
|
|
|
|
|
if (notAlreadyIncluded.first) {
|
|
|
|
|
m_alreadyIncludedFileUIDs.insert(notAlreadyIncluded.second, fileUID);
|
|
|
|
|
FilePath filePath = filePathFromFile(file);
|
|
|
|
|
if (!filePath.empty()) {
|
|
|
|
|
FilePathId includeId = m_filePathCache.filePathId(filePath);
|
|
|
|
|
|
|
|
|
|
time_t lastModified = file->getModificationTime();
|
|
|
|
|
|
|
|
|
|
SourceType sourceType = SourceType::UserInclude;
|
|
|
|
|
if (isSystem(fileType)) {
|
|
|
|
|
if (isInSystemHeader(hashLocation))
|
|
|
|
|
sourceType = SourceType::SystemInclude;
|
|
|
|
|
else
|
|
|
|
|
sourceType = SourceType::TopSystemInclude;
|
|
|
|
|
} else if (isNotInExcludedIncludeUID(fileUID)
|
|
|
|
|
&& isInExcludedIncludeUID(sourceFileUID)) {
|
|
|
|
|
sourceType = SourceType::TopInclude;
|
2017-02-16 18:25:19 +01:00
|
|
|
}
|
2018-11-12 19:27:51 +01:00
|
|
|
|
|
|
|
|
addInclude({includeId, sourceType, lastModified});
|
2017-01-31 14:21:05 +01:00
|
|
|
}
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-20 17:14:17 +01:00
|
|
|
|
|
|
|
|
m_skipInclude = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
bool FileNotFound(clang::StringRef /*fileNameRef*/,
|
|
|
|
|
clang::SmallVectorImpl<char> &recoveryPath) override
|
2017-02-20 17:14:17 +01:00
|
|
|
{
|
2018-11-12 19:27:51 +01:00
|
|
|
auto dummyPath = llvm::StringRef("/dummyPath");
|
2017-02-20 17:14:17 +01:00
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
recoveryPath.append(std::cbegin(dummyPath), std::cend(dummyPath));
|
2017-02-20 17:14:17 +01:00
|
|
|
|
|
|
|
|
m_skipInclude = true;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 17:22:47 +02:00
|
|
|
|
|
|
|
|
void Ifndef(clang::SourceLocation,
|
|
|
|
|
const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition) override
|
|
|
|
|
{
|
|
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Ifdef(clang::SourceLocation,
|
|
|
|
|
const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition) override
|
|
|
|
|
{
|
|
|
|
|
addUsedMacro( macroNameToken, macroDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Defined(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
clang::SourceRange) override
|
|
|
|
|
{
|
|
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MacroExpands(const clang::Token ¯oNameToken,
|
|
|
|
|
const clang::MacroDefinition ¯oDefinition,
|
|
|
|
|
clang::SourceRange,
|
|
|
|
|
const clang::MacroArgs *) override
|
2018-10-18 11:27:49 +02:00
|
|
|
{
|
2018-10-18 17:22:47 +02:00
|
|
|
addUsedMacro(macroNameToken, macroDefinition);
|
2018-10-18 11:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 17:22:47 +02:00
|
|
|
void EndOfMainFile() override
|
2018-10-18 11:27:49 +02:00
|
|
|
{
|
2018-10-18 17:22:47 +02:00
|
|
|
filterOutHeaderGuards();
|
|
|
|
|
mergeUsedMacros();
|
|
|
|
|
m_sourcesManager.updateModifiedTimeStamps();
|
2018-10-18 11:27:49 +02:00
|
|
|
}
|
|
|
|
|
|
2017-02-20 17:14:17 +01:00
|
|
|
void ensureDirectory(const QString &directory, const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
QStringList directoryEntries = fileName.split('/');
|
|
|
|
|
directoryEntries.pop_back();
|
|
|
|
|
|
|
|
|
|
if (!directoryEntries.isEmpty())
|
|
|
|
|
QDir(directory).mkpath(directoryEntries.join('/'));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
bool isNotInExcludedIncludeUID(uint uid) const
|
|
|
|
|
{
|
2018-03-28 16:00:10 +02:00
|
|
|
return !isInExcludedIncludeUID(uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isInExcludedIncludeUID(uint uid) const
|
|
|
|
|
{
|
|
|
|
|
return std::binary_search(m_excludedIncludeUID.begin(),
|
|
|
|
|
m_excludedIncludeUID.end(),
|
|
|
|
|
uid);
|
2017-01-30 11:24:46 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
std::pair<bool, std::vector<uint>::iterator> isNotAlreadyIncluded(uint uid) const
|
2017-01-30 11:24:46 +01:00
|
|
|
{
|
|
|
|
|
auto range = std::equal_range(m_alreadyIncludedFileUIDs.begin(),
|
|
|
|
|
m_alreadyIncludedFileUIDs.end(),
|
|
|
|
|
uid);
|
|
|
|
|
|
|
|
|
|
return {range.first == range.second, range.first};
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-16 17:48:53 +01:00
|
|
|
static FilePath filePathFromFile(const clang::FileEntry *file)
|
2017-02-16 18:25:19 +01:00
|
|
|
{
|
2017-11-16 17:48:53 +01:00
|
|
|
return FilePath::fromNativeFilePath(absolutePath(file->getName()));
|
2017-02-16 18:25:19 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-12 19:27:51 +01:00
|
|
|
void addInclude(SourceEntry sourceEntry)
|
|
|
|
|
{
|
|
|
|
|
auto &includes = m_buildDependency.includes;
|
|
|
|
|
auto found = std::lower_bound(includes.begin(),
|
|
|
|
|
includes.end(),
|
|
|
|
|
sourceEntry,
|
|
|
|
|
[](auto first, auto second) { return first < second; });
|
|
|
|
|
|
|
|
|
|
if (found == includes.end() || *found != sourceEntry)
|
|
|
|
|
includes.emplace(found, sourceEntry);
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-30 11:24:46 +01:00
|
|
|
private:
|
2018-11-12 19:27:51 +01:00
|
|
|
FilePathIds m_containsMissingIncludes;
|
|
|
|
|
BuildDependency &m_buildDependency;
|
2017-09-21 11:43:59 +02:00
|
|
|
const std::vector<uint> &m_excludedIncludeUID;
|
|
|
|
|
std::vector<uint> &m_alreadyIncludedFileUIDs;
|
2017-02-20 17:14:17 +01:00
|
|
|
bool m_skipInclude = false;
|
2017-01-30 11:24:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|