summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2015-08-14 14:31:31 +0000
committerAlexander Kornienko <alexfh@google.com>2015-08-14 14:31:31 +0000
commit5f252cac51f62695afb003a597eac83510095509 (patch)
tree526f5791e724a7f89a3e195e598e9996e26762f7 /clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
parentda8a3b903b4506e4b6a1498fc23f71b2a5d49f02 (diff)
downloadbcm5719-llvm-5f252cac51f62695afb003a597eac83510095509.tar.gz
bcm5719-llvm-5f252cac51f62695afb003a597eac83510095509.zip
[clang-tidy] Move IncludeSorter.* and IncludeInserter.* to clang-tidy/utils/
This is better structurally and it also fixes a linker error in the configure build. llvm-svn: 245052
Diffstat (limited to 'clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
new file mode 100644
index 00000000000..e0b977386d8
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/utils/IncludeInserter.cpp
@@ -0,0 +1,84 @@
+//===-------- 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"
+
+namespace clang {
+namespace tidy {
+
+class IncludeInserterCallback : public PPCallbacks {
+public:
+ explicit IncludeInserterCallback(IncludeInserter *Inserter)
+ : Inserter(Inserter) {}
+ // Implements PPCallbacks::InclusionDerective(). Records the names and source
+ // locations of the inclusions in the main source file being processed.
+ void InclusionDirective(SourceLocation HashLocation,
+ const Token & /*include_token*/,
+ StringRef FileNameRef, bool IsAngled,
+ CharSourceRange FileNameRange,
+ const FileEntry * /*IncludedFile*/,
+ StringRef /*SearchPath*/, StringRef /*RelativePath*/,
+ const Module * /*ImportedModule*/) override {
+ Inserter->AddInclude(FileNameRef, IsAngled, HashLocation,
+ FileNameRange.getEnd());
+ }
+
+private:
+ IncludeInserter *Inserter;
+};
+
+IncludeInserter::IncludeInserter(const SourceManager &SourceMgr,
+ const LangOptions &LangOpts,
+ IncludeSorter::IncludeStyle Style)
+ : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {}
+
+IncludeInserter::~IncludeInserter() {}
+
+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.
+ if (!InsertedHeaders.insert(std::make_pair(FileID, std::set<std::string>()))
+ .second) {
+ return llvm::None;
+ }
+ if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) {
+ // 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)));
+ }
+ return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled);
+}
+
+void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled,
+ SourceLocation HashLocation,
+ SourceLocation end_location) {
+ 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)));
+ }
+ IncludeSorterByFile[FileID]->AddInclude(file_name, IsAngled, HashLocation,
+ end_location);
+}
+
+} // namespace tidy
+} // namespace clang
OpenPOWER on IntegriCloud