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 { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 15 | namespace utils { |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 16 | |
| 17 | class IncludeInserterCallback : public PPCallbacks { |
| 18 | public: |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 19 | explicit IncludeInserterCallback(IncludeInserter *Inserter) |
| 20 | : Inserter(Inserter) {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 21 | // Implements PPCallbacks::InclusionDerective(). Records the names and source |
| 22 | // locations of the inclusions in the main source file being processed. |
| 23 | void InclusionDirective(SourceLocation HashLocation, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 24 | const Token &IncludeToken, StringRef FileNameRef, |
| 25 | bool IsAngled, CharSourceRange FileNameRange, |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 26 | const FileEntry * /*IncludedFile*/, |
| 27 | StringRef /*SearchPath*/, StringRef /*RelativePath*/, |
Julie Hockett | 546943f | 2018-05-10 19:13:14 +0000 | [diff] [blame] | 28 | const Module * /*ImportedModule*/, |
| 29 | SrcMgr::CharacteristicKind /*FileType*/) override { |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 30 | Inserter->AddInclude(FileNameRef, IsAngled, HashLocation, |
Haojian Wu | 7d08ba2 | 2016-03-02 14:12:17 +0000 | [diff] [blame] | 31 | IncludeToken.getEndLoc()); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | private: |
Manuel Klimek | 795daa2 | 2015-08-11 12:02:28 +0000 | [diff] [blame] | 35 | IncludeInserter *Inserter; |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | IncludeInserter::IncludeInserter(const SourceManager &SourceMgr, |
| 39 | const LangOptions &LangOpts, |
| 40 | IncludeSorter::IncludeStyle Style) |
| 41 | : SourceMgr(SourceMgr), LangOpts(LangOpts), Style(Style) {} |
| 42 | |
David Blaikie | e04a3da | 2015-10-20 21:45:52 +0000 | [diff] [blame] | 43 | IncludeInserter::~IncludeInserter() {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 44 | |
| 45 | std::unique_ptr<PPCallbacks> IncludeInserter::CreatePPCallbacks() { |
| 46 | return llvm::make_unique<IncludeInserterCallback>(this); |
| 47 | } |
| 48 | |
| 49 | llvm::Optional<FixItHint> |
| 50 | IncludeInserter::CreateIncludeInsertion(FileID FileID, StringRef Header, |
| 51 | bool IsAngled) { |
| 52 | // We assume the same Header will never be included both angled and not |
| 53 | // angled. |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 54 | if (!InsertedHeaders[FileID].insert(Header).second) |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 55 | return llvm::None; |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 56 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 57 | if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 58 | // This may happen if there have been no preprocessor directives in this |
| 59 | // file. |
| 60 | IncludeSorterByFile.insert(std::make_pair( |
| 61 | FileID, |
| 62 | llvm::make_unique<IncludeSorter>( |
| 63 | &SourceMgr, &LangOpts, FileID, |
| 64 | SourceMgr.getFilename(SourceMgr.getLocForStartOfFile(FileID)), |
| 65 | Style))); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 66 | } |
| 67 | return IncludeSorterByFile[FileID]->CreateIncludeInsertion(Header, IsAngled); |
| 68 | } |
| 69 | |
Etienne Bergeron | 8d73de9 | 2016-05-16 14:34:20 +0000 | [diff] [blame] | 70 | void IncludeInserter::AddInclude(StringRef FileName, bool IsAngled, |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 71 | SourceLocation HashLocation, |
Etienne Bergeron | 8d73de9 | 2016-05-16 14:34:20 +0000 | [diff] [blame] | 72 | SourceLocation EndLocation) { |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 73 | FileID FileID = SourceMgr.getFileID(HashLocation); |
| 74 | if (IncludeSorterByFile.find(FileID) == IncludeSorterByFile.end()) { |
| 75 | IncludeSorterByFile.insert(std::make_pair( |
| 76 | FileID, llvm::make_unique<IncludeSorter>( |
| 77 | &SourceMgr, &LangOpts, FileID, |
| 78 | SourceMgr.getFilename(HashLocation), Style))); |
| 79 | } |
Etienne Bergeron | 8d73de9 | 2016-05-16 14:34:20 +0000 | [diff] [blame] | 80 | IncludeSorterByFile[FileID]->AddInclude(FileName, IsAngled, HashLocation, |
| 81 | EndLocation); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 84 | } // namespace utils |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 85 | } // namespace tidy |
| 86 | } // namespace clang |