2015-08-11 19:37:48 +08:00
|
|
|
//===-------- IncludeInserter.cpp - clang-tidy ----------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "IncludeInserter.h"
|
2016-03-02 22:12:17 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
2015-08-11 19:37:48 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
2016-05-03 10:54:05 +08:00
|
|
|
namespace utils {
|
2015-08-11 19:37:48 +08:00
|
|
|
|
|
|
|
class IncludeInserterCallback : public PPCallbacks {
|
|
|
|
public:
|
2015-08-11 20:02:28 +08:00
|
|
|
explicit IncludeInserterCallback(IncludeInserter *Inserter)
|
|
|
|
: Inserter(Inserter) {}
|
2015-08-11 19:37:48 +08:00
|
|
|
// Implements PPCallbacks::InclusionDerective(). Records the names and source
|
|
|
|
// locations of the inclusions in the main source file being processed.
|
|
|
|
void InclusionDirective(SourceLocation HashLocation,
|
2016-03-02 22:12:17 +08:00
|
|
|
const Token & IncludeToken,
|
2015-08-11 19:37:48 +08:00
|
|
|
StringRef FileNameRef, bool IsAngled,
|
|
|
|
CharSourceRange FileNameRange,
|
|
|
|
const FileEntry * /*IncludedFile*/,
|
|
|
|
StringRef /*SearchPath*/, StringRef /*RelativePath*/,
|
|
|
|
const Module * /*ImportedModule*/) override {
|
2015-08-11 20:02:28 +08:00
|
|
|
Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
|
2016-03-02 22:12:17 +08:00
|
|
|
IncludeToken.getEndLoc());
|
2015-08-11 19:37:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-08-11 20:02:28 +08:00
|
|
|
IncludeInserter *Inserter;
|
2015-08-11 19:37:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
IncludeSorter::IncludeStyle Style)
|
|
|
|
: SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
|
|
|
|
|
2015-10-21 05:45:52 +08:00
|
|
|
IncludeInserter::~IncludeInserter() {}
|
2015-08-11 19:37:48 +08:00
|
|
|
|
|
|
|
std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() {
|
|
|
|
return llvm::make_unique<IncludeInserterCallback>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Optional<FixItHint>
|
|
|
|
IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header,
|
|
|
|
bool IsAngled) {
|
|
|
|
// We assume the same Header will never be included both angled and not
|
|
|
|
// angled.
|
2015-08-20 05:02:27 +08:00
|
|
|
if (!InsertedHeaders[FileID].insert(Header).second)
|
2015-08-11 19:37:48 +08:00
|
|
|
return llvm::None;
|
2015-08-20 05:02:27 +08:00
|
|
|
|
2015-08-11 19:37:48 +08:00
|
|
|
if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
|
2015-08-14 20:33:25 +08:00
|
|
|
// This may happen if there have been no preprocessor directives in this
|
|
|
|
// file.
|
|
|
|
IncludeSorterByFile.insert(std::make_pair(
|
|
|
|
FileID,
|
|
|
|
llvm::make_unique<IncludeSorter>(
|
|
|
|
&SourceMgr, &LangOpts, FileID,
|
|
|
|
SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)),
|
|
|
|
Style)));
|
2015-08-11 19:37:48 +08:00
|
|
|
}
|
|
|
|
return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
|
|
|
|
}
|
|
|
|
|
2016-05-16 22:34:20 +08:00
|
|
|
void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled,
|
2015-08-11 19:37:48 +08:00
|
|
|
SourceLocation HashLocation,
|
2016-05-16 22:34:20 +08:00
|
|
|
SourceLocation EndLocation) {
|
2015-08-11 19:37:48 +08:00
|
|
|
FileID FileID = SourceMgr.getFileID(HashLocation);
|
|
|
|
if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
|
|
|
|
IncludeSorterByFile.insert(std::make_pair(
|
|
|
|
FileID, llvm::make_unique<IncludeSorter>(
|
|
|
|
&SourceMgr, &LangOpts, FileID,
|
|
|
|
SourceMgr.getFilename(HashLocation), Style)));
|
|
|
|
}
|
2016-05-16 22:34:20 +08:00
|
|
|
IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation,
|
|
|
|
EndLocation);
|
2015-08-11 19:37:48 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 10:54:05 +08:00
|
|
|
} // namespace utils
|
2015-08-11 19:37:48 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|