Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 1 | //===-------- IncludeInserter.cpp - clang-tidy ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "IncludeInserter.h" |
Haojian Wu | 7d08ba2 | 2016-03-02 14:12:17 +0000 | [diff] [blame] | 11 | #include "clang/Lex/Token.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 12 | |
| 13 | namespace clang { |
| 14 | namespace tidy { |
| 15 | |
| 16 | class IncludeInserterCallback : public PPCallbacks { |
| 17 | public: |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 18 | explicit IncludeInserterCallback(IncludeInserter *Inserter) |
| 19 | : Inserter(Inserter) {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 20 | // Implements PPCallbacks::InclusionDerective(). Records the names and source |
| 21 | // locations of the inclusions in the main source file being processed. |
| 22 | void InclusionDirective(SourceLocation HashLocation, |
Haojian Wu | 7d08ba2 | 2016-03-02 14:12:17 +0000 | [diff] [blame] | 23 | const Token & IncludeToken, |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 24 | StringRef FileNameRef, bool IsAngled, |
| 25 | CharSourceRange FileNameRange, |
| 26 | const FileEntry * /*IncludedFile*/, |
| 27 | StringRef /*SearchPath*/, StringRef /*RelativePath*/, |
| 28 | const Module * /*ImportedModule*/) override { |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 29 | Inserter->AddInclude(FileNameRef, IsAngled, HashLocation, |
Haojian Wu | 7d08ba2 | 2016-03-02 14:12:17 +0000 | [diff] [blame] | 30 | IncludeToken.getEndLoc()); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | private: |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 34 | IncludeInserter *Inserter; |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | IncludeInserter::IncludeInserter(const SourceManager &SourceMgr, |
| 38 | const LangOptions &LangOpts, |
| 39 | IncludeSorter::IncludeStyle Style) |
| 40 | : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {} |
| 41 | |
David Blaikie | e04a3da | 2015-10-20 21:45:52 +0000 | [diff] [blame] | 42 | IncludeInserter::~IncludeInserter() {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 43 | |
| 44 | std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() { |
| 45 | return llvm::make_unique<IncludeInserterCallback>(this); |
| 46 | } |
| 47 | |
| 48 | llvm::Optional<FixItHint> |
| 49 | IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header, |
| 50 | bool IsAngled) { |
| 51 | // We assume the same Header will never be included both angled and not |
| 52 | // angled. |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 53 | if (!InsertedHeaders[FileID].insert(Header).second) |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 54 | return llvm::None; |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 55 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 56 | if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 57 | // This may happen if there have been no preprocessor directives in this |
| 58 | // file. |
| 59 | IncludeSorterByFile.insert(std::make_pair( |
| 60 | FileID, |
| 61 | llvm::make_unique<IncludeSorter>( |
| 62 | &SourceMgr, &LangOpts, FileID, |
| 63 | SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), |
| 64 | Style))); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 65 | } |
| 66 | return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled); |
| 67 | } |
| 68 | |
| 69 | void IncludeInserter::AddInclude(StringRef file_name, bool IsAngled, |
| 70 | SourceLocation HashLocation, |
| 71 | SourceLocation end_location) { |
| 72 | FileID FileID = SourceMgr.getFileID(HashLocation); |
| 73 | if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { |
| 74 | IncludeSorterByFile.insert(std::make_pair( |
| 75 | FileID, llvm::make_unique<IncludeSorter>( |
| 76 | &SourceMgr, &LangOpts, FileID, |
| 77 | SourceMgr.getFilename(HashLocation), Style))); |
| 78 | } |
| 79 | IncludeSorterByFile[FileID]->AddInclude(file_name, IsAngled, HashLocation, |
| 80 | end_location); |
| 81 | } |
| 82 | |
| 83 | } // namespace tidy |
| 84 | } // namespace clang |