Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 1 | //===--- ASTUnit.cpp - ASTUnit utility --------------------------*- C++ -*-===// |
Argyrios Kyrtzidis | 3a08ec1 | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 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 | // ASTUnit Implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
| 18 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeOrdering.h" |
| 20 | #include "clang/Basic/Diagnostic.h" |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 21 | #include "clang/Basic/MemoryBufferCache.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
| 23 | #include "clang/Basic/TargetOptions.h" |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 24 | #include "clang/Basic/VirtualFileSystem.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/CompilerInstance.h" |
| 26 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendOptions.h" |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/MultiplexConsumer.h" |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/Utils.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
Douglas Gregor | 1452ff1 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 33 | #include "clang/Lex/PreprocessorOptions.h" |
David Blaikie | 0a4e61f | 2013-09-13 18:32:52 +0000 | [diff] [blame] | 34 | #include "clang/Sema/Sema.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Serialization/ASTReader.h" |
| 36 | #include "clang/Serialization/ASTWriter.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/ArrayRef.h" |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 40a5a7d | 2010-08-16 23:08:34 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/StringSet.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CrashRecoveryContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Host.h" |
| 42 | #include "llvm/Support/MemoryBuffer.h" |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Mutex.h" |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MutexGuard.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Timer.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 47 | #include <atomic> |
Zhongxing Xu | 318e403 | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 48 | #include <cstdio> |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 49 | #include <cstdlib> |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 50 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 51 | using namespace clang; |
| 52 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 53 | using llvm::TimeRecord; |
| 54 | |
| 55 | namespace { |
| 56 | class SimpleTimer { |
| 57 | bool WantTiming; |
| 58 | TimeRecord Start; |
| 59 | std::string Output; |
| 60 | |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 61 | public: |
Douglas Gregor | 1cbdd95 | 2010-11-01 13:48:43 +0000 | [diff] [blame] | 62 | explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 63 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 64 | Start = TimeRecord::getCurrentTime(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | void setOutput(const Twine &Output) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 68 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 69 | this->Output = Output.str(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 72 | ~SimpleTimer() { |
| 73 | if (WantTiming) { |
| 74 | TimeRecord Elapsed = TimeRecord::getCurrentTime(); |
| 75 | Elapsed -= Start; |
| 76 | llvm::errs() << Output << ':'; |
| 77 | Elapsed.print(Elapsed, llvm::errs()); |
| 78 | llvm::errs() << '\n'; |
| 79 | } |
| 80 | } |
| 81 | }; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 82 | |
| 83 | struct OnDiskData { |
| 84 | /// \brief The file in which the precompiled preamble is stored. |
| 85 | std::string PreambleFile; |
| 86 | |
Rafael Espindola | bc7d949 | 2013-06-26 03:52:38 +0000 | [diff] [blame] | 87 | /// \brief Temporary files that should be removed when the ASTUnit is |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 88 | /// destroyed. |
Rafael Espindola | bc7d949 | 2013-06-26 03:52:38 +0000 | [diff] [blame] | 89 | SmallVector<std::string, 4> TemporaryFiles; |
| 90 | |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 91 | /// \brief Erase temporary files. |
| 92 | void CleanTemporaryFiles(); |
| 93 | |
| 94 | /// \brief Erase the preamble file. |
| 95 | void CleanPreambleFile(); |
| 96 | |
| 97 | /// \brief Erase temporary files and the preamble file. |
| 98 | void Cleanup(); |
| 99 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 100 | } |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 102 | static llvm::sys::SmartMutex<false> &getOnDiskMutex() { |
| 103 | static llvm::sys::SmartMutex<false> M(/* recursive = */ true); |
| 104 | return M; |
| 105 | } |
| 106 | |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 107 | static void cleanupOnDiskMapAtExit(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 108 | |
Dylan Noblesmith | cdd3151 | 2014-08-24 18:59:52 +0000 | [diff] [blame] | 109 | typedef llvm::DenseMap<const ASTUnit *, |
| 110 | std::unique_ptr<OnDiskData>> OnDiskDataMap; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 111 | static OnDiskDataMap &getOnDiskDataMap() { |
| 112 | static OnDiskDataMap M; |
| 113 | static bool hasRegisteredAtExit = false; |
| 114 | if (!hasRegisteredAtExit) { |
| 115 | hasRegisteredAtExit = true; |
| 116 | atexit(cleanupOnDiskMapAtExit); |
| 117 | } |
| 118 | return M; |
| 119 | } |
| 120 | |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 121 | static void cleanupOnDiskMapAtExit() { |
Argyrios Kyrtzidis | 4cf2ffe | 2012-07-03 16:30:52 +0000 | [diff] [blame] | 122 | // Use the mutex because there can be an alive thread destroying an ASTUnit. |
| 123 | llvm::MutexGuard Guard(getOnDiskMutex()); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 124 | for (const auto &I : getOnDiskDataMap()) { |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 125 | // We don't worry about freeing the memory associated with OnDiskDataMap. |
| 126 | // All we care about is erasing stale files. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 127 | I.second->Cleanup(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | static OnDiskData &getOnDiskData(const ASTUnit *AU) { |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 132 | // We require the mutex since we are modifying the structure of the |
| 133 | // DenseMap. |
| 134 | llvm::MutexGuard Guard(getOnDiskMutex()); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 135 | OnDiskDataMap &M = getOnDiskDataMap(); |
Dylan Noblesmith | cdd3151 | 2014-08-24 18:59:52 +0000 | [diff] [blame] | 136 | auto &D = M[AU]; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 137 | if (!D) |
Dylan Noblesmith | cdd3151 | 2014-08-24 18:59:52 +0000 | [diff] [blame] | 138 | D = llvm::make_unique<OnDiskData>(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 139 | return *D; |
| 140 | } |
| 141 | |
| 142 | static void erasePreambleFile(const ASTUnit *AU) { |
| 143 | getOnDiskData(AU).CleanPreambleFile(); |
| 144 | } |
| 145 | |
| 146 | static void removeOnDiskEntry(const ASTUnit *AU) { |
Ted Kremenek | bd307a5 | 2011-10-27 19:44:25 +0000 | [diff] [blame] | 147 | // We require the mutex since we are modifying the structure of the |
| 148 | // DenseMap. |
| 149 | llvm::MutexGuard Guard(getOnDiskMutex()); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 150 | OnDiskDataMap &M = getOnDiskDataMap(); |
| 151 | OnDiskDataMap::iterator I = M.find(AU); |
| 152 | if (I != M.end()) { |
| 153 | I->second->Cleanup(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 154 | M.erase(I); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 158 | static void setPreambleFile(const ASTUnit *AU, StringRef preambleFile) { |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 159 | getOnDiskData(AU).PreambleFile = preambleFile; |
| 160 | } |
| 161 | |
| 162 | static const std::string &getPreambleFile(const ASTUnit *AU) { |
| 163 | return getOnDiskData(AU).PreambleFile; |
| 164 | } |
| 165 | |
| 166 | void OnDiskData::CleanTemporaryFiles() { |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 167 | for (StringRef File : TemporaryFiles) |
| 168 | llvm::sys::fs::remove(File); |
Rafael Espindola | bc7d949 | 2013-06-26 03:52:38 +0000 | [diff] [blame] | 169 | TemporaryFiles.clear(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void OnDiskData::CleanPreambleFile() { |
| 173 | if (!PreambleFile.empty()) { |
Rafael Espindola | bc4aa55 | 2013-06-26 04:02:37 +0000 | [diff] [blame] | 174 | llvm::sys::fs::remove(PreambleFile); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 175 | PreambleFile.clear(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void OnDiskData::Cleanup() { |
| 180 | CleanTemporaryFiles(); |
| 181 | CleanPreambleFile(); |
| 182 | } |
| 183 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 184 | struct ASTUnit::ASTWriterData { |
| 185 | SmallString<128> Buffer; |
| 186 | llvm::BitstreamWriter Stream; |
| 187 | ASTWriter Writer; |
| 188 | |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 189 | ASTWriterData(MemoryBufferCache &PCMCache) |
| 190 | : Stream(Buffer), Writer(Stream, Buffer, PCMCache, {}) {} |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 193 | void ASTUnit::clearFileLevelDecls() { |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 194 | llvm::DeleteContainerSeconds(FileDecls); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 197 | void ASTUnit::CleanTemporaryFiles() { |
| 198 | getOnDiskData(this).CleanTemporaryFiles(); |
| 199 | } |
| 200 | |
Rafael Espindola | bc7d949 | 2013-06-26 03:52:38 +0000 | [diff] [blame] | 201 | void ASTUnit::addTemporaryFile(StringRef TempFile) { |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 202 | getOnDiskData(this).TemporaryFiles.push_back(TempFile); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 205 | /// \brief After failing to build a precompiled preamble (due to |
| 206 | /// errors in the source that occurs in the preamble), the number of |
| 207 | /// reparses during which we'll skip even trying to precompile the |
| 208 | /// preamble. |
| 209 | const unsigned DefaultPreambleRebuildInterval = 5; |
| 210 | |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 211 | /// \brief Tracks the number of ASTUnit objects that are currently active. |
| 212 | /// |
| 213 | /// Used for debugging purposes only. |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 214 | static std::atomic<unsigned> ActiveASTUnitObjects; |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 215 | |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 216 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 217 | : Reader(nullptr), HadModuleLoaderFatalFailure(false), |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 218 | OnlyLocalDecls(false), CaptureDiagnostics(false), |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 219 | MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 220 | TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")), |
Argyrios Kyrtzidis | 4954bc1 | 2011-03-05 01:03:48 +0000 | [diff] [blame] | 221 | OwnsRemappedFileBuffers(true), |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 222 | NumStoredDiagnosticsFromDriver(0), |
Rafael Espindola | 4674a87 | 2014-08-13 17:08:22 +0000 | [diff] [blame] | 223 | PreambleRebuildCounter(0), |
Rafael Espindola | fa49c0b | 2014-08-13 16:47:00 +0000 | [diff] [blame] | 224 | NumWarningsInPreamble(0), |
Douglas Gregor | 2c8bd47 | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 225 | ShouldCacheCodeCompletionResults(false), |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 226 | IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 227 | CompletionCacheTopLevelHashValue(0), |
| 228 | PreambleTopLevelHashValue(0), |
| 229 | CurrentTopLevelHashValue(0), |
Douglas Gregor | 4740c45 | 2010-08-19 00:45:44 +0000 | [diff] [blame] | 230 | UnsafeToFree(false) { |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 231 | if (getenv("LIBCLANG_OBJTRACKING")) |
| 232 | fprintf(stderr, "+++ %u translation units\n", ++ActiveASTUnitObjects); |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 233 | } |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 234 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 235 | ASTUnit::~ASTUnit() { |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 236 | // If we loaded from an AST file, balance out the BeginSourceFile call. |
| 237 | if (MainFileIsAST && getDiagnostics().getClient()) { |
| 238 | getDiagnostics().getClient()->EndSourceFile(); |
| 239 | } |
| 240 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 241 | clearFileLevelDecls(); |
| 242 | |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 243 | // Clean up the temporary files and the preamble file. |
| 244 | removeOnDiskEntry(this); |
| 245 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 246 | // Free the buffers associated with remapped files. We are required to |
| 247 | // perform this operation here because we explicitly request that the |
| 248 | // compiler instance *not* free these buffers for each invocation of the |
| 249 | // parser. |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 250 | if (Invocation && OwnsRemappedFileBuffers) { |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 251 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 252 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
| 253 | delete RB.second; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 254 | } |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 256 | ClearCachedCompletionResults(); |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 257 | |
Benjamin Kramer | 4527fb2 | 2014-03-02 17:08:31 +0000 | [diff] [blame] | 258 | if (getenv("LIBCLANG_OBJTRACKING")) |
| 259 | fprintf(stderr, "--- %u translation units\n", --ActiveASTUnitObjects); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 262 | void ASTUnit::setPreprocessor(std::shared_ptr<Preprocessor> PP) { |
| 263 | this->PP = std::move(PP); |
| 264 | } |
Argyrios Kyrtzidis | da6e054 | 2012-01-17 18:48:07 +0000 | [diff] [blame] | 265 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 266 | /// \brief Determine the set of code-completion contexts in which this |
| 267 | /// declaration should be shown. |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 268 | static unsigned getDeclShowContexts(const NamedDecl *ND, |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 269 | const LangOptions &LangOpts, |
| 270 | bool &IsNestedNameSpecifier) { |
| 271 | IsNestedNameSpecifier = false; |
| 272 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 273 | if (isa<UsingShadowDecl>(ND)) |
| 274 | ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 275 | if (!ND) |
| 276 | return 0; |
| 277 | |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 278 | uint64_t Contexts = 0; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 279 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
| 280 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { |
| 281 | // Types can appear in these contexts. |
| 282 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 283 | Contexts |= (1LL << CodeCompletionContext::CCC_TopLevel) |
| 284 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 285 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 286 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 287 | | (1LL << CodeCompletionContext::CCC_Type) |
| 288 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 289 | |
| 290 | // In C++, types can appear in expressions contexts (for functional casts). |
| 291 | if (LangOpts.CPlusPlus) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 292 | Contexts |= (1LL << CodeCompletionContext::CCC_Expression); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 293 | |
| 294 | // In Objective-C, message sends can send interfaces. In Objective-C++, |
| 295 | // all types are available due to functional casts. |
| 296 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 297 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 298 | |
| 299 | // In Objective-C, you can only be a subclass of another Objective-C class |
| 300 | if (isa<ObjCInterfaceDecl>(ND)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 301 | Contexts |= (1LL << CodeCompletionContext::CCC_ObjCInterfaceName); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 302 | |
| 303 | // Deal with tag names. |
| 304 | if (isa<EnumDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 305 | Contexts |= (1LL << CodeCompletionContext::CCC_EnumTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 307 | // Part of the nested-name-specifier in C++0x. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 308 | if (LangOpts.CPlusPlus11) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 309 | IsNestedNameSpecifier = true; |
Dmitri Gribenko | fe0483d | 2013-01-23 17:21:11 +0000 | [diff] [blame] | 310 | } else if (const RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 311 | if (Record->isUnion()) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 312 | Contexts |= (1LL << CodeCompletionContext::CCC_UnionTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 313 | else |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 314 | Contexts |= (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 316 | if (LangOpts.CPlusPlus) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 317 | IsNestedNameSpecifier = true; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 318 | } else if (isa<ClassTemplateDecl>(ND)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 319 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 320 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
| 321 | // Values can appear in these contexts. |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 322 | Contexts = (1LL << CodeCompletionContext::CCC_Statement) |
| 323 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 324 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 325 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 326 | } else if (isa<ObjCProtocolDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 327 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCProtocolName); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 328 | } else if (isa<ObjCCategoryDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 329 | Contexts = (1LL << CodeCompletionContext::CCC_ObjCCategoryName); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 330 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 331 | Contexts = (1LL << CodeCompletionContext::CCC_Namespace); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 332 | |
| 333 | // Part of the nested-name-specifier. |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 334 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return Contexts; |
| 338 | } |
| 339 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 340 | void ASTUnit::CacheCodeCompletionResults() { |
| 341 | if (!TheSema) |
| 342 | return; |
| 343 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 344 | SimpleTimer Timer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 345 | Timer.setOutput("Cache global code completions for " + getMainFileName()); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 346 | |
| 347 | // Clear out the previous results. |
| 348 | ClearCachedCompletionResults(); |
| 349 | |
| 350 | // Gather the set of global code completions. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 351 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 352 | SmallVector<Result, 8> Results; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 353 | CachedCompletionAllocator = std::make_shared<GlobalCodeCompletionAllocator>(); |
Argyrios Kyrtzidis | 2bafa00 | 2012-11-16 03:34:57 +0000 | [diff] [blame] | 354 | CodeCompletionTUInfo CCTUInfo(CachedCompletionAllocator); |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 355 | TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, |
Argyrios Kyrtzidis | 2bafa00 | 2012-11-16 03:34:57 +0000 | [diff] [blame] | 356 | CCTUInfo, Results); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 357 | |
| 358 | // Translate global code completions into cached completions. |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 359 | llvm::DenseMap<CanQualType, unsigned> CompletionTypes; |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 360 | CodeCompletionContext CCContext(CodeCompletionContext::CCC_TopLevel); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 361 | |
| 362 | for (Result &R : Results) { |
| 363 | switch (R.Kind) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 364 | case Result::RK_Declaration: { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 365 | bool IsNestedNameSpecifier = false; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 366 | CachedCodeCompletionResult CachedResult; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 367 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 368 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 369 | IncludeBriefCommentsInCodeCompletion); |
| 370 | CachedResult.ShowInContexts = getDeclShowContexts( |
| 371 | R.Declaration, Ctx->getLangOpts(), IsNestedNameSpecifier); |
| 372 | CachedResult.Priority = R.Priority; |
| 373 | CachedResult.Kind = R.CursorKind; |
| 374 | CachedResult.Availability = R.Availability; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 375 | |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 376 | // Keep track of the type of this completion in an ASTContext-agnostic |
| 377 | // way. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 378 | QualType UsageType = getDeclUsageType(*Ctx, R.Declaration); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 379 | if (UsageType.isNull()) { |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 380 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 381 | CachedResult.Type = 0; |
| 382 | } else { |
| 383 | CanQualType CanUsageType |
| 384 | = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); |
| 385 | CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); |
| 386 | |
| 387 | // Determine whether we have already seen this type. If so, we save |
| 388 | // ourselves the work of formatting the type string by using the |
| 389 | // temporary, CanQualType-based hash table to find the associated value. |
| 390 | unsigned &TypeValue = CompletionTypes[CanUsageType]; |
| 391 | if (TypeValue == 0) { |
| 392 | TypeValue = CompletionTypes.size(); |
| 393 | CachedCompletionTypes[QualType(CanUsageType).getAsString()] |
| 394 | = TypeValue; |
| 395 | } |
| 396 | |
| 397 | CachedResult.Type = TypeValue; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 398 | } |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 400 | CachedCompletionResults.push_back(CachedResult); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 401 | |
| 402 | /// Handle nested-name-specifiers in C++. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 403 | if (TheSema->Context.getLangOpts().CPlusPlus && IsNestedNameSpecifier && |
| 404 | !R.StartsNestedNameSpecifier) { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 405 | // The contexts in which a nested-name-specifier can appear in C++. |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 406 | uint64_t NNSContexts |
| 407 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 408 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 409 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 410 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 411 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 412 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 413 | | (1LL << CodeCompletionContext::CCC_EnumTag) |
| 414 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
| 415 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag) |
| 416 | | (1LL << CodeCompletionContext::CCC_Type) |
| 417 | | (1LL << CodeCompletionContext::CCC_PotentiallyQualifiedName) |
| 418 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 419 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 420 | if (isa<NamespaceDecl>(R.Declaration) || |
| 421 | isa<NamespaceAliasDecl>(R.Declaration)) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 422 | NNSContexts |= (1LL << CodeCompletionContext::CCC_Namespace); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 423 | |
| 424 | if (unsigned RemainingContexts |
| 425 | = NNSContexts & ~CachedResult.ShowInContexts) { |
| 426 | // If there any contexts where this completion can be a |
| 427 | // nested-name-specifier but isn't already an option, create a |
| 428 | // nested-name-specifier completion. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 429 | R.StartsNestedNameSpecifier = true; |
| 430 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 431 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 432 | IncludeBriefCommentsInCodeCompletion); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 433 | CachedResult.ShowInContexts = RemainingContexts; |
| 434 | CachedResult.Priority = CCP_NestedNameSpecifier; |
| 435 | CachedResult.TypeClass = STC_Void; |
| 436 | CachedResult.Type = 0; |
| 437 | CachedCompletionResults.push_back(CachedResult); |
| 438 | } |
| 439 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 440 | break; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 443 | case Result::RK_Keyword: |
| 444 | case Result::RK_Pattern: |
| 445 | // Ignore keywords and patterns; we don't care, since they are so |
| 446 | // easily regenerated. |
| 447 | break; |
| 448 | |
| 449 | case Result::RK_Macro: { |
| 450 | CachedCodeCompletionResult CachedResult; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 451 | CachedResult.Completion = R.CreateCodeCompletionString( |
Douglas Gregor | c3425b1 | 2015-07-07 06:20:19 +0000 | [diff] [blame] | 452 | *TheSema, CCContext, *CachedCompletionAllocator, CCTUInfo, |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 453 | IncludeBriefCommentsInCodeCompletion); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 454 | CachedResult.ShowInContexts |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 455 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 456 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
| 457 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
| 458 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 459 | | (1LL << CodeCompletionContext::CCC_ClassStructUnion) |
| 460 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 461 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 462 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 463 | | (1LL << CodeCompletionContext::CCC_MacroNameUse) |
| 464 | | (1LL << CodeCompletionContext::CCC_PreprocessorExpression) |
| 465 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 466 | | (1LL << CodeCompletionContext::CCC_OtherWithMacros); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 467 | |
| 468 | CachedResult.Priority = R.Priority; |
| 469 | CachedResult.Kind = R.CursorKind; |
| 470 | CachedResult.Availability = R.Availability; |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 471 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 472 | CachedResult.Type = 0; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 473 | CachedCompletionResults.push_back(CachedResult); |
| 474 | break; |
| 475 | } |
| 476 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 477 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 478 | |
| 479 | // Save the current top-level hash value. |
| 480 | CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | void ASTUnit::ClearCachedCompletionResults() { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 484 | CachedCompletionResults.clear(); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 485 | CachedCompletionTypes.clear(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 486 | CachedCompletionAllocator = nullptr; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 489 | namespace { |
| 490 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 491 | /// \brief Gathers information from ASTReader that will be used to initialize |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 492 | /// a Preprocessor. |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 493 | class ASTInfoCollector : public ASTReaderListener { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 494 | Preprocessor &PP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 495 | ASTContext &Context; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 496 | LangOptions &LangOpt; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 497 | std::shared_ptr<TargetOptions> &TargetOpts; |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 498 | IntrusiveRefCntPtr<TargetInfo> &Target; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 499 | unsigned &Counter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 501 | bool InitializedLanguage; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 502 | public: |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 503 | ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt, |
| 504 | std::shared_ptr<TargetOptions> &TargetOpts, |
| 505 | IntrusiveRefCntPtr<TargetInfo> &Target, unsigned &Counter) |
| 506 | : PP(PP), Context(Context), LangOpt(LangOpt), TargetOpts(TargetOpts), |
| 507 | Target(Target), Counter(Counter), InitializedLanguage(false) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 508 | |
Richard Smith | 1e2cf0d | 2014-10-31 02:28:58 +0000 | [diff] [blame] | 509 | bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain, |
| 510 | bool AllowCompatibleDifferences) override { |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 511 | if (InitializedLanguage) |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 512 | return false; |
| 513 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 514 | LangOpt = LangOpts; |
| 515 | InitializedLanguage = true; |
| 516 | |
| 517 | updated(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 518 | return false; |
| 519 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Chandler Carruth | 0d745bc | 2015-03-14 04:47:43 +0000 | [diff] [blame] | 521 | bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain, |
| 522 | bool AllowCompatibleDifferences) override { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 523 | // If we've already initialized the target, don't do it again. |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 524 | if (Target) |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 525 | return false; |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 526 | |
| 527 | this->TargetOpts = std::make_shared<TargetOptions>(TargetOpts); |
| 528 | Target = |
| 529 | TargetInfo::CreateTargetInfo(PP.getDiagnostics(), this->TargetOpts); |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 530 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 531 | updated(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 532 | return false; |
| 533 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 535 | void ReadCounter(const serialization::ModuleFile &M, |
| 536 | unsigned Value) override { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 537 | Counter = Value; |
| 538 | } |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 539 | |
| 540 | private: |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 541 | void updated() { |
| 542 | if (!Target || !InitializedLanguage) |
| 543 | return; |
| 544 | |
| 545 | // Inform the target of the language options. |
| 546 | // |
| 547 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 548 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 549 | Target->adjust(LangOpt); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 550 | |
| 551 | // Initialize the preprocessor. |
| 552 | PP.Initialize(*Target); |
| 553 | |
| 554 | // Initialize the ASTContext |
| 555 | Context.InitBuiltinTypes(*Target); |
Dmitri Gribenko | acf2e78 | 2013-02-22 14:21:27 +0000 | [diff] [blame] | 556 | |
| 557 | // We didn't have access to the comment options when the ASTContext was |
| 558 | // constructed, so register them now. |
| 559 | Context.getCommentCommandTraits().registerCommentOptions( |
| 560 | LangOpt.CommentOpts); |
Argyrios Kyrtzidis | 9e1fb56 | 2012-09-14 20:24:53 +0000 | [diff] [blame] | 561 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 564 | /// \brief Diagnostic consumer that saves each diagnostic it is given. |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 565 | class StoredDiagnosticConsumer : public DiagnosticConsumer { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 566 | SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 567 | SourceManager *SourceMgr; |
| 568 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 569 | public: |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 570 | explicit StoredDiagnosticConsumer( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 571 | SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 572 | : StoredDiags(StoredDiags), SourceMgr(nullptr) {} |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 573 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 574 | void BeginSourceFile(const LangOptions &LangOpts, |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 575 | const Preprocessor *PP = nullptr) override { |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 576 | if (PP) |
| 577 | SourceMgr = &PP->getSourceManager(); |
| 578 | } |
| 579 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 580 | void HandleDiagnostic(DiagnosticsEngine::Level Level, |
| 581 | const Diagnostic &Info) override; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 582 | }; |
| 583 | |
| 584 | /// \brief RAII object that optionally captures diagnostics, if |
| 585 | /// there is no diagnostic client to capture them already. |
| 586 | class CaptureDroppedDiagnostics { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 587 | DiagnosticsEngine &Diags; |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 588 | StoredDiagnosticConsumer Client; |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 589 | DiagnosticConsumer *PreviousClient; |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 590 | std::unique_ptr<DiagnosticConsumer> OwningPreviousClient; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 591 | |
| 592 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 593 | CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 594 | SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 595 | : Diags(Diags), Client(StoredDiags), PreviousClient(nullptr) |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 596 | { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 597 | if (RequestCapture || Diags.getClient() == nullptr) { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 598 | OwningPreviousClient = Diags.takeClient(); |
| 599 | PreviousClient = Diags.getClient(); |
| 600 | Diags.setClient(&Client, false); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 601 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | ~CaptureDroppedDiagnostics() { |
Alexander Kornienko | 41c247a | 2014-11-17 23:46:02 +0000 | [diff] [blame] | 605 | if (Diags.getClient() == &Client) |
| 606 | Diags.setClient(PreviousClient, !!OwningPreviousClient.release()); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 607 | } |
| 608 | }; |
| 609 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 610 | } // anonymous namespace |
| 611 | |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 612 | void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 613 | const Diagnostic &Info) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 614 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 615 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 617 | // Only record the diagnostic if it's part of the source manager we know |
| 618 | // about. This effectively drops diagnostics from modules we're building. |
| 619 | // FIXME: In the long run, ee don't want to drop source managers from modules. |
| 620 | if (!Info.hasSourceManager() || &Info.getSourceManager() == SourceMgr) |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 621 | StoredDiags.emplace_back(Level, Info); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Argyrios Kyrtzidis | a38cb20 | 2017-01-30 06:05:58 +0000 | [diff] [blame] | 624 | IntrusiveRefCntPtr<ASTReader> ASTUnit::getASTReader() const { |
| 625 | return Reader; |
| 626 | } |
| 627 | |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 628 | ASTMutationListener *ASTUnit::getASTMutationListener() { |
| 629 | if (WriterData) |
| 630 | return &WriterData->Writer; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 631 | return nullptr; |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 634 | ASTDeserializationListener *ASTUnit::getDeserializationListener() { |
| 635 | if (WriterData) |
| 636 | return &WriterData->Writer; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 637 | return nullptr; |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Rafael Espindola | 16e1ba1 | 2014-08-26 20:17:44 +0000 | [diff] [blame] | 640 | std::unique_ptr<llvm::MemoryBuffer> |
| 641 | ASTUnit::getBufferForFile(StringRef Filename, std::string *ErrorStr) { |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 642 | assert(FileMgr); |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 643 | auto Buffer = FileMgr->getBufferForFile(Filename); |
| 644 | if (Buffer) |
| 645 | return std::move(*Buffer); |
| 646 | if (ErrorStr) |
| 647 | *ErrorStr = Buffer.getError().message(); |
| 648 | return nullptr; |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 651 | /// \brief Configure the diagnostics object for use with ASTUnit. |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 652 | void ASTUnit::ConfigureDiags(IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 653 | ASTUnit &AST, bool CaptureDiagnostics) { |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 654 | assert(Diags.get() && "no DiagnosticsEngine was provided"); |
| 655 | if (CaptureDiagnostics) |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 656 | Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics)); |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 657 | } |
| 658 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 659 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromASTFile( |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 660 | const std::string &Filename, const PCHContainerReader &PCHContainerRdr, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 661 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 662 | const FileSystemOptions &FileSystemOpts, bool UseDebugInfo, |
| 663 | bool OnlyLocalDecls, ArrayRef<RemappedFile> RemappedFiles, |
| 664 | bool CaptureDiagnostics, bool AllowPCHWithCompilerErrors, |
| 665 | bool UserFilesAreVolatile) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 666 | std::unique_ptr<ASTUnit> AST(new ASTUnit(true)); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 667 | |
| 668 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 669 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 670 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 671 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 672 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 673 | DiagCleanup(Diags.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 674 | |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 675 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | 16bef85 | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 677 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 678 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 679 | AST->Diagnostics = Diags; |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 680 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = vfs::getRealFileSystem(); |
| 681 | AST->FileMgr = new FileManager(FileSystemOpts, VFS); |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 682 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 683 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 684 | AST->getFileManager(), |
| 685 | UserFilesAreVolatile); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 686 | AST->PCMCache = new MemoryBufferCache; |
David Blaikie | 9c28cb3 | 2017-01-06 01:04:46 +0000 | [diff] [blame] | 687 | AST->HSOpts = std::make_shared<HeaderSearchOptions>(); |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 688 | AST->HSOpts->ModuleFormat = PCHContainerRdr.getFormat(); |
Douglas Gregor | b85b9cc | 2012-10-24 16:19:39 +0000 | [diff] [blame] | 689 | AST->HeaderInfo.reset(new HeaderSearch(AST->HSOpts, |
Manuel Klimek | 1f76c4e | 2013-10-24 07:51:24 +0000 | [diff] [blame] | 690 | AST->getSourceManager(), |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 691 | AST->getDiagnostics(), |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 692 | AST->ASTFileLangOpts, |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 693 | /*Target=*/nullptr)); |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 694 | |
David Blaikie | e304168 | 2017-01-05 19:11:36 +0000 | [diff] [blame] | 695 | auto PPOpts = std::make_shared<PreprocessorOptions>(); |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 696 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 697 | for (const auto &RemappedFile : RemappedFiles) |
| 698 | PPOpts->addRemappedFile(RemappedFile.first, RemappedFile.second); |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 699 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 700 | // Gather Info for preprocessor construction later on. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 702 | HeaderSearch &HeaderInfo = *AST->HeaderInfo; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 703 | unsigned Counter; |
| 704 | |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 705 | AST->PP = std::make_shared<Preprocessor>( |
| 706 | std::move(PPOpts), AST->getDiagnostics(), AST->ASTFileLangOpts, |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 707 | AST->getSourceManager(), *AST->PCMCache, HeaderInfo, *AST, |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 708 | /*IILookup=*/nullptr, |
| 709 | /*OwnsHeaderSearch=*/false); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 710 | Preprocessor &PP = *AST->PP; |
| 711 | |
Alp Toker | 0804343 | 2014-05-03 03:46:04 +0000 | [diff] [blame] | 712 | AST->Ctx = new ASTContext(AST->ASTFileLangOpts, AST->getSourceManager(), |
| 713 | PP.getIdentifierTable(), PP.getSelectorTable(), |
| 714 | PP.getBuiltinInfo()); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 715 | ASTContext &Context = *AST->Ctx; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 716 | |
Argyrios Kyrtzidis | 945a819 | 2012-09-15 01:10:20 +0000 | [diff] [blame] | 717 | bool disableValid = false; |
| 718 | if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) |
| 719 | disableValid = true; |
Douglas Gregor | 6623e1f | 2015-11-03 18:33:07 +0000 | [diff] [blame] | 720 | AST->Reader = new ASTReader(PP, Context, PCHContainerRdr, { }, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 721 | /*isysroot=*/"", |
| 722 | /*DisableValidation=*/disableValid, |
| 723 | AllowPCHWithCompilerErrors); |
Ted Kremenek | 2159b8d | 2011-05-04 23:27:12 +0000 | [diff] [blame] | 724 | |
David Blaikie | 2721c32 | 2014-08-10 16:54:39 +0000 | [diff] [blame] | 725 | AST->Reader->setListener(llvm::make_unique<ASTInfoCollector>( |
| 726 | *AST->PP, Context, AST->ASTFileLangOpts, AST->TargetOpts, AST->Target, |
| 727 | Counter)); |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 728 | |
Argyrios Kyrtzidis | f0b4cd1 | 2015-03-03 08:04:19 +0000 | [diff] [blame] | 729 | // Attach the AST reader to the AST context as an external AST |
| 730 | // source, so that declarations will be deserialized from the |
| 731 | // AST file as needed. |
| 732 | // We need the external source to be set up before we read the AST, because |
| 733 | // eagerly-deserialized declarations may use it. |
| 734 | Context.setExternalSource(AST->Reader); |
| 735 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 736 | switch (AST->Reader->ReadAST(Filename, serialization::MK_MainFile, |
Argyrios Kyrtzidis | 2ec2936 | 2012-11-15 18:57:22 +0000 | [diff] [blame] | 737 | SourceLocation(), ASTReader::ARR_None)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 738 | case ASTReader::Success: |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 739 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 740 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 741 | case ASTReader::Failure: |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 742 | case ASTReader::Missing: |
Douglas Gregor | c9ad5fb | 2012-10-22 22:50:17 +0000 | [diff] [blame] | 743 | case ASTReader::OutOfDate: |
| 744 | case ASTReader::VersionMismatch: |
| 745 | case ASTReader::ConfigurationMismatch: |
| 746 | case ASTReader::HadErrors: |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 747 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 748 | return nullptr; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 749 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 751 | AST->OriginalSourceFile = AST->Reader->getOriginalSourceFile(); |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 752 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 753 | PP.setCounterValue(Counter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 755 | // Create an AST consumer, even though it isn't used. |
| 756 | AST->Consumer.reset(new ASTConsumer); |
| 757 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 758 | // Create a semantic analysis object and tell the AST reader about it. |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 759 | AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); |
| 760 | AST->TheSema->Initialize(); |
Argyrios Kyrtzidis | 1b7ed91 | 2014-02-27 04:11:59 +0000 | [diff] [blame] | 761 | AST->Reader->InitializeSema(*AST->TheSema); |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 763 | // Tell the diagnostic client that we have started a source file. |
| 764 | AST->getDiagnostics().getClient()->BeginSourceFile(Context.getLangOpts(),&PP); |
| 765 | |
David Blaikie | 6f7382d | 2014-08-10 19:08:04 +0000 | [diff] [blame] | 766 | return AST; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 767 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 768 | |
| 769 | namespace { |
| 770 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 771 | /// \brief Preprocessor callback class that updates a hash value with the names |
| 772 | /// of all macros that have been defined by the translation unit. |
| 773 | class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { |
| 774 | unsigned &Hash; |
| 775 | |
| 776 | public: |
| 777 | explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 778 | |
| 779 | void MacroDefined(const Token &MacroNameTok, |
| 780 | const MacroDirective *MD) override { |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 781 | Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash); |
| 782 | } |
| 783 | }; |
| 784 | |
| 785 | /// \brief Add the given declaration to the hash of all top-level entities. |
| 786 | void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { |
| 787 | if (!D) |
| 788 | return; |
| 789 | |
| 790 | DeclContext *DC = D->getDeclContext(); |
| 791 | if (!DC) |
| 792 | return; |
| 793 | |
| 794 | if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) |
| 795 | return; |
| 796 | |
| 797 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
Argyrios Kyrtzidis | ca5c7be | 2013-10-15 17:37:55 +0000 | [diff] [blame] | 798 | if (EnumDecl *EnumD = dyn_cast<EnumDecl>(D)) { |
| 799 | // For an unscoped enum include the enumerators in the hash since they |
| 800 | // enter the top-level namespace. |
| 801 | if (!EnumD->isScoped()) { |
Aaron Ballman | 23a6dcb | 2014-03-08 18:45:14 +0000 | [diff] [blame] | 802 | for (const auto *EI : EnumD->enumerators()) { |
| 803 | if (EI->getIdentifier()) |
| 804 | Hash = llvm::HashString(EI->getIdentifier()->getName(), Hash); |
Argyrios Kyrtzidis | ca5c7be | 2013-10-15 17:37:55 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 809 | if (ND->getIdentifier()) |
| 810 | Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash); |
| 811 | else if (DeclarationName Name = ND->getDeclName()) { |
| 812 | std::string NameStr = Name.getAsString(); |
| 813 | Hash = llvm::HashString(NameStr, Hash); |
| 814 | } |
| 815 | return; |
Argyrios Kyrtzidis | 48d88de | 2013-06-24 21:19:12 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | if (ImportDecl *ImportD = dyn_cast<ImportDecl>(D)) { |
| 819 | if (Module *Mod = ImportD->getImportedModule()) { |
| 820 | std::string ModName = Mod->getFullModuleName(); |
| 821 | Hash = llvm::HashString(ModName, Hash); |
| 822 | } |
| 823 | return; |
| 824 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 827 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 828 | ASTUnit &Unit; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 829 | unsigned &Hash; |
| 830 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 831 | public: |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 832 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) |
| 833 | : Unit(_Unit), Hash(Hash) { |
| 834 | Hash = 0; |
| 835 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 836 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 837 | void handleTopLevelDecl(Decl *D) { |
Argyrios Kyrtzidis | 516eec2 | 2011-11-16 02:35:10 +0000 | [diff] [blame] | 838 | if (!D) |
| 839 | return; |
| 840 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 841 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 842 | // reported as top-level declarations, even though their DeclContext |
| 843 | // is the containing ObjC @interface/@implementation. This is a |
| 844 | // fundamental problem in the parser right now. |
| 845 | if (isa<ObjCMethodDecl>(D)) |
| 846 | return; |
| 847 | |
| 848 | AddTopLevelDeclarationToHash(D, Hash); |
| 849 | Unit.addTopLevelDecl(D); |
| 850 | |
| 851 | handleFileLevelDecl(D); |
| 852 | } |
| 853 | |
| 854 | void handleFileLevelDecl(Decl *D) { |
| 855 | Unit.addFileLevelDecl(D); |
| 856 | if (NamespaceDecl *NSD = dyn_cast<NamespaceDecl>(D)) { |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 857 | for (auto *I : NSD->decls()) |
| 858 | handleFileLevelDecl(I); |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 859 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 860 | } |
Sebastian Redl | eaa4ade | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 861 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 862 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 863 | for (Decl *TopLevelDecl : D) |
| 864 | handleTopLevelDecl(TopLevelDecl); |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 865 | return true; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Sebastian Redl | eaa4ade | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 868 | // We're not interested in "interesting" decls. |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 869 | void HandleInterestingDecl(DeclGroupRef) override {} |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 870 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 871 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 872 | for (Decl *TopLevelDecl : D) |
| 873 | handleTopLevelDecl(TopLevelDecl); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 874 | } |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 875 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 876 | ASTMutationListener *GetASTMutationListener() override { |
Argyrios Kyrtzidis | 1c7455f | 2013-05-10 01:28:51 +0000 | [diff] [blame] | 877 | return Unit.getASTMutationListener(); |
| 878 | } |
| 879 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 880 | ASTDeserializationListener *GetASTDeserializationListener() override { |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 881 | return Unit.getDeserializationListener(); |
| 882 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 883 | }; |
| 884 | |
| 885 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 886 | public: |
| 887 | ASTUnit &Unit; |
| 888 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 889 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 890 | StringRef InFile) override { |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 891 | CI.getPreprocessor().addPPCallbacks( |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 892 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
| 893 | Unit.getCurrentTopLevelHashValue())); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 894 | return llvm::make_unique<TopLevelDeclTrackerConsumer>( |
| 895 | Unit, Unit.getCurrentTopLevelHashValue()); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | public: |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 899 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 900 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 901 | bool hasCodeCompletionSupport() const override { return false; } |
| 902 | TranslationUnitKind getTranslationUnitKind() override { |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 903 | return Unit.getTranslationUnitKind(); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 904 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 905 | }; |
| 906 | |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 907 | class PrecompilePreambleAction : public ASTFrontendAction { |
| 908 | ASTUnit &Unit; |
| 909 | bool HasEmittedPreamblePCH; |
| 910 | |
| 911 | public: |
| 912 | explicit PrecompilePreambleAction(ASTUnit &Unit) |
| 913 | : Unit(Unit), HasEmittedPreamblePCH(false) {} |
| 914 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 915 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 916 | StringRef InFile) override; |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 917 | bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; } |
| 918 | void setHasEmittedPreamblePCH() { HasEmittedPreamblePCH = true; } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 919 | bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); } |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 920 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 921 | bool hasCodeCompletionSupport() const override { return false; } |
| 922 | bool hasASTFileSupport() const override { return false; } |
| 923 | TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; } |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 924 | }; |
| 925 | |
Argyrios Kyrtzidis | 5733271 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 926 | class PrecompilePreambleConsumer : public PCHGenerator { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 927 | ASTUnit &Unit; |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 928 | unsigned &Hash; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 929 | std::vector<Decl *> TopLevelDecls; |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 930 | PrecompilePreambleAction *Action; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 931 | std::unique_ptr<raw_ostream> Out; |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 933 | public: |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 934 | PrecompilePreambleConsumer(ASTUnit &Unit, PrecompilePreambleAction *Action, |
| 935 | const Preprocessor &PP, StringRef isysroot, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 936 | std::unique_ptr<raw_ostream> Out) |
Richard Smith | bd97f35 | 2016-08-25 18:26:30 +0000 | [diff] [blame] | 937 | : PCHGenerator(PP, "", isysroot, std::make_shared<PCHBuffer>(), |
David Blaikie | 61137e1 | 2017-01-05 18:23:18 +0000 | [diff] [blame] | 938 | ArrayRef<std::shared_ptr<ModuleFileExtension>>(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 939 | /*AllowASTWithErrors=*/true), |
| 940 | Unit(Unit), Hash(Unit.getCurrentTopLevelHashValue()), Action(Action), |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 941 | Out(std::move(Out)) { |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 942 | Hash = 0; |
| 943 | } |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 944 | |
Benjamin Kramer | a401b9b | 2015-02-06 18:58:04 +0000 | [diff] [blame] | 945 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
| 946 | for (Decl *D : DG) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 947 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 948 | // reported as top-level declarations, even though their DeclContext |
| 949 | // is the containing ObjC @interface/@implementation. This is a |
| 950 | // fundamental problem in the parser right now. |
| 951 | if (isa<ObjCMethodDecl>(D)) |
| 952 | continue; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 953 | AddTopLevelDeclarationToHash(D, Hash); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 954 | TopLevelDecls.push_back(D); |
| 955 | } |
Argyrios Kyrtzidis | 841dd88 | 2011-11-18 00:26:59 +0000 | [diff] [blame] | 956 | return true; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 959 | void HandleTranslationUnit(ASTContext &Ctx) override { |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 960 | PCHGenerator::HandleTranslationUnit(Ctx); |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 961 | if (hasEmittedPCH()) { |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 962 | // Write the generated bitstream to "Out". |
| 963 | *Out << getPCH(); |
| 964 | // Make sure it hits disk now. |
| 965 | Out->flush(); |
| 966 | // Free the buffer. |
| 967 | llvm::SmallVector<char, 0> Empty; |
| 968 | getPCH() = std::move(Empty); |
| 969 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 970 | // Translate the top-level declarations we captured during |
| 971 | // parsing into declaration IDs in the precompiled |
| 972 | // preamble. This will allow us to deserialize those top-level |
| 973 | // declarations when requested. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 974 | for (Decl *D : TopLevelDecls) { |
Argyrios Kyrtzidis | acfbbd7 | 2013-08-07 21:17:33 +0000 | [diff] [blame] | 975 | // Invalid top-level decls may not have been serialized. |
| 976 | if (D->isInvalidDecl()) |
| 977 | continue; |
| 978 | Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D)); |
| 979 | } |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 980 | |
| 981 | Action->setHasEmittedPreamblePCH(); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | }; |
| 985 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 986 | } // anonymous namespace |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 987 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 988 | std::unique_ptr<ASTConsumer> |
| 989 | PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, |
| 990 | StringRef InFile) { |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 991 | std::string Sysroot; |
| 992 | std::string OutputFile; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 993 | std::unique_ptr<raw_ostream> OS = |
| 994 | GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, |
| 995 | OutputFile); |
Rafael Espindola | 47de149 | 2015-04-10 12:54:53 +0000 | [diff] [blame] | 996 | if (!OS) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 997 | return nullptr; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 998 | |
Benjamin Kramer | 65745dc | 2013-06-11 13:07:19 +0000 | [diff] [blame] | 999 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 1000 | Sysroot.clear(); |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 1001 | |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 1002 | CI.getPreprocessor().addPPCallbacks( |
| 1003 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
| 1004 | Unit.getCurrentTopLevelHashValue())); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1005 | return llvm::make_unique<PrecompilePreambleConsumer>( |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 1006 | Unit, this, CI.getPreprocessor(), Sysroot, std::move(OS)); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Benjamin Kramer | 1ce5d80 | 2013-05-05 12:39:28 +0000 | [diff] [blame] | 1009 | static bool isNonDriverDiag(const StoredDiagnostic &StoredDiag) { |
| 1010 | return StoredDiag.getLocation().isValid(); |
| 1011 | } |
| 1012 | |
| 1013 | static void |
| 1014 | checkAndRemoveNonDriverDiags(SmallVectorImpl<StoredDiagnostic> &StoredDiags) { |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1015 | // Get rid of stored diagnostics except the ones from the driver which do not |
| 1016 | // have a source location. |
Benjamin Kramer | 1ce5d80 | 2013-05-05 12:39:28 +0000 | [diff] [blame] | 1017 | StoredDiags.erase( |
| 1018 | std::remove_if(StoredDiags.begin(), StoredDiags.end(), isNonDriverDiag), |
| 1019 | StoredDiags.end()); |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | static void checkAndSanitizeDiags(SmallVectorImpl<StoredDiagnostic> & |
| 1023 | StoredDiagnostics, |
| 1024 | SourceManager &SM) { |
| 1025 | // The stored diagnostic has the old source manager in it; update |
| 1026 | // the locations to refer into the new source manager. Since we've |
| 1027 | // been careful to make sure that the source manager's state |
| 1028 | // before and after are identical, so that we can reuse the source |
| 1029 | // location itself. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1030 | for (StoredDiagnostic &SD : StoredDiagnostics) { |
| 1031 | if (SD.getLocation().isValid()) { |
| 1032 | FullSourceLoc Loc(SD.getLocation(), SM); |
| 1033 | SD.setLocation(Loc); |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1038 | /// Parse the source file into a translation unit using the given compiler |
| 1039 | /// invocation, replacing the current translation unit. |
| 1040 | /// |
| 1041 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 1042 | /// contain any translation-unit information, false otherwise. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1043 | bool ASTUnit::Parse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 1044 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer) { |
Rafael Espindola | 4674a87 | 2014-08-13 17:08:22 +0000 | [diff] [blame] | 1045 | SavedMainFileBuffer.reset(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1046 | |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1047 | if (!Invocation) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1048 | return true; |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1049 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1050 | // Create the compiler instance to use for building the AST. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1051 | std::unique_ptr<CompilerInstance> Clang( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1052 | new CompilerInstance(std::move(PCHContainerOps))); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1055 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1056 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1057 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1058 | Clang->setInvocation(std::make_shared<CompilerInvocation>(*Invocation)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1059 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1060 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1061 | // Set up diagnostics, capturing any diagnostics that would |
| 1062 | // otherwise be dropped. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1063 | Clang->setDiagnostics(&getDiagnostics()); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1064 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1065 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 1066 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 1067 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1068 | if (!Clang->hasTarget()) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1069 | return true; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1070 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1071 | // Inform the target of the language options. |
| 1072 | // |
| 1073 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1074 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 1075 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1076 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1077 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1078 | "Invocation must have exactly one source file!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1079 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1080 | "FIXME: AST inputs not yet supported here!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1081 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR && |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 1082 | "IR inputs not support here!"); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1083 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1084 | // Configure the various subsystems. |
Alp Toker | 269d840 | 2014-07-06 05:26:07 +0000 | [diff] [blame] | 1085 | LangOpts = Clang->getInvocation().LangOpts; |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1086 | FileSystemOpts = Clang->getFileSystemOpts(); |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1087 | if (!FileMgr) { |
| 1088 | Clang->createFileManager(); |
| 1089 | FileMgr = &Clang->getFileManager(); |
| 1090 | } |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1091 | SourceMgr = new SourceManager(getDiagnostics(), *FileMgr, |
| 1092 | UserFilesAreVolatile); |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 1093 | TheSema.reset(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1094 | Ctx = nullptr; |
| 1095 | PP = nullptr; |
| 1096 | Reader = nullptr; |
| 1097 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1098 | // Clear out old caches and data. |
| 1099 | TopLevelDecls.clear(); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 1100 | clearFileLevelDecls(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1101 | CleanTemporaryFiles(); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1103 | if (!OverrideMainBuffer) { |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1104 | checkAndRemoveNonDriverDiags(StoredDiagnostics); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1105 | TopLevelDeclsInPreamble.clear(); |
| 1106 | } |
| 1107 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1108 | // Create a file manager object to provide access to and cache the filesystem. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1109 | Clang->setFileManager(&getFileManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1110 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1111 | // Create the source manager. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1112 | Clang->setSourceManager(&getSourceManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1113 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1114 | // If the main file has been overridden due to the use of a preamble, |
| 1115 | // make that override happen and introduce the preamble. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1116 | PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1117 | if (OverrideMainBuffer) { |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1118 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, |
| 1119 | OverrideMainBuffer.get()); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1120 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 1121 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 1122 | = PreambleEndsAtStartOfLine; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 1123 | PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this); |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1124 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 1125 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1126 | // The stored diagnostic has the old source manager in it; update |
| 1127 | // the locations to refer into the new source manager. Since we've |
| 1128 | // been careful to make sure that the source manager's state |
| 1129 | // before and after are identical, so that we can reuse the source |
| 1130 | // location itself. |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1131 | checkAndSanitizeDiags(StoredDiagnostics, getSourceManager()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Keep track of the override buffer; |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1134 | SavedMainFileBuffer = std::move(OverrideMainBuffer); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1135 | } |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1136 | |
| 1137 | std::unique_ptr<TopLevelDeclTrackerAction> Act( |
| 1138 | new TopLevelDeclTrackerAction(*this)); |
| 1139 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1140 | // Recover resources if we crash before exiting this method. |
| 1141 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 1142 | ActCleanup(Act.get()); |
| 1143 | |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 1144 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1145 | goto error; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1146 | |
Richard Smith | 26b8f78 | 2016-03-25 21:46:44 +0000 | [diff] [blame] | 1147 | if (SavedMainFileBuffer) |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1148 | TranslateStoredDiagnostics(getFileManager(), getSourceManager(), |
| 1149 | PreambleDiagnostics, StoredDiagnostics); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1150 | |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1151 | if (!Act->Execute()) |
| 1152 | goto error; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1153 | |
| 1154 | transferASTDataFromCompilerInstance(*Clang); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1155 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 1156 | Act->EndSourceFile(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1157 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1158 | FailedParseDiagnostics.clear(); |
| 1159 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1160 | return false; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1161 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1162 | error: |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1163 | // Remove the overridden buffer we used for the preamble. |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1164 | SavedMainFileBuffer = nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1165 | |
| 1166 | // Keep the ownership of the data in the ASTUnit because the client may |
| 1167 | // want to see the diagnostics. |
| 1168 | transferASTDataFromCompilerInstance(*Clang); |
| 1169 | FailedParseDiagnostics.swap(StoredDiagnostics); |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1170 | StoredDiagnostics.clear(); |
Argyrios Kyrtzidis | 067cbfa | 2011-10-24 17:25:20 +0000 | [diff] [blame] | 1171 | NumStoredDiagnosticsFromDriver = 0; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1172 | return true; |
| 1173 | } |
| 1174 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1175 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 1176 | static std::string GetPreamblePCHPath() { |
Douglas Gregor | 250ab1d | 2010-09-11 18:05:19 +0000 | [diff] [blame] | 1177 | // FIXME: This is a hack so that we can override the preamble file during |
| 1178 | // crash-recovery testing, which is the only case where the preamble files |
Rafael Espindola | bc4aa55 | 2013-06-26 04:02:37 +0000 | [diff] [blame] | 1179 | // are not necessarily cleaned up. |
Douglas Gregor | 250ab1d | 2010-09-11 18:05:19 +0000 | [diff] [blame] | 1180 | const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); |
| 1181 | if (TmpFile) |
| 1182 | return TmpFile; |
Rafael Espindola | bc4aa55 | 2013-06-26 04:02:37 +0000 | [diff] [blame] | 1183 | |
| 1184 | SmallString<128> Path; |
Rafael Espindola | a36e78e | 2013-07-05 20:00:06 +0000 | [diff] [blame] | 1185 | llvm::sys::fs::createTemporaryFile("preamble", "pch", Path); |
Rafael Espindola | bc4aa55 | 2013-06-26 04:02:37 +0000 | [diff] [blame] | 1186 | |
| 1187 | return Path.str(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1190 | /// \brief Compute the preamble for the main file, providing the source buffer |
| 1191 | /// that corresponds to the main file along with a pair (bytes, start-of-line) |
| 1192 | /// that describes the preamble. |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1193 | ASTUnit::ComputedPreamble |
| 1194 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, unsigned MaxLines) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1195 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 1196 | PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1197 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1198 | // Try to determine if the main file has been remapped, either from the |
| 1199 | // command line (to another file) or directly through the compiler invocation |
| 1200 | // (to a memory buffer). |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1201 | llvm::MemoryBuffer *Buffer = nullptr; |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1202 | std::unique_ptr<llvm::MemoryBuffer> BufferOwner; |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 1203 | std::string MainFilePath(FrontendOpts.Inputs[0].getFile()); |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1204 | llvm::sys::fs::UniqueID MainFileID; |
Rafael Espindola | be3b12b0 | 2013-06-20 15:12:38 +0000 | [diff] [blame] | 1205 | if (!llvm::sys::fs::getUniqueID(MainFilePath, MainFileID)) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1206 | // Check whether there is a file-file remapping of the main file |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1207 | for (const auto &RF : PreprocessorOpts.RemappedFiles) { |
| 1208 | std::string MPath(RF.first); |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1209 | llvm::sys::fs::UniqueID MID; |
Rafael Espindola | be3b12b0 | 2013-06-20 15:12:38 +0000 | [diff] [blame] | 1210 | if (!llvm::sys::fs::getUniqueID(MPath, MID)) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 1211 | if (MainFileID == MID) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1212 | // We found a remapping. Try to load the resulting, remapped source. |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1213 | BufferOwner = getBufferForFile(RF.second); |
| 1214 | if (!BufferOwner) |
| 1215 | return ComputedPreamble(nullptr, nullptr, 0, true); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | // Check whether there is a file-buffer remapping. It supercedes the |
| 1221 | // file-file remapping. |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1222 | for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) { |
| 1223 | std::string MPath(RB.first); |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 1224 | llvm::sys::fs::UniqueID MID; |
Rafael Espindola | be3b12b0 | 2013-06-20 15:12:38 +0000 | [diff] [blame] | 1225 | if (!llvm::sys::fs::getUniqueID(MPath, MID)) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 1226 | if (MainFileID == MID) { |
| 1227 | // We found a remapping. |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1228 | BufferOwner.reset(); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1229 | Buffer = const_cast<llvm::MemoryBuffer *>(RB.second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1230 | } |
| 1231 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1232 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | // If the main source file was not remapped, load it now. |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1236 | if (!Buffer && !BufferOwner) { |
| 1237 | BufferOwner = getBufferForFile(FrontendOpts.Inputs[0].getFile()); |
| 1238 | if (!BufferOwner) |
| 1239 | return ComputedPreamble(nullptr, nullptr, 0, true); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1240 | } |
David Blaikie | 3d95d85 | 2014-08-11 22:08:06 +0000 | [diff] [blame] | 1241 | |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1242 | if (!Buffer) |
| 1243 | Buffer = BufferOwner.get(); |
| 1244 | auto Pre = Lexer::ComputePreamble(Buffer->getBuffer(), |
| 1245 | *Invocation.getLangOpts(), MaxLines); |
| 1246 | return ComputedPreamble(Buffer, std::move(BufferOwner), Pre.first, |
| 1247 | Pre.second); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1250 | ASTUnit::PreambleFileHash |
| 1251 | ASTUnit::PreambleFileHash::createForFile(off_t Size, time_t ModTime) { |
| 1252 | PreambleFileHash Result; |
| 1253 | Result.Size = Size; |
| 1254 | Result.ModTime = ModTime; |
Dmitri Gribenko | 3ec8ee7 | 2013-12-20 01:07:30 +0000 | [diff] [blame] | 1255 | memset(Result.MD5, 0, sizeof(Result.MD5)); |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1256 | return Result; |
| 1257 | } |
| 1258 | |
| 1259 | ASTUnit::PreambleFileHash ASTUnit::PreambleFileHash::createForMemoryBuffer( |
| 1260 | const llvm::MemoryBuffer *Buffer) { |
| 1261 | PreambleFileHash Result; |
| 1262 | Result.Size = Buffer->getBufferSize(); |
| 1263 | Result.ModTime = 0; |
| 1264 | |
| 1265 | llvm::MD5 MD5Ctx; |
| 1266 | MD5Ctx.update(Buffer->getBuffer().data()); |
| 1267 | MD5Ctx.final(Result.MD5); |
| 1268 | |
| 1269 | return Result; |
| 1270 | } |
| 1271 | |
| 1272 | namespace clang { |
| 1273 | bool operator==(const ASTUnit::PreambleFileHash &LHS, |
| 1274 | const ASTUnit::PreambleFileHash &RHS) { |
| 1275 | return LHS.Size == RHS.Size && LHS.ModTime == RHS.ModTime && |
Dmitri Gribenko | 3ec8ee7 | 2013-12-20 01:07:30 +0000 | [diff] [blame] | 1276 | memcmp(LHS.MD5, RHS.MD5, sizeof(LHS.MD5)) == 0; |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1277 | } |
| 1278 | } // namespace clang |
| 1279 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1280 | static std::pair<unsigned, unsigned> |
| 1281 | makeStandaloneRange(CharSourceRange Range, const SourceManager &SM, |
| 1282 | const LangOptions &LangOpts) { |
| 1283 | CharSourceRange FileRange = Lexer::makeFileCharRange(Range, SM, LangOpts); |
| 1284 | unsigned Offset = SM.getFileOffset(FileRange.getBegin()); |
| 1285 | unsigned EndOffset = SM.getFileOffset(FileRange.getEnd()); |
| 1286 | return std::make_pair(Offset, EndOffset); |
| 1287 | } |
| 1288 | |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1289 | static ASTUnit::StandaloneFixIt makeStandaloneFixIt(const SourceManager &SM, |
| 1290 | const LangOptions &LangOpts, |
| 1291 | const FixItHint &InFix) { |
| 1292 | ASTUnit::StandaloneFixIt OutFix; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1293 | OutFix.RemoveRange = makeStandaloneRange(InFix.RemoveRange, SM, LangOpts); |
| 1294 | OutFix.InsertFromRange = makeStandaloneRange(InFix.InsertFromRange, SM, |
| 1295 | LangOpts); |
| 1296 | OutFix.CodeToInsert = InFix.CodeToInsert; |
| 1297 | OutFix.BeforePreviousInsertions = InFix.BeforePreviousInsertions; |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1298 | return OutFix; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1301 | static ASTUnit::StandaloneDiagnostic |
| 1302 | makeStandaloneDiagnostic(const LangOptions &LangOpts, |
| 1303 | const StoredDiagnostic &InDiag) { |
| 1304 | ASTUnit::StandaloneDiagnostic OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1305 | OutDiag.ID = InDiag.getID(); |
| 1306 | OutDiag.Level = InDiag.getLevel(); |
| 1307 | OutDiag.Message = InDiag.getMessage(); |
| 1308 | OutDiag.LocOffset = 0; |
| 1309 | if (InDiag.getLocation().isInvalid()) |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1310 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1311 | const SourceManager &SM = InDiag.getLocation().getManager(); |
| 1312 | SourceLocation FileLoc = SM.getFileLoc(InDiag.getLocation()); |
| 1313 | OutDiag.Filename = SM.getFilename(FileLoc); |
| 1314 | if (OutDiag.Filename.empty()) |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1315 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1316 | OutDiag.LocOffset = SM.getFileOffset(FileLoc); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1317 | for (const CharSourceRange &Range : InDiag.getRanges()) |
| 1318 | OutDiag.Ranges.push_back(makeStandaloneRange(Range, SM, LangOpts)); |
| 1319 | for (const FixItHint &FixIt : InDiag.getFixIts()) |
| 1320 | OutDiag.FixIts.push_back(makeStandaloneFixIt(SM, LangOpts, FixIt)); |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1321 | |
| 1322 | return OutDiag; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1325 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 1326 | /// the source file. |
| 1327 | /// |
| 1328 | /// This routine will compute the preamble of the main source file. If a |
| 1329 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 1330 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 1331 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 1332 | /// this routine will determine if it is still valid and, if so, avoid |
| 1333 | /// rebuilding the precompiled preamble. |
| 1334 | /// |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1335 | /// \param AllowRebuild When true (the default), this routine is |
| 1336 | /// allowed to rebuild the precompiled preamble if it is found to be |
| 1337 | /// out-of-date. |
| 1338 | /// |
| 1339 | /// \param MaxLines When non-zero, the maximum number of lines that |
| 1340 | /// can occur within the preamble. |
| 1341 | /// |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1342 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 1343 | /// buffer that should be used in place of the main file when doing so. |
| 1344 | /// Otherwise, returns a NULL pointer. |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1345 | std::unique_ptr<llvm::MemoryBuffer> |
| 1346 | ASTUnit::getMainBufferWithPrecompiledPreamble( |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1347 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1348 | const CompilerInvocation &PreambleInvocationIn, bool AllowRebuild, |
| 1349 | unsigned MaxLines) { |
| 1350 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1351 | auto PreambleInvocation = |
| 1352 | std::make_shared<CompilerInvocation>(PreambleInvocationIn); |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1353 | FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1354 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1355 | = PreambleInvocation->getPreprocessorOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1356 | |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1357 | ComputedPreamble NewPreamble = ComputePreamble(*PreambleInvocation, MaxLines); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1358 | |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1359 | if (!NewPreamble.Size) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1360 | // We couldn't find a preamble in the main source. Clear out the current |
| 1361 | // preamble, if we have one. It's obviously no good any more. |
| 1362 | Preamble.clear(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 1363 | erasePreambleFile(this); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1364 | |
| 1365 | // The next time we actually see a preamble, precompile it. |
| 1366 | PreambleRebuildCounter = 1; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1367 | return nullptr; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | if (!Preamble.empty()) { |
| 1371 | // We've previously computed a preamble. Check whether we have the same |
| 1372 | // preamble now that we did before, and that there's enough space in |
| 1373 | // the main-file buffer within the precompiled preamble to fit the |
| 1374 | // new main file. |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1375 | if (Preamble.size() == NewPreamble.Size && |
| 1376 | PreambleEndsAtStartOfLine == NewPreamble.PreambleEndsAtStartOfLine && |
| 1377 | memcmp(Preamble.getBufferStart(), NewPreamble.Buffer->getBufferStart(), |
| 1378 | NewPreamble.Size) == 0) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1379 | // The preamble has not changed. We may be able to re-use the precompiled |
| 1380 | // preamble. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1382 | // Check that none of the files used by the preamble have changed. |
| 1383 | bool AnyFileChanged = false; |
| 1384 | |
| 1385 | // First, make a record of those files that have been overridden via |
| 1386 | // remapping or unsaved_files. |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1387 | std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1388 | for (const auto &R : PreprocessorOpts.RemappedFiles) { |
| 1389 | if (AnyFileChanged) |
| 1390 | break; |
| 1391 | |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 1392 | vfs::Status Status; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1393 | if (FileMgr->getNoncachedStatValue(R.second, Status)) { |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1394 | // If we can't stat the file we're remapping to, assume that something |
| 1395 | // horrible happened. |
| 1396 | AnyFileChanged = true; |
| 1397 | break; |
| 1398 | } |
Rafael Espindola | e4777f4 | 2013-07-29 18:22:23 +0000 | [diff] [blame] | 1399 | |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1400 | OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile( |
Pavel Labath | ac71c8e | 2016-11-09 10:52:22 +0000 | [diff] [blame] | 1401 | Status.getSize(), |
| 1402 | llvm::sys::toTimeT(Status.getLastModificationTime())); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1403 | } |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1404 | |
| 1405 | for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) { |
| 1406 | if (AnyFileChanged) |
| 1407 | break; |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1408 | |
| 1409 | vfs::Status Status; |
| 1410 | if (FileMgr->getNoncachedStatValue(RB.first, Status)) { |
| 1411 | AnyFileChanged = true; |
| 1412 | break; |
| 1413 | } |
| 1414 | |
| 1415 | OverriddenFiles[Status.getUniqueID()] = |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1416 | PreambleFileHash::createForMemoryBuffer(RB.second); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | // Check whether anything has changed. |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1420 | for (llvm::StringMap<PreambleFileHash>::iterator |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1421 | F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); |
| 1422 | !AnyFileChanged && F != FEnd; |
| 1423 | ++F) { |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1424 | vfs::Status Status; |
| 1425 | if (FileMgr->getNoncachedStatValue(F->first(), Status)) { |
| 1426 | // If we can't stat the file, assume that something horrible happened. |
| 1427 | AnyFileChanged = true; |
| 1428 | break; |
| 1429 | } |
| 1430 | |
| 1431 | std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden |
| 1432 | = OverriddenFiles.find(Status.getUniqueID()); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1433 | if (Overridden != OverriddenFiles.end()) { |
| 1434 | // This file was remapped; check whether the newly-mapped file |
| 1435 | // matches up with the previous mapping. |
| 1436 | if (Overridden->second != F->second) |
| 1437 | AnyFileChanged = true; |
| 1438 | continue; |
| 1439 | } |
| 1440 | |
| 1441 | // The file was not remapped; check whether it has changed on disk. |
Cameron Desrochers | 6fffec3 | 2016-05-17 14:34:53 +0000 | [diff] [blame] | 1442 | if (Status.getSize() != uint64_t(F->second.Size) || |
Pavel Labath | ac71c8e | 2016-11-09 10:52:22 +0000 | [diff] [blame] | 1443 | llvm::sys::toTimeT(Status.getLastModificationTime()) != |
| 1444 | F->second.ModTime) |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1445 | AnyFileChanged = true; |
| 1446 | } |
| 1447 | |
| 1448 | if (!AnyFileChanged) { |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1449 | // Okay! We can re-use the precompiled preamble. |
| 1450 | |
| 1451 | // Set the state of the diagnostic object to mimic its state |
| 1452 | // after parsing the preamble. |
| 1453 | getDiagnostics().Reset(); |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1454 | ProcessWarningOptions(getDiagnostics(), |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1455 | PreambleInvocation->getDiagnosticOpts()); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1456 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1457 | |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 1458 | return llvm::MemoryBuffer::getMemBufferCopy( |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1459 | NewPreamble.Buffer->getBuffer(), FrontendOpts.Inputs[0].getFile()); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1460 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1461 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1462 | |
| 1463 | // If we aren't allowed to rebuild the precompiled preamble, just |
| 1464 | // return now. |
| 1465 | if (!AllowRebuild) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1466 | return nullptr; |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1468 | // We can't reuse the previously-computed preamble. Build a new one. |
| 1469 | Preamble.clear(); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1470 | PreambleDiagnostics.clear(); |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 1471 | erasePreambleFile(this); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1472 | PreambleRebuildCounter = 1; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1473 | } else if (!AllowRebuild) { |
| 1474 | // We aren't allowed to rebuild the precompiled preamble; just |
| 1475 | // return now. |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1476 | return nullptr; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1477 | } |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1478 | |
| 1479 | // If the preamble rebuild counter > 1, it's because we previously |
| 1480 | // failed to build a preamble and we're not yet ready to try |
| 1481 | // again. Decrement the counter and return a failure. |
| 1482 | if (PreambleRebuildCounter > 1) { |
| 1483 | --PreambleRebuildCounter; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1484 | return nullptr; |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Douglas Gregor | e10f0e5 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1487 | // Create a temporary file for the precompiled preamble. In rare |
| 1488 | // circumstances, this can fail. |
| 1489 | std::string PreamblePCHPath = GetPreamblePCHPath(); |
| 1490 | if (PreamblePCHPath.empty()) { |
| 1491 | // Try again next time. |
| 1492 | PreambleRebuildCounter = 1; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1493 | return nullptr; |
Douglas Gregor | e10f0e5 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1496 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1497 | SimpleTimer PreambleTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1498 | PreambleTimer.setOutput("Precompiling preamble"); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1499 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1500 | // Save the preamble text for later; we'll need to compare against it for |
| 1501 | // subsequent reparses. |
Dmitri Gribenko | 40798d3 | 2013-12-19 23:25:59 +0000 | [diff] [blame] | 1502 | StringRef MainFilename = FrontendOpts.Inputs[0].getFile(); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1503 | Preamble.assign(FileMgr->getFile(MainFilename), |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1504 | NewPreamble.Buffer->getBufferStart(), |
| 1505 | NewPreamble.Buffer->getBufferStart() + NewPreamble.Size); |
| 1506 | PreambleEndsAtStartOfLine = NewPreamble.PreambleEndsAtStartOfLine; |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1507 | |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 1508 | PreambleBuffer = llvm::MemoryBuffer::getMemBufferCopy( |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1509 | NewPreamble.Buffer->getBuffer().slice(0, Preamble.size()), MainFilename); |
Rafael Espindola | a96bd56 | 2013-06-26 04:12:57 +0000 | [diff] [blame] | 1510 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1511 | // Remap the main source file to the preamble buffer. |
Rafael Espindola | a96bd56 | 2013-06-26 04:12:57 +0000 | [diff] [blame] | 1512 | StringRef MainFilePath = FrontendOpts.Inputs[0].getFile(); |
Rafael Espindola | fa49c0b | 2014-08-13 16:47:00 +0000 | [diff] [blame] | 1513 | PreprocessorOpts.addRemappedFile(MainFilePath, PreambleBuffer.get()); |
Rafael Espindola | a96bd56 | 2013-06-26 04:12:57 +0000 | [diff] [blame] | 1514 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1515 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 1516 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1517 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | e10f0e5 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1518 | FrontendOpts.OutputFile = PreamblePCHPath; |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1519 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 1520 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1521 | |
| 1522 | // Create the compiler instance to use for building the precompiled preamble. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1523 | std::unique_ptr<CompilerInstance> Clang( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1524 | new CompilerInstance(std::move(PCHContainerOps))); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1525 | |
| 1526 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1527 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1528 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1529 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1530 | Clang->setInvocation(std::move(PreambleInvocation)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1531 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1532 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1533 | // Set up diagnostics, capturing all of the diagnostics produced. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1534 | Clang->setDiagnostics(&getDiagnostics()); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1535 | |
| 1536 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 1537 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 1538 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1539 | if (!Clang->hasTarget()) { |
Rafael Espindola | f5e5bc4 | 2013-06-26 04:26:38 +0000 | [diff] [blame] | 1540 | llvm::sys::fs::remove(FrontendOpts.OutputFile); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1541 | Preamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1542 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1543 | PreprocessorOpts.RemappedFileBuffers.pop_back(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1544 | return nullptr; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | // Inform the target of the language options. |
| 1548 | // |
| 1549 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1550 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 1551 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1553 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1554 | "Invocation must have exactly one source file!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1555 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1556 | "FIXME: AST inputs not yet supported here!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1557 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1558 | "IR inputs not support here!"); |
| 1559 | |
| 1560 | // Clear out old caches and data. |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1561 | getDiagnostics().Reset(); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1562 | ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts()); |
Argyrios Kyrtzidis | 38bacf3 | 2012-02-01 19:54:02 +0000 | [diff] [blame] | 1563 | checkAndRemoveNonDriverDiags(StoredDiagnostics); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1564 | TopLevelDecls.clear(); |
| 1565 | TopLevelDeclsInPreamble.clear(); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1566 | PreambleDiagnostics.clear(); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1567 | |
| 1568 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = |
| 1569 | createVFSFromCompilerInvocation(Clang->getInvocation(), getDiagnostics()); |
| 1570 | if (!VFS) |
| 1571 | return nullptr; |
| 1572 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1573 | // Create a file manager object to provide access to and cache the filesystem. |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1574 | Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS)); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1575 | |
| 1576 | // Create the source manager. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1577 | Clang->setSourceManager(new SourceManager(getDiagnostics(), |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1578 | Clang->getFileManager())); |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1579 | |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1580 | auto PreambleDepCollector = std::make_shared<DependencyCollector>(); |
| 1581 | Clang->addDependencyCollector(PreambleDepCollector); |
| 1582 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1583 | std::unique_ptr<PrecompilePreambleAction> Act; |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 1584 | Act.reset(new PrecompilePreambleAction(*this)); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 1585 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
Rafael Espindola | f5e5bc4 | 2013-06-26 04:26:38 +0000 | [diff] [blame] | 1586 | llvm::sys::fs::remove(FrontendOpts.OutputFile); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1587 | Preamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1588 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1589 | PreprocessorOpts.RemappedFileBuffers.pop_back(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1590 | return nullptr; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | Act->Execute(); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1594 | |
| 1595 | // Transfer any diagnostics generated when parsing the preamble into the set |
| 1596 | // of preamble diagnostics. |
Benjamin Kramer | 1a6e0a9 | 2014-10-03 18:52:54 +0000 | [diff] [blame] | 1597 | for (stored_diag_iterator I = stored_diag_afterDriver_begin(), |
| 1598 | E = stored_diag_end(); |
| 1599 | I != E; ++I) |
| 1600 | PreambleDiagnostics.push_back( |
| 1601 | makeStandaloneDiagnostic(Clang->getLangOpts(), *I)); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1602 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1603 | Act->EndSourceFile(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1604 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 1605 | checkAndRemoveNonDriverDiags(StoredDiagnostics); |
| 1606 | |
Argyrios Kyrtzidis | f0168de | 2013-06-11 00:36:55 +0000 | [diff] [blame] | 1607 | if (!Act->hasEmittedPreamblePCH()) { |
Argyrios Kyrtzidis | d6f5722 | 2013-06-11 16:42:34 +0000 | [diff] [blame] | 1608 | // The preamble PCH failed (e.g. there was a module loading fatal error), |
| 1609 | // so no precompiled header was generated. Forget that we even tried. |
Douglas Gregor | a6f74e2 | 2010-09-27 16:43:25 +0000 | [diff] [blame] | 1610 | // FIXME: Should we leave a note for ourselves to try again? |
Rafael Espindola | f5e5bc4 | 2013-06-26 04:26:38 +0000 | [diff] [blame] | 1611 | llvm::sys::fs::remove(FrontendOpts.OutputFile); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1612 | Preamble.clear(); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1613 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1614 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1615 | PreprocessorOpts.RemappedFileBuffers.pop_back(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1616 | return nullptr; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | // Keep track of the preamble we precompiled. |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 1620 | setPreambleFile(this, FrontendOpts.OutputFile); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1621 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1622 | |
| 1623 | // Keep track of all of the files that the source manager knows about, |
| 1624 | // so we can verify whether they have changed or not. |
| 1625 | FilesInPreamble.clear(); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1626 | SourceManager &SourceMgr = Clang->getSourceManager(); |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1627 | for (auto &Filename : PreambleDepCollector->getDependencies()) { |
| 1628 | const FileEntry *File = Clang->getFileManager().getFile(Filename); |
| 1629 | if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())) |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1630 | continue; |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1631 | if (time_t ModTime = File->getModificationTime()) { |
| 1632 | FilesInPreamble[File->getName()] = PreambleFileHash::createForFile( |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1633 | File->getSize(), ModTime); |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1634 | } else { |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1635 | llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File); |
Dmitri Gribenko | 4765252 | 2013-12-20 00:16:25 +0000 | [diff] [blame] | 1636 | FilesInPreamble[File->getName()] = |
| 1637 | PreambleFileHash::createForMemoryBuffer(Buffer); |
| 1638 | } |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1639 | } |
Ben Langmuir | 33c8090 | 2014-06-30 20:04:14 +0000 | [diff] [blame] | 1640 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1641 | PreambleRebuildCounter = 1; |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 1642 | PreprocessorOpts.RemappedFileBuffers.pop_back(); |
| 1643 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 1644 | // If the hash of top-level entities differs from the hash of the top-level |
| 1645 | // entities the last time we rebuilt the preamble, clear out the completion |
| 1646 | // cache. |
| 1647 | if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) { |
| 1648 | CompletionCacheTopLevelHashValue = 0; |
| 1649 | PreambleTopLevelHashValue = CurrentTopLevelHashValue; |
| 1650 | } |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 1651 | |
David Blaikie | d6902a1 | 2014-08-29 06:34:53 +0000 | [diff] [blame] | 1652 | return llvm::MemoryBuffer::getMemBufferCopy(NewPreamble.Buffer->getBuffer(), |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 1653 | MainFilename); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1654 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1656 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
| 1657 | std::vector<Decl *> Resolved; |
| 1658 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 1659 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1660 | for (serialization::DeclID TopLevelDecl : TopLevelDeclsInPreamble) { |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1661 | // Resolve the declaration ID to an actual declaration, possibly |
| 1662 | // deserializing the declaration in the process. |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1663 | if (Decl *D = Source.GetExternalDecl(TopLevelDecl)) |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1664 | Resolved.push_back(D); |
| 1665 | } |
| 1666 | TopLevelDeclsInPreamble.clear(); |
| 1667 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 1668 | } |
| 1669 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1670 | void ASTUnit::transferASTDataFromCompilerInstance(CompilerInstance &CI) { |
Ben Langmuir | 749323f | 2014-04-22 17:40:12 +0000 | [diff] [blame] | 1671 | // Steal the created target, context, and preprocessor if they have been |
| 1672 | // created. |
| 1673 | assert(CI.hasInvocation() && "missing invocation"); |
Alp Toker | 269d840 | 2014-07-06 05:26:07 +0000 | [diff] [blame] | 1674 | LangOpts = CI.getInvocation().LangOpts; |
David Blaikie | ec99b5e | 2014-08-10 19:14:48 +0000 | [diff] [blame] | 1675 | TheSema = CI.takeSema(); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1676 | Consumer = CI.takeASTConsumer(); |
Ben Langmuir | 532fdc0 | 2014-04-18 20:39:48 +0000 | [diff] [blame] | 1677 | if (CI.hasASTContext()) |
| 1678 | Ctx = &CI.getASTContext(); |
| 1679 | if (CI.hasPreprocessor()) |
David Blaikie | 4156546 | 2017-01-05 19:48:07 +0000 | [diff] [blame] | 1680 | PP = CI.getPreprocessorPtr(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1681 | CI.setSourceManager(nullptr); |
| 1682 | CI.setFileManager(nullptr); |
Ben Langmuir | 532fdc0 | 2014-04-18 20:39:48 +0000 | [diff] [blame] | 1683 | if (CI.hasTarget()) |
| 1684 | Target = &CI.getTarget(); |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1685 | Reader = CI.getModuleManager(); |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 1686 | HadModuleLoaderFatalFailure = CI.hadModuleLoaderFatalFailure(); |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1689 | StringRef ASTUnit::getMainFileName() const { |
Argyrios Kyrtzidis | 928e1fd | 2013-01-11 22:11:14 +0000 | [diff] [blame] | 1690 | if (Invocation && !Invocation->getFrontendOpts().Inputs.empty()) { |
| 1691 | const FrontendInputFile &Input = Invocation->getFrontendOpts().Inputs[0]; |
| 1692 | if (Input.isFile()) |
| 1693 | return Input.getFile(); |
| 1694 | else |
| 1695 | return Input.getBuffer()->getBufferIdentifier(); |
| 1696 | } |
| 1697 | |
| 1698 | if (SourceMgr) { |
| 1699 | if (const FileEntry * |
| 1700 | FE = SourceMgr->getFileEntryForID(SourceMgr->getMainFileID())) |
| 1701 | return FE->getName(); |
| 1702 | } |
| 1703 | |
| 1704 | return StringRef(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Argyrios Kyrtzidis | 37f2ab4 | 2013-03-05 20:21:14 +0000 | [diff] [blame] | 1707 | StringRef ASTUnit::getASTFileName() const { |
| 1708 | if (!isMainFileAST()) |
| 1709 | return StringRef(); |
| 1710 | |
| 1711 | serialization::ModuleFile & |
| 1712 | Mod = Reader->getModuleManager().getPrimaryModule(); |
| 1713 | return Mod.FileName; |
| 1714 | } |
| 1715 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1716 | std::unique_ptr<ASTUnit> |
| 1717 | ASTUnit::create(std::shared_ptr<CompilerInvocation> CI, |
| 1718 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
| 1719 | bool CaptureDiagnostics, bool UserFilesAreVolatile) { |
| 1720 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1721 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1722 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = |
| 1723 | createVFSFromCompilerInvocation(*CI, *Diags); |
| 1724 | if (!VFS) |
| 1725 | return nullptr; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1726 | AST->Diagnostics = Diags; |
| 1727 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
| 1728 | AST->Invocation = std::move(CI); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1729 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1730 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
| 1731 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr, |
| 1732 | UserFilesAreVolatile); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 1733 | AST->PCMCache = new MemoryBufferCache; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1734 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1735 | return AST; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1738 | ASTUnit *ASTUnit::LoadFromCompilerInvocationAction( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1739 | std::shared_ptr<CompilerInvocation> CI, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1740 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Argyrios Kyrtzidis | c382abf | 2016-02-09 19:07:13 +0000 | [diff] [blame] | 1741 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FrontendAction *Action, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1742 | ASTUnit *Unit, bool Persistent, StringRef ResourceFilesPath, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1743 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1744 | unsigned PrecompilePreambleAfterNParses, bool CacheCodeCompletionResults, |
| 1745 | bool IncludeBriefCommentsInCodeCompletion, bool UserFilesAreVolatile, |
| 1746 | std::unique_ptr<ASTUnit> *ErrAST) { |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1747 | assert(CI && "A CompilerInvocation is required"); |
| 1748 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1749 | std::unique_ptr<ASTUnit> OwnAST; |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1750 | ASTUnit *AST = Unit; |
| 1751 | if (!AST) { |
| 1752 | // Create the AST unit. |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1753 | OwnAST = create(CI, Diags, CaptureDiagnostics, UserFilesAreVolatile); |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1754 | AST = OwnAST.get(); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1755 | if (!AST) |
| 1756 | return nullptr; |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1759 | if (!ResourceFilesPath.empty()) { |
| 1760 | // Override the resources path. |
| 1761 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
| 1762 | } |
| 1763 | AST->OnlyLocalDecls = OnlyLocalDecls; |
| 1764 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1765 | if (PrecompilePreambleAfterNParses > 0) |
| 1766 | AST->PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1767 | AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1768 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1769 | AST->IncludeBriefCommentsInCodeCompletion |
| 1770 | = IncludeBriefCommentsInCodeCompletion; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1771 | |
| 1772 | // Recover resources if we crash before exiting this method. |
| 1773 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1774 | ASTUnitCleanup(OwnAST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1775 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1776 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1777 | DiagCleanup(Diags.get()); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1778 | |
| 1779 | // We'll manage file buffers ourselves. |
| 1780 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1781 | CI->getFrontendOpts().DisableFree = false; |
| 1782 | ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); |
| 1783 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1784 | // Create the compiler instance to use for building the AST. |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1785 | std::unique_ptr<CompilerInstance> Clang( |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1786 | new CompilerInstance(std::move(PCHContainerOps))); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Recover resources if we crash before exiting this method. |
| 1789 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1790 | CICleanup(Clang.get()); |
| 1791 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1792 | Clang->setInvocation(std::move(CI)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1793 | AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1794 | |
| 1795 | // Set up diagnostics, capturing any diagnostics that would |
| 1796 | // otherwise be dropped. |
| 1797 | Clang->setDiagnostics(&AST->getDiagnostics()); |
| 1798 | |
| 1799 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 1800 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 1801 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1802 | if (!Clang->hasTarget()) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1803 | return nullptr; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1804 | |
| 1805 | // Inform the target of the language options. |
| 1806 | // |
| 1807 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1808 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 1809 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1810 | |
| 1811 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
| 1812 | "Invocation must have exactly one source file!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1813 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST && |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1814 | "FIXME: AST inputs not yet supported here!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 1815 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR && |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1816 | "IR inputs not supported here!"); |
| 1817 | |
| 1818 | // Configure the various subsystems. |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1819 | AST->TheSema.reset(); |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1820 | AST->Ctx = nullptr; |
| 1821 | AST->PP = nullptr; |
| 1822 | AST->Reader = nullptr; |
| 1823 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1824 | // Create a file manager object to provide access to and cache the filesystem. |
| 1825 | Clang->setFileManager(&AST->getFileManager()); |
| 1826 | |
| 1827 | // Create the source manager. |
| 1828 | Clang->setSourceManager(&AST->getSourceManager()); |
| 1829 | |
Argyrios Kyrtzidis | c382abf | 2016-02-09 19:07:13 +0000 | [diff] [blame] | 1830 | FrontendAction *Act = Action; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1831 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1832 | std::unique_ptr<TopLevelDeclTrackerAction> TrackerAct; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1833 | if (!Act) { |
| 1834 | TrackerAct.reset(new TopLevelDeclTrackerAction(*AST)); |
| 1835 | Act = TrackerAct.get(); |
| 1836 | } |
| 1837 | |
| 1838 | // Recover resources if we crash before exiting this method. |
| 1839 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 1840 | ActCleanup(TrackerAct.get()); |
| 1841 | |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1842 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
| 1843 | AST->transferASTDataFromCompilerInstance(*Clang); |
| 1844 | if (OwnAST && ErrAST) |
| 1845 | ErrAST->swap(OwnAST); |
| 1846 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1847 | return nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1848 | } |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1849 | |
| 1850 | if (Persistent && !TrackerAct) { |
| 1851 | Clang->getPreprocessor().addPPCallbacks( |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 1852 | llvm::make_unique<MacroDefinitionTrackerPPCallbacks>( |
| 1853 | AST->getCurrentTopLevelHashValue())); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1854 | std::vector<std::unique_ptr<ASTConsumer>> Consumers; |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1855 | if (Clang->hasASTConsumer()) |
| 1856 | Consumers.push_back(Clang->takeASTConsumer()); |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 1857 | Consumers.push_back(llvm::make_unique<TopLevelDeclTrackerConsumer>( |
| 1858 | *AST, AST->getCurrentTopLevelHashValue())); |
| 1859 | Clang->setASTConsumer( |
| 1860 | llvm::make_unique<MultiplexConsumer>(std::move(Consumers))); |
Argyrios Kyrtzidis | b11f5a4 | 2011-11-28 04:56:00 +0000 | [diff] [blame] | 1861 | } |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1862 | if (!Act->Execute()) { |
| 1863 | AST->transferASTDataFromCompilerInstance(*Clang); |
| 1864 | if (OwnAST && ErrAST) |
| 1865 | ErrAST->swap(OwnAST); |
| 1866 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1867 | return nullptr; |
Argyrios Kyrtzidis | 1416e17 | 2012-06-08 05:48:06 +0000 | [diff] [blame] | 1868 | } |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1869 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1870 | // Steal the created target, context, and preprocessor. |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 1871 | AST->transferASTDataFromCompilerInstance(*Clang); |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1872 | |
| 1873 | Act->EndSourceFile(); |
| 1874 | |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1875 | if (OwnAST) |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1876 | return OwnAST.release(); |
Argyrios Kyrtzidis | 1ac5da1 | 2011-10-14 21:22:05 +0000 | [diff] [blame] | 1877 | else |
| 1878 | return AST; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1881 | bool ASTUnit::LoadFromCompilerInvocation( |
| 1882 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1883 | unsigned PrecompilePreambleAfterNParses) { |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1884 | if (!Invocation) |
| 1885 | return true; |
| 1886 | |
| 1887 | // We'll manage file buffers ourselves. |
| 1888 | Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1889 | Invocation->getFrontendOpts().DisableFree = false; |
Benjamin Kramer | 8de9c9b | 2017-01-18 16:25:48 +0000 | [diff] [blame] | 1890 | getDiagnostics().Reset(); |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1891 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1892 | |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1893 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1894 | if (PrecompilePreambleAfterNParses > 0) { |
| 1895 | PreambleRebuildCounter = PrecompilePreambleAfterNParses; |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1896 | OverrideMainBuffer = |
| 1897 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation); |
Benjamin Kramer | 8484a32 | 2017-02-13 16:16:43 +0000 | [diff] [blame] | 1898 | getDiagnostics().Reset(); |
| 1899 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1902 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1903 | ParsingTimer.setOutput("Parsing " + getMainFileName()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1904 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1905 | // Recover resources if we crash before exiting this method. |
| 1906 | llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer> |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 1907 | MemBufferCleanup(OverrideMainBuffer.get()); |
| 1908 | |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1909 | return Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer)); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1912 | std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1913 | std::shared_ptr<CompilerInvocation> CI, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1914 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1915 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, FileManager *FileMgr, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1916 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1917 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
| 1918 | bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, |
| 1919 | bool UserFilesAreVolatile) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1920 | // Create the AST unit. |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1921 | std::unique_ptr<ASTUnit> AST(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1922 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1923 | AST->Diagnostics = Diags; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1924 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1925 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1926 | AST->TUKind = TUKind; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1927 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 1928 | AST->IncludeBriefCommentsInCodeCompletion |
| 1929 | = IncludeBriefCommentsInCodeCompletion; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1930 | AST->Invocation = std::move(CI); |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 1931 | AST->FileSystemOpts = FileMgr->getFileSystemOpts(); |
| 1932 | AST->FileMgr = FileMgr; |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 1933 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1934 | |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1935 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1936 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1937 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1938 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1939 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Alp Toker | f994cef | 2014-07-05 03:08:06 +0000 | [diff] [blame] | 1940 | DiagCleanup(Diags.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1941 | |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 1942 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1943 | PrecompilePreambleAfterNParses)) |
David Blaikie | 103a2de | 2014-04-25 17:01:33 +0000 | [diff] [blame] | 1944 | return nullptr; |
| 1945 | return AST; |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1946 | } |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1947 | |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1948 | ASTUnit *ASTUnit::LoadFromCommandLine( |
| 1949 | const char **ArgBegin, const char **ArgEnd, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 1950 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1951 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags, StringRef ResourceFilesPath, |
| 1952 | bool OnlyLocalDecls, bool CaptureDiagnostics, |
| 1953 | ArrayRef<RemappedFile> RemappedFiles, bool RemappedFilesKeepOriginalName, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1954 | unsigned PrecompilePreambleAfterNParses, TranslationUnitKind TUKind, |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1955 | bool CacheCodeCompletionResults, bool IncludeBriefCommentsInCodeCompletion, |
| 1956 | bool AllowPCHWithCompilerErrors, bool SkipFunctionBodies, |
| 1957 | bool UserFilesAreVolatile, bool ForSerialization, |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 1958 | llvm::Optional<StringRef> ModuleFormat, std::unique_ptr<ASTUnit> *ErrAST) { |
Justin Bogner | d512c1e | 2014-10-15 00:33:06 +0000 | [diff] [blame] | 1959 | assert(Diags.get() && "no DiagnosticsEngine was provided"); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1960 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1961 | SmallVector<StoredDiagnostic, 4> StoredDiagnostics; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1962 | |
| 1963 | std::shared_ptr<CompilerInvocation> CI; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1965 | { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1967 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1968 | StoredDiagnostics); |
Daniel Dunbar | fcf2d42 | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1969 | |
Argyrios Kyrtzidis | 5cf423e | 2011-04-04 23:11:45 +0000 | [diff] [blame] | 1970 | CI = clang::createInvocationFromCommandLine( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 1971 | llvm::makeArrayRef(ArgBegin, ArgEnd), Diags); |
Argyrios Kyrtzidis | f606b82 | 2011-04-04 21:38:51 +0000 | [diff] [blame] | 1972 | if (!CI) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 1973 | return nullptr; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1974 | } |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1976 | // Override any files that need remapping |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 1977 | for (const auto &RemappedFile : RemappedFiles) { |
| 1978 | CI->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
| 1979 | RemappedFile.second); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1980 | } |
Argyrios Kyrtzidis | 4a280ff | 2012-03-07 01:51:17 +0000 | [diff] [blame] | 1981 | PreprocessorOptions &PPOpts = CI->getPreprocessorOpts(); |
| 1982 | PPOpts.RemappedFilesKeepOriginalName = RemappedFilesKeepOriginalName; |
| 1983 | PPOpts.AllowPCHWithCompilerErrors = AllowPCHWithCompilerErrors; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1984 | |
Daniel Dunbar | a5a166d | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1985 | // Override the resources path. |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1986 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1987 | |
Erik Verbruggen | 6e92251 | 2012-04-12 10:11:59 +0000 | [diff] [blame] | 1988 | CI->getFrontendOpts().SkipFunctionBodies = SkipFunctionBodies; |
| 1989 | |
Argyrios Kyrtzidis | a3e2ff1 | 2015-11-20 03:36:21 +0000 | [diff] [blame] | 1990 | if (ModuleFormat) |
| 1991 | CI->getHeaderSearchOpts().ModuleFormat = ModuleFormat.getValue(); |
| 1992 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1993 | // Create the AST unit. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1994 | std::unique_ptr<ASTUnit> AST; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1995 | AST.reset(new ASTUnit(false)); |
Justin Bogner | dbbcb11 | 2014-10-14 23:36:06 +0000 | [diff] [blame] | 1996 | ConfigureDiags(Diags, *AST, CaptureDiagnostics); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1997 | AST->Diagnostics = Diags; |
Anders Carlsson | c30dcec | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 1998 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
Ben Langmuir | 8832c06 | 2014-04-15 18:16:25 +0000 | [diff] [blame] | 1999 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = |
| 2000 | createVFSFromCompilerInvocation(*CI, *Diags); |
| 2001 | if (!VFS) |
| 2002 | return nullptr; |
| 2003 | AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 2004 | AST->PCMCache = new MemoryBufferCache; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 2005 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 2006 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 2007 | AST->TUKind = TUKind; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 2008 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2009 | AST->IncludeBriefCommentsInCodeCompletion |
| 2010 | = IncludeBriefCommentsInCodeCompletion; |
Argyrios Kyrtzidis | 6d7833f | 2012-07-11 20:59:04 +0000 | [diff] [blame] | 2011 | AST->UserFilesAreVolatile = UserFilesAreVolatile; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 2012 | AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 2013 | AST->StoredDiagnostics.swap(StoredDiagnostics); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2014 | AST->Invocation = CI; |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2015 | if (ForSerialization) |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 2016 | AST->WriterData.reset(new ASTWriterData(*AST->PCMCache)); |
Alexey Samsonov | b4f99dd | 2014-08-28 23:51:01 +0000 | [diff] [blame] | 2017 | // Zero out now to ease cleanup during crash recovery. |
| 2018 | CI = nullptr; |
| 2019 | Diags = nullptr; |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2020 | |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 2021 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2022 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 2023 | ASTUnitCleanup(AST.get()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 2024 | |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 2025 | if (AST->LoadFromCompilerInvocation(std::move(PCHContainerOps), |
Benjamin Kramer | 5c248d8 | 2015-12-15 09:30:31 +0000 | [diff] [blame] | 2026 | PrecompilePreambleAfterNParses)) { |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2027 | // Some error occurred, if caller wants to examine diagnostics, pass it the |
| 2028 | // ASTUnit. |
| 2029 | if (ErrAST) { |
| 2030 | AST->StoredDiagnostics.swap(AST->FailedParseDiagnostics); |
| 2031 | ErrAST->swap(AST); |
| 2032 | } |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2033 | return nullptr; |
Argyrios Kyrtzidis | ac1cc93 | 2012-04-11 02:11:16 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 2036 | return AST.release(); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 2037 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2038 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2039 | bool ASTUnit::Reparse(std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 2040 | ArrayRef<RemappedFile> RemappedFiles) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2041 | if (!Invocation) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2042 | return true; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2043 | |
| 2044 | clearFileLevelDecls(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2045 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 2046 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 2047 | ParsingTimer.setOutput("Reparsing " + getMainFileName()); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 2049 | // Remap files. |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 2050 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Alp Toker | 1b070d2 | 2014-07-07 07:47:20 +0000 | [diff] [blame] | 2051 | for (const auto &RB : PPOpts.RemappedFileBuffers) |
| 2052 | delete RB.second; |
| 2053 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 2054 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2055 | for (const auto &RemappedFile : RemappedFiles) { |
| 2056 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFile.first, |
| 2057 | RemappedFile.second); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 2058 | } |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 2060 | // If we have a preamble file lying around, or if we might try to |
| 2061 | // build a precompiled preamble, do so now. |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 2062 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 2063 | if (!getPreambleFile(this).empty() || PreambleRebuildCounter > 0) |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2064 | OverrideMainBuffer = |
| 2065 | getMainBufferWithPrecompiledPreamble(PCHContainerOps, *Invocation); |
| 2066 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2067 | // Clear out the diagnostics state. |
Benjamin Kramer | bc63290 | 2015-10-06 14:45:20 +0000 | [diff] [blame] | 2068 | FileMgr.reset(); |
Argyrios Kyrtzidis | f50f7b2 | 2011-11-03 20:28:19 +0000 | [diff] [blame] | 2069 | getDiagnostics().Reset(); |
| 2070 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Argyrios Kyrtzidis | 462ff35 | 2011-11-03 20:57:33 +0000 | [diff] [blame] | 2071 | if (OverrideMainBuffer) |
| 2072 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
Argyrios Kyrtzidis | f50f7b2 | 2011-11-03 20:28:19 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 2074 | // Parse the sources |
Benjamin Kramer | d6da1a0 | 2016-06-12 20:05:23 +0000 | [diff] [blame] | 2075 | bool Result = |
| 2076 | Parse(std::move(PCHContainerOps), std::move(OverrideMainBuffer)); |
Rafael Espindola | 3248208 | 2014-08-18 16:23:45 +0000 | [diff] [blame] | 2077 | |
Argyrios Kyrtzidis | 3689337 | 2011-10-31 21:25:31 +0000 | [diff] [blame] | 2078 | // If we're caching global code-completion results, and the top-level |
| 2079 | // declarations have changed, clear out the code-completion cache. |
| 2080 | if (!Result && ShouldCacheCodeCompletionResults && |
| 2081 | CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) |
| 2082 | CacheCodeCompletionResults(); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 2083 | |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2084 | // We now need to clear out the completion info related to this translation |
| 2085 | // unit; it'll be recreated if necessary. |
| 2086 | CCTUInfo.reset(); |
Douglas Gregor | 3f35bb2 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 2088 | return Result; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 2089 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2091 | //----------------------------------------------------------------------------// |
| 2092 | // Code completion |
| 2093 | //----------------------------------------------------------------------------// |
| 2094 | |
| 2095 | namespace { |
| 2096 | /// \brief Code completion consumer that combines the cached code-completion |
| 2097 | /// results from an ASTUnit with the code-completion results provided to it, |
| 2098 | /// then passes the result on to |
| 2099 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 2100 | uint64_t NormalContexts; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2101 | ASTUnit &AST; |
| 2102 | CodeCompleteConsumer &Next; |
| 2103 | |
| 2104 | public: |
| 2105 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2106 | const CodeCompleteOptions &CodeCompleteOpts) |
| 2107 | : CodeCompleteConsumer(CodeCompleteOpts, Next.isOutputBinary()), |
| 2108 | AST(AST), Next(Next) |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2109 | { |
| 2110 | // Compute the set of contexts in which we will look when we don't have |
| 2111 | // any information about the specific context. |
| 2112 | NormalContexts |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 2113 | = (1LL << CodeCompletionContext::CCC_TopLevel) |
| 2114 | | (1LL << CodeCompletionContext::CCC_ObjCInterface) |
| 2115 | | (1LL << CodeCompletionContext::CCC_ObjCImplementation) |
| 2116 | | (1LL << CodeCompletionContext::CCC_ObjCIvarList) |
| 2117 | | (1LL << CodeCompletionContext::CCC_Statement) |
| 2118 | | (1LL << CodeCompletionContext::CCC_Expression) |
| 2119 | | (1LL << CodeCompletionContext::CCC_ObjCMessageReceiver) |
| 2120 | | (1LL << CodeCompletionContext::CCC_DotMemberAccess) |
| 2121 | | (1LL << CodeCompletionContext::CCC_ArrowMemberAccess) |
| 2122 | | (1LL << CodeCompletionContext::CCC_ObjCPropertyAccess) |
| 2123 | | (1LL << CodeCompletionContext::CCC_ObjCProtocolName) |
| 2124 | | (1LL << CodeCompletionContext::CCC_ParenthesizedExpression) |
| 2125 | | (1LL << CodeCompletionContext::CCC_Recovery); |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2126 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2127 | if (AST.getASTContext().getLangOpts().CPlusPlus) |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 2128 | NormalContexts |= (1LL << CodeCompletionContext::CCC_EnumTag) |
| 2129 | | (1LL << CodeCompletionContext::CCC_UnionTag) |
| 2130 | | (1LL << CodeCompletionContext::CCC_ClassOrStructTag); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2131 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 2132 | |
| 2133 | void ProcessCodeCompleteResults(Sema &S, CodeCompletionContext Context, |
| 2134 | CodeCompletionResult *Results, |
| 2135 | unsigned NumResults) override; |
| 2136 | |
| 2137 | void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 2138 | OverloadCandidate *Candidates, |
| 2139 | unsigned NumCandidates) override { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2140 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); |
| 2141 | } |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 2142 | |
| 2143 | CodeCompletionAllocator &getAllocator() override { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2144 | return Next.getAllocator(); |
| 2145 | } |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2146 | |
Craig Topper | afa7cb3 | 2014-03-13 06:07:04 +0000 | [diff] [blame] | 2147 | CodeCompletionTUInfo &getCodeCompletionTUInfo() override { |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2148 | return Next.getCodeCompletionTUInfo(); |
| 2149 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2150 | }; |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2151 | } // anonymous namespace |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2153 | /// \brief Helper function that computes which global names are hidden by the |
| 2154 | /// local code-completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 2155 | static void CalculateHiddenNames(const CodeCompletionContext &Context, |
| 2156 | CodeCompletionResult *Results, |
| 2157 | unsigned NumResults, |
| 2158 | ASTContext &Ctx, |
| 2159 | llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){ |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2160 | bool OnlyTagNames = false; |
| 2161 | switch (Context.getKind()) { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2162 | case CodeCompletionContext::CCC_Recovery: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2163 | case CodeCompletionContext::CCC_TopLevel: |
| 2164 | case CodeCompletionContext::CCC_ObjCInterface: |
| 2165 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 2166 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 2167 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 2168 | case CodeCompletionContext::CCC_Statement: |
| 2169 | case CodeCompletionContext::CCC_Expression: |
| 2170 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2171 | case CodeCompletionContext::CCC_DotMemberAccess: |
| 2172 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
| 2173 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2174 | case CodeCompletionContext::CCC_Namespace: |
| 2175 | case CodeCompletionContext::CCC_Type: |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2176 | case CodeCompletionContext::CCC_Name: |
| 2177 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 2178 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 2179 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | |
| 2182 | case CodeCompletionContext::CCC_EnumTag: |
| 2183 | case CodeCompletionContext::CCC_UnionTag: |
| 2184 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 2185 | OnlyTagNames = true; |
| 2186 | break; |
| 2187 | |
| 2188 | case CodeCompletionContext::CCC_ObjCProtocolName: |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2189 | case CodeCompletionContext::CCC_MacroName: |
| 2190 | case CodeCompletionContext::CCC_MacroNameUse: |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 2191 | case CodeCompletionContext::CCC_PreprocessorExpression: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 2192 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Douglas Gregor | ea14705 | 2010-08-25 18:04:30 +0000 | [diff] [blame] | 2193 | case CodeCompletionContext::CCC_NaturalLanguage: |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 2194 | case CodeCompletionContext::CCC_SelectorName: |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 2195 | case CodeCompletionContext::CCC_TypeQualifiers: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 2196 | case CodeCompletionContext::CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 2197 | case CodeCompletionContext::CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 2198 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
| 2199 | case CodeCompletionContext::CCC_ObjCClassMessage: |
| 2200 | case CodeCompletionContext::CCC_ObjCCategoryName: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 2201 | // We're looking for nothing, or we're looking for names that cannot |
| 2202 | // be hidden. |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2203 | return; |
| 2204 | } |
| 2205 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2206 | typedef CodeCompletionResult Result; |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2207 | for (unsigned I = 0; I != NumResults; ++I) { |
| 2208 | if (Results[I].Kind != Result::RK_Declaration) |
| 2209 | continue; |
| 2210 | |
| 2211 | unsigned IDNS |
| 2212 | = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); |
| 2213 | |
| 2214 | bool Hiding = false; |
| 2215 | if (OnlyTagNames) |
| 2216 | Hiding = (IDNS & Decl::IDNS_Tag); |
| 2217 | else { |
| 2218 | unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 2219 | Decl::IDNS_Namespace | Decl::IDNS_Ordinary | |
| 2220 | Decl::IDNS_NonMemberOperator); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2221 | if (Ctx.getLangOpts().CPlusPlus) |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2222 | HiddenIDNS |= Decl::IDNS_Tag; |
| 2223 | Hiding = (IDNS & HiddenIDNS); |
| 2224 | } |
| 2225 | |
| 2226 | if (!Hiding) |
| 2227 | continue; |
| 2228 | |
| 2229 | DeclarationName Name = Results[I].Declaration->getDeclName(); |
| 2230 | if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) |
| 2231 | HiddenNames.insert(Identifier->getName()); |
| 2232 | else |
| 2233 | HiddenNames.insert(Name.getAsString()); |
| 2234 | } |
| 2235 | } |
| 2236 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2237 | void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, |
| 2238 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2239 | CodeCompletionResult *Results, |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2240 | unsigned NumResults) { |
| 2241 | // Merge the results we were given with the results we cached. |
| 2242 | bool AddedResult = false; |
Richard Smith | 697cc9e | 2012-08-14 03:13:00 +0000 | [diff] [blame] | 2243 | uint64_t InContexts = |
| 2244 | Context.getKind() == CodeCompletionContext::CCC_Recovery |
| 2245 | ? NormalContexts : (1LL << Context.getKind()); |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2246 | // Contains the set of names that are hidden by "local" completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 2247 | llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames; |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 2248 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2249 | SmallVector<Result, 8> AllResults; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2250 | for (ASTUnit::cached_completion_iterator |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2251 | C = AST.cached_completion_begin(), |
| 2252 | CEnd = AST.cached_completion_end(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2253 | C != CEnd; ++C) { |
| 2254 | // If the context we are in matches any of the contexts we are |
| 2255 | // interested in, we'll add this result. |
| 2256 | if ((C->ShowInContexts & InContexts) == 0) |
| 2257 | continue; |
| 2258 | |
| 2259 | // If we haven't added any results previously, do so now. |
| 2260 | if (!AddedResult) { |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2261 | CalculateHiddenNames(Context, Results, NumResults, S.Context, |
| 2262 | HiddenNames); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2263 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
| 2264 | AddedResult = true; |
| 2265 | } |
| 2266 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2267 | // Determine whether this global completion result is hidden by a local |
| 2268 | // completion result. If so, skip it. |
| 2269 | if (C->Kind != CXCursor_MacroDefinition && |
| 2270 | HiddenNames.count(C->Completion->getTypedText())) |
| 2271 | continue; |
| 2272 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2273 | // Adjust priority based on similar type classes. |
| 2274 | unsigned Priority = C->Priority; |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2275 | CodeCompletionString *Completion = C->Completion; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2276 | if (!Context.getPreferredType().isNull()) { |
| 2277 | if (C->Kind == CXCursor_MacroDefinition) { |
| 2278 | Priority = getMacroUsagePriority(C->Completion->getTypedText(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2279 | S.getLangOpts(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2280 | Context.getPreferredType()->isAnyPointerType()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2281 | } else if (C->Type) { |
| 2282 | CanQualType Expected |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2283 | = S.Context.getCanonicalType( |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2284 | Context.getPreferredType().getUnqualifiedType()); |
| 2285 | SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); |
| 2286 | if (ExpectedSTC == C->TypeClass) { |
| 2287 | // We know this type is similar; check for an exact match. |
| 2288 | llvm::StringMap<unsigned> &CachedCompletionTypes |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2289 | = AST.getCachedCompletionTypes(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2290 | llvm::StringMap<unsigned>::iterator Pos |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2291 | = CachedCompletionTypes.find(QualType(Expected).getAsString()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2292 | if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) |
| 2293 | Priority /= CCF_ExactTypeMatch; |
| 2294 | else |
| 2295 | Priority /= CCF_SimilarTypeMatch; |
| 2296 | } |
| 2297 | } |
| 2298 | } |
| 2299 | |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2300 | // Adjust the completion string, if required. |
| 2301 | if (C->Kind == CXCursor_MacroDefinition && |
| 2302 | Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { |
| 2303 | // Create a new code-completion string that just contains the |
| 2304 | // macro name, without its arguments. |
Argyrios Kyrtzidis | 9d7c0fe | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 2305 | CodeCompletionBuilder Builder(getAllocator(), getCodeCompletionTUInfo(), |
| 2306 | CCP_CodePattern, C->Availability); |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2307 | Builder.AddTypedTextChunk(C->Completion->getTypedText()); |
Douglas Gregor | 8850aa3 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 2308 | Priority = CCP_CodePattern; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2309 | Completion = Builder.TakeString(); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
Argyrios Kyrtzidis | 5c8b1cd | 2012-09-27 00:24:09 +0000 | [diff] [blame] | 2312 | AllResults.push_back(Result(Completion, Priority, C->Kind, |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 2313 | C->Availability)); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | // If we did not add any cached completion results, just forward the |
| 2317 | // results we were given to the next consumer. |
| 2318 | if (!AddedResult) { |
| 2319 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
| 2320 | return; |
| 2321 | } |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2323 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
| 2324 | AllResults.size()); |
| 2325 | } |
| 2326 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2327 | void ASTUnit::CodeComplete( |
| 2328 | StringRef File, unsigned Line, unsigned Column, |
| 2329 | ArrayRef<RemappedFile> RemappedFiles, bool IncludeMacros, |
| 2330 | bool IncludeCodePatterns, bool IncludeBriefComments, |
| 2331 | CodeCompleteConsumer &Consumer, |
| 2332 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, |
| 2333 | DiagnosticsEngine &Diag, LangOptions &LangOpts, SourceManager &SourceMgr, |
| 2334 | FileManager &FileMgr, SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, |
| 2335 | SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2336 | if (!Invocation) |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2337 | return; |
| 2338 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 2339 | SimpleTimer CompletionTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 2340 | CompletionTimer.setOutput("Code completion @ " + File + ":" + |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2341 | Twine(Line) + ":" + Twine(Column)); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2342 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2343 | auto CCInvocation = std::make_shared<CompilerInvocation>(*Invocation); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2344 | |
| 2345 | FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2346 | CodeCompleteOptions &CodeCompleteOpts = FrontendOpts.CodeCompleteOpts; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2347 | PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); |
Douglas Gregor | b68bc59 | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 2348 | |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2349 | CodeCompleteOpts.IncludeMacros = IncludeMacros && |
| 2350 | CachedCompletionResults.empty(); |
| 2351 | CodeCompleteOpts.IncludeCodePatterns = IncludeCodePatterns; |
| 2352 | CodeCompleteOpts.IncludeGlobals = CachedCompletionResults.empty(); |
| 2353 | CodeCompleteOpts.IncludeBriefComments = IncludeBriefComments; |
| 2354 | |
| 2355 | assert(IncludeBriefComments == this->IncludeBriefCommentsInCodeCompletion); |
| 2356 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2357 | FrontendOpts.CodeCompletionAt.FileName = File; |
| 2358 | FrontendOpts.CodeCompletionAt.Line = Line; |
| 2359 | FrontendOpts.CodeCompletionAt.Column = Column; |
| 2360 | |
| 2361 | // Set the language options appropriately. |
Ted Kremenek | 8cf47df | 2011-11-17 23:01:24 +0000 | [diff] [blame] | 2362 | LangOpts = *CCInvocation->getLangOpts(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2363 | |
Argyrios Kyrtzidis | 06e8d69 | 2014-10-31 16:44:32 +0000 | [diff] [blame] | 2364 | // Spell-checking and warnings are wasteful during code-completion. |
| 2365 | LangOpts.SpellChecking = false; |
| 2366 | CCInvocation->getDiagnosticOpts().IgnoreWarnings = true; |
| 2367 | |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 2368 | std::unique_ptr<CompilerInstance> Clang( |
| 2369 | new CompilerInstance(PCHContainerOps)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2370 | |
| 2371 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2372 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 2373 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2374 | |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2375 | auto &Inv = *CCInvocation; |
| 2376 | Clang->setInvocation(std::move(CCInvocation)); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 2377 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].getFile(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2378 | |
| 2379 | // Set up diagnostics, capturing any diagnostics produced. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2380 | Clang->setDiagnostics(&Diag); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2381 | CaptureDroppedDiagnostics Capture(true, |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2382 | Clang->getDiagnostics(), |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2383 | StoredDiagnostics); |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2384 | ProcessWarningOptions(Diag, Inv.getDiagnosticOpts()); |
| 2385 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2386 | // Create the target instance. |
Alp Toker | 8075808 | 2014-07-06 05:26:44 +0000 | [diff] [blame] | 2387 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
Saleem Abdulrasool | 10a4972 | 2016-04-08 16:52:00 +0000 | [diff] [blame] | 2388 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2389 | if (!Clang->hasTarget()) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2390 | Clang->setInvocation(nullptr); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 2391 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | // Inform the target of the language options. |
| 2395 | // |
| 2396 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 2397 | // created. This complexity should be lifted elsewhere. |
Alp Toker | 7443797 | 2014-07-06 05:14:24 +0000 | [diff] [blame] | 2398 | Clang->getTarget().adjust(Clang->getLangOpts()); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2399 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2400 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2401 | "Invocation must have exactly one source file!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 2402 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_AST && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2403 | "FIXME: AST inputs not yet supported here!"); |
Argyrios Kyrtzidis | 873c858 | 2012-11-09 19:40:39 +0000 | [diff] [blame] | 2404 | assert(Clang->getFrontendOpts().Inputs[0].getKind() != IK_LLVM_IR && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2405 | "IR inputs not support here!"); |
| 2406 | |
| 2407 | |
| 2408 | // Use the source and file managers that we were given. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2409 | Clang->setFileManager(&FileMgr); |
| 2410 | Clang->setSourceManager(&SourceMgr); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2411 | |
| 2412 | // Remap files. |
| 2413 | PreprocessorOpts.clearRemappedFiles(); |
Douglas Gregor | d8a5dba | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 2414 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2415 | for (const auto &RemappedFile : RemappedFiles) { |
| 2416 | PreprocessorOpts.addRemappedFile(RemappedFile.first, RemappedFile.second); |
| 2417 | OwnedBuffers.push_back(RemappedFile.second); |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2418 | } |
Dmitri Gribenko | c444b57 | 2014-02-08 00:38:15 +0000 | [diff] [blame] | 2419 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2420 | // Use the code completion consumer we were given, but adding any cached |
| 2421 | // code-completion results. |
Douglas Gregor | e9186e6 | 2010-11-29 16:13:56 +0000 | [diff] [blame] | 2422 | AugmentedCodeCompleteConsumer *AugmentedConsumer |
Dmitri Gribenko | 3292d06 | 2012-07-02 17:35:10 +0000 | [diff] [blame] | 2423 | = new AugmentedCodeCompleteConsumer(*this, Consumer, CodeCompleteOpts); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2424 | Clang->setCodeCompletionConsumer(AugmentedConsumer); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2425 | |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2426 | // If we have a precompiled preamble, try to use it. We only allow |
| 2427 | // the use of the precompiled preamble if we're if the completion |
| 2428 | // point is within the main file, after the end of the precompiled |
| 2429 | // preamble. |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2430 | std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 2431 | if (!getPreambleFile(this).empty()) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2432 | std::string CompleteFilePath(File); |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 2433 | llvm::sys::fs::UniqueID CompleteFileID; |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2434 | |
Rafael Espindola | be3b12b0 | 2013-06-20 15:12:38 +0000 | [diff] [blame] | 2435 | if (!llvm::sys::fs::getUniqueID(CompleteFilePath, CompleteFileID)) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2436 | std::string MainPath(OriginalSourceFile); |
Rafael Espindola | 073ff10 | 2013-07-29 21:26:52 +0000 | [diff] [blame] | 2437 | llvm::sys::fs::UniqueID MainID; |
Rafael Espindola | be3b12b0 | 2013-06-20 15:12:38 +0000 | [diff] [blame] | 2438 | if (!llvm::sys::fs::getUniqueID(MainPath, MainID)) { |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2439 | if (CompleteFileID == MainID && Line > 1) |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2440 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble( |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 2441 | PCHContainerOps, Inv, false, Line - 1); |
Rafael Espindola | f9e9bb8 | 2013-06-18 19:40:07 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | // If the main file has been overridden due to the use of a preamble, |
| 2447 | // make that override happen and introduce the preamble. |
| 2448 | if (OverrideMainBuffer) { |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2449 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, |
| 2450 | OverrideMainBuffer.get()); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2451 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 2452 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 2453 | = PreambleEndsAtStartOfLine; |
Ted Kremenek | 06b4f91 | 2011-10-27 17:55:18 +0000 | [diff] [blame] | 2454 | PreprocessorOpts.ImplicitPCHInclude = getPreambleFile(this); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2455 | PreprocessorOpts.DisablePCHValidation = true; |
Rafael Espindola | 2346a37 | 2014-08-18 18:47:08 +0000 | [diff] [blame] | 2456 | |
| 2457 | OwnedBuffers.push_back(OverrideMainBuffer.release()); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 2458 | } else { |
| 2459 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 2460 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Argyrios Kyrtzidis | 870704f | 2012-11-02 22:18:44 +0000 | [diff] [blame] | 2463 | // Disable the preprocessing record if modules are not enabled. |
| 2464 | if (!Clang->getLangOpts().Modules) |
| 2465 | PreprocessorOpts.DetailedRecord = false; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 2466 | |
| 2467 | std::unique_ptr<SyntaxOnlyAction> Act; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2468 | Act.reset(new SyntaxOnlyAction); |
Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 2469 | if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) { |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2470 | Act->Execute(); |
| 2471 | Act->EndSourceFile(); |
| 2472 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2473 | } |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2474 | |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2475 | bool ASTUnit::Save(StringRef File) { |
Argyrios Kyrtzidis | dc9fdaf | 2013-05-24 05:44:08 +0000 | [diff] [blame] | 2476 | if (HadModuleLoaderFatalFailure) |
| 2477 | return true; |
| 2478 | |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2479 | // Write to a temporary file and later rename it to the actual file, to avoid |
| 2480 | // possible race conditions. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2481 | SmallString<128> TempPath; |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2482 | TempPath = File; |
| 2483 | TempPath += "-%%%%%%%%"; |
| 2484 | int fd; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 2485 | if (llvm::sys::fs::createUniqueFile(TempPath, fd, TempPath)) |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2486 | return true; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2487 | |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2488 | // FIXME: Can we somehow regenerate the stat cache here, or do we need to |
| 2489 | // unconditionally create a stat cache when we parse the file? |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2490 | llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2491 | |
| 2492 | serialize(Out); |
| 2493 | Out.close(); |
Argyrios Kyrtzidis | eeea16a | 2012-03-13 02:17:06 +0000 | [diff] [blame] | 2494 | if (Out.has_error()) { |
| 2495 | Out.clear_error(); |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2496 | return true; |
Argyrios Kyrtzidis | eeea16a | 2012-03-13 02:17:06 +0000 | [diff] [blame] | 2497 | } |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2498 | |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 2499 | if (llvm::sys::fs::rename(TempPath, File)) { |
| 2500 | llvm::sys::fs::remove(TempPath); |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2501 | return true; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Argyrios Kyrtzidis | 39a7638 | 2012-09-26 16:39:46 +0000 | [diff] [blame] | 2504 | return false; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2507 | static bool serializeUnit(ASTWriter &Writer, |
| 2508 | SmallVectorImpl<char> &Buffer, |
| 2509 | Sema &S, |
| 2510 | bool hasErrors, |
| 2511 | raw_ostream &OS) { |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2512 | Writer.WriteAST(S, std::string(), nullptr, "", hasErrors); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Write the generated bitstream to "Out". |
| 2515 | if (!Buffer.empty()) |
| 2516 | OS.write(Buffer.data(), Buffer.size()); |
| 2517 | |
| 2518 | return false; |
| 2519 | } |
| 2520 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2521 | bool ASTUnit::serialize(raw_ostream &OS) { |
Argyrios Kyrtzidis | 70ec1c7 | 2016-07-13 20:35:26 +0000 | [diff] [blame] | 2522 | // For serialization we are lenient if the errors were only warn-as-error kind. |
| 2523 | bool hasErrors = getDiagnostics().hasUncompilableErrorOccurred(); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2524 | |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2525 | if (WriterData) |
| 2526 | return serializeUnit(WriterData->Writer, WriterData->Buffer, |
| 2527 | getSema(), hasErrors, OS); |
| 2528 | |
Daniel Dunbar | 9a96386 | 2012-02-29 20:31:23 +0000 | [diff] [blame] | 2529 | SmallString<128> Buffer; |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2530 | llvm::BitstreamWriter Stream(Buffer); |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame^] | 2531 | MemoryBufferCache PCMCache; |
| 2532 | ASTWriter Writer(Stream, Buffer, PCMCache, {}); |
Argyrios Kyrtzidis | 0db720f | 2012-10-11 16:05:00 +0000 | [diff] [blame] | 2533 | return serializeUnit(Writer, Buffer, getSema(), hasErrors, OS); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2534 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2535 | |
| 2536 | typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap; |
| 2537 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2538 | void ASTUnit::TranslateStoredDiagnostics( |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2539 | FileManager &FileMgr, |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2540 | SourceManager &SrcMgr, |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2541 | const SmallVectorImpl<StandaloneDiagnostic> &Diags, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2542 | SmallVectorImpl<StoredDiagnostic> &Out) { |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2543 | // Map the standalone diagnostic into the new source manager. We also need to |
| 2544 | // remap all the locations to the new view. This includes the diag location, |
| 2545 | // any associated source ranges, and the source ranges of associated fix-its. |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2546 | // FIXME: There should be a cleaner way to do this. |
| 2547 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2548 | SmallVector<StoredDiagnostic, 4> Result; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2549 | Result.reserve(Diags.size()); |
Erik Verbruggen | 2c7c38d | 2017-02-16 09:49:30 +0000 | [diff] [blame] | 2550 | const FileEntry *PreviousFE = nullptr; |
| 2551 | FileID FID; |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2552 | for (const StandaloneDiagnostic &SD : Diags) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2553 | // Rebuild the StoredDiagnostic. |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2554 | if (SD.Filename.empty()) |
| 2555 | continue; |
| 2556 | const FileEntry *FE = FileMgr.getFile(SD.Filename); |
| 2557 | if (!FE) |
| 2558 | continue; |
Erik Verbruggen | 2c7c38d | 2017-02-16 09:49:30 +0000 | [diff] [blame] | 2559 | if (FE != PreviousFE) { |
| 2560 | FID = SrcMgr.translateFile(FE); |
| 2561 | PreviousFE = FE; |
| 2562 | } |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2563 | SourceLocation FileLoc = SrcMgr.getLocForStartOfFile(FID); |
| 2564 | if (FileLoc.isInvalid()) |
| 2565 | continue; |
| 2566 | SourceLocation L = FileLoc.getLocWithOffset(SD.LocOffset); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2567 | FullSourceLoc Loc(L, SrcMgr); |
| 2568 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2569 | SmallVector<CharSourceRange, 4> Ranges; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2570 | Ranges.reserve(SD.Ranges.size()); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2571 | for (const auto &Range : SD.Ranges) { |
| 2572 | SourceLocation BL = FileLoc.getLocWithOffset(Range.first); |
| 2573 | SourceLocation EL = FileLoc.getLocWithOffset(Range.second); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2574 | Ranges.push_back(CharSourceRange::getCharRange(BL, EL)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2577 | SmallVector<FixItHint, 2> FixIts; |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2578 | FixIts.reserve(SD.FixIts.size()); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2579 | for (const StandaloneFixIt &FixIt : SD.FixIts) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2580 | FixIts.push_back(FixItHint()); |
| 2581 | FixItHint &FH = FixIts.back(); |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2582 | FH.CodeToInsert = FixIt.CodeToInsert; |
| 2583 | SourceLocation BL = FileLoc.getLocWithOffset(FixIt.RemoveRange.first); |
| 2584 | SourceLocation EL = FileLoc.getLocWithOffset(FixIt.RemoveRange.second); |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2585 | FH.RemoveRange = CharSourceRange::getCharRange(BL, EL); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Argyrios Kyrtzidis | 24c5522 | 2014-02-28 07:11:01 +0000 | [diff] [blame] | 2588 | Result.push_back(StoredDiagnostic(SD.Level, SD.ID, |
| 2589 | SD.Message, Loc, Ranges, FixIts)); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2590 | } |
| 2591 | Result.swap(Out); |
| 2592 | } |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2593 | |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2594 | void ASTUnit::addFileLevelDecl(Decl *D) { |
| 2595 | assert(D); |
Douglas Gregor | 61d63d0 | 2011-11-07 18:53:57 +0000 | [diff] [blame] | 2596 | |
| 2597 | // We only care about local declarations. |
| 2598 | if (D->isFromASTFile()) |
| 2599 | return; |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2600 | |
| 2601 | SourceManager &SM = *SourceMgr; |
| 2602 | SourceLocation Loc = D->getLocation(); |
| 2603 | if (Loc.isInvalid() || !SM.isLocalSourceLocation(Loc)) |
| 2604 | return; |
| 2605 | |
| 2606 | // We only keep track of the file-level declarations of each file. |
| 2607 | if (!D->getLexicalDeclContext()->isFileContext()) |
| 2608 | return; |
| 2609 | |
| 2610 | SourceLocation FileLoc = SM.getFileLoc(Loc); |
| 2611 | assert(SM.isLocalSourceLocation(FileLoc)); |
| 2612 | FileID FID; |
| 2613 | unsigned Offset; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 2614 | std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2615 | if (FID.isInvalid()) |
| 2616 | return; |
| 2617 | |
| 2618 | LocDeclsTy *&Decls = FileDecls[FID]; |
| 2619 | if (!Decls) |
| 2620 | Decls = new LocDeclsTy(); |
| 2621 | |
| 2622 | std::pair<unsigned, Decl *> LocDecl(Offset, D); |
| 2623 | |
| 2624 | if (Decls->empty() || Decls->back().first <= Offset) { |
| 2625 | Decls->push_back(LocDecl); |
| 2626 | return; |
| 2627 | } |
| 2628 | |
Benjamin Kramer | 45025c0 | 2013-08-24 13:22:59 +0000 | [diff] [blame] | 2629 | LocDeclsTy::iterator I = std::upper_bound(Decls->begin(), Decls->end(), |
| 2630 | LocDecl, llvm::less_first()); |
Argyrios Kyrtzidis | e54568d | 2011-10-31 07:19:59 +0000 | [diff] [blame] | 2631 | |
| 2632 | Decls->insert(I, LocDecl); |
| 2633 | } |
| 2634 | |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2635 | void ASTUnit::findFileRegionDecls(FileID File, unsigned Offset, unsigned Length, |
| 2636 | SmallVectorImpl<Decl *> &Decls) { |
| 2637 | if (File.isInvalid()) |
| 2638 | return; |
| 2639 | |
| 2640 | if (SourceMgr->isLoadedFileID(File)) { |
| 2641 | assert(Ctx->getExternalSource() && "No external source!"); |
| 2642 | return Ctx->getExternalSource()->FindFileRegionDecls(File, Offset, Length, |
| 2643 | Decls); |
| 2644 | } |
| 2645 | |
| 2646 | FileDeclsTy::iterator I = FileDecls.find(File); |
| 2647 | if (I == FileDecls.end()) |
| 2648 | return; |
| 2649 | |
| 2650 | LocDeclsTy &LocDecls = *I->second; |
| 2651 | if (LocDecls.empty()) |
| 2652 | return; |
| 2653 | |
Benjamin Kramer | e3e855b | 2013-08-24 13:12:34 +0000 | [diff] [blame] | 2654 | LocDeclsTy::iterator BeginIt = |
| 2655 | std::lower_bound(LocDecls.begin(), LocDecls.end(), |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2656 | std::make_pair(Offset, (Decl *)nullptr), |
| 2657 | llvm::less_first()); |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2658 | if (BeginIt != LocDecls.begin()) |
| 2659 | --BeginIt; |
| 2660 | |
Argyrios Kyrtzidis | 8ad3bab | 2011-11-23 20:27:36 +0000 | [diff] [blame] | 2661 | // If we are pointing at a top-level decl inside an objc container, we need |
| 2662 | // to backtrack until we find it otherwise we will fail to report that the |
| 2663 | // region overlaps with an objc container. |
| 2664 | while (BeginIt != LocDecls.begin() && |
| 2665 | BeginIt->second->isTopLevelDeclInObjCContainer()) |
| 2666 | --BeginIt; |
| 2667 | |
Benjamin Kramer | e3e855b | 2013-08-24 13:12:34 +0000 | [diff] [blame] | 2668 | LocDeclsTy::iterator EndIt = std::upper_bound( |
| 2669 | LocDecls.begin(), LocDecls.end(), |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2670 | std::make_pair(Offset + Length, (Decl *)nullptr), llvm::less_first()); |
Argyrios Kyrtzidis | e968152 | 2011-11-03 02:20:32 +0000 | [diff] [blame] | 2671 | if (EndIt != LocDecls.end()) |
| 2672 | ++EndIt; |
| 2673 | |
| 2674 | for (LocDeclsTy::iterator DIt = BeginIt; DIt != EndIt; ++DIt) |
| 2675 | Decls.push_back(DIt->second); |
| 2676 | } |
| 2677 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2678 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2679 | unsigned Line, unsigned Col) const { |
| 2680 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2681 | SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2682 | return SM.getMacroArgExpandedLocation(Loc); |
| 2683 | } |
| 2684 | |
| 2685 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2686 | unsigned Offset) const { |
| 2687 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2688 | SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2689 | return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); |
| 2690 | } |
| 2691 | |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2692 | /// \brief If \arg Loc is a loaded location from the preamble, returns |
| 2693 | /// the corresponding local location of the main file, otherwise it returns |
| 2694 | /// \arg Loc. |
| 2695 | SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) { |
| 2696 | FileID PreambleID; |
| 2697 | if (SourceMgr) |
| 2698 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2699 | |
| 2700 | if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) |
| 2701 | return Loc; |
| 2702 | |
| 2703 | unsigned Offs; |
| 2704 | if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) { |
| 2705 | SourceLocation FileLoc |
| 2706 | = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); |
| 2707 | return FileLoc.getLocWithOffset(Offs); |
| 2708 | } |
| 2709 | |
| 2710 | return Loc; |
| 2711 | } |
| 2712 | |
| 2713 | /// \brief If \arg Loc is a local location of the main file but inside the |
| 2714 | /// preamble chunk, returns the corresponding loaded location from the |
| 2715 | /// preamble, otherwise it returns \arg Loc. |
| 2716 | SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) { |
| 2717 | FileID PreambleID; |
| 2718 | if (SourceMgr) |
| 2719 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2720 | |
| 2721 | if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) |
| 2722 | return Loc; |
| 2723 | |
| 2724 | unsigned Offs; |
| 2725 | if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) && |
| 2726 | Offs < Preamble.size()) { |
| 2727 | SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID); |
| 2728 | return FileLoc.getLocWithOffset(Offs); |
| 2729 | } |
| 2730 | |
| 2731 | return Loc; |
| 2732 | } |
| 2733 | |
Argyrios Kyrtzidis | 429ec02 | 2011-10-25 00:29:50 +0000 | [diff] [blame] | 2734 | bool ASTUnit::isInPreambleFileID(SourceLocation Loc) { |
| 2735 | FileID FID; |
| 2736 | if (SourceMgr) |
| 2737 | FID = SourceMgr->getPreambleFileID(); |
| 2738 | |
| 2739 | if (Loc.isInvalid() || FID.isInvalid()) |
| 2740 | return false; |
| 2741 | |
| 2742 | return SourceMgr->isInFileID(Loc, FID); |
| 2743 | } |
| 2744 | |
| 2745 | bool ASTUnit::isInMainFileID(SourceLocation Loc) { |
| 2746 | FileID FID; |
| 2747 | if (SourceMgr) |
| 2748 | FID = SourceMgr->getMainFileID(); |
| 2749 | |
| 2750 | if (Loc.isInvalid() || FID.isInvalid()) |
| 2751 | return false; |
| 2752 | |
| 2753 | return SourceMgr->isInFileID(Loc, FID); |
| 2754 | } |
| 2755 | |
| 2756 | SourceLocation ASTUnit::getEndOfPreambleFileID() { |
| 2757 | FileID FID; |
| 2758 | if (SourceMgr) |
| 2759 | FID = SourceMgr->getPreambleFileID(); |
| 2760 | |
| 2761 | if (FID.isInvalid()) |
| 2762 | return SourceLocation(); |
| 2763 | |
| 2764 | return SourceMgr->getLocForEndOfFile(FID); |
| 2765 | } |
| 2766 | |
| 2767 | SourceLocation ASTUnit::getStartOfMainFileID() { |
| 2768 | FileID FID; |
| 2769 | if (SourceMgr) |
| 2770 | FID = SourceMgr->getMainFileID(); |
| 2771 | |
| 2772 | if (FID.isInvalid()) |
| 2773 | return SourceLocation(); |
| 2774 | |
| 2775 | return SourceMgr->getLocForStartOfFile(FID); |
| 2776 | } |
| 2777 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2778 | llvm::iterator_range<PreprocessingRecord::iterator> |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2779 | ASTUnit::getLocalPreprocessingEntities() const { |
| 2780 | if (isMainFileAST()) { |
| 2781 | serialization::ModuleFile & |
| 2782 | Mod = Reader->getModuleManager().getPrimaryModule(); |
| 2783 | return Reader->getModulePreprocessedEntities(Mod); |
| 2784 | } |
| 2785 | |
| 2786 | if (PreprocessingRecord *PPRec = PP->getPreprocessingRecord()) |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2787 | return llvm::make_range(PPRec->local_begin(), PPRec->local_end()); |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2788 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2789 | return llvm::make_range(PreprocessingRecord::iterator(), |
| 2790 | PreprocessingRecord::iterator()); |
Argyrios Kyrtzidis | d4fcf580 | 2012-10-02 16:10:51 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Argyrios Kyrtzidis | e514b20 | 2012-10-03 01:58:28 +0000 | [diff] [blame] | 2793 | bool ASTUnit::visitLocalTopLevelDecls(void *context, DeclVisitorFn Fn) { |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 2794 | if (isMainFileAST()) { |
| 2795 | serialization::ModuleFile & |
| 2796 | Mod = Reader->getModuleManager().getPrimaryModule(); |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 2797 | for (const Decl *D : Reader->getModuleFileLevelDecls(Mod)) { |
| 2798 | if (!Fn(context, D)) |
Argyrios Kyrtzidis | 10e7846 | 2012-10-02 21:09:13 +0000 | [diff] [blame] | 2799 | return false; |
| 2800 | } |
| 2801 | |
| 2802 | return true; |
| 2803 | } |
| 2804 | |
| 2805 | for (ASTUnit::top_level_iterator TL = top_level_begin(), |
| 2806 | TLEnd = top_level_end(); |
| 2807 | TL != TLEnd; ++TL) { |
| 2808 | if (!Fn(context, *TL)) |
| 2809 | return false; |
| 2810 | } |
| 2811 | |
| 2812 | return true; |
| 2813 | } |
| 2814 | |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2815 | const FileEntry *ASTUnit::getPCHFile() { |
| 2816 | if (!Reader) |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2817 | return nullptr; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2818 | |
Benjamin Kramer | 9a9efba | 2015-07-25 12:14:04 +0000 | [diff] [blame] | 2819 | serialization::ModuleFile *Mod = nullptr; |
| 2820 | Reader->getModuleManager().visit([&Mod](serialization::ModuleFile &M) { |
| 2821 | switch (M.Kind) { |
| 2822 | case serialization::MK_ImplicitModule: |
| 2823 | case serialization::MK_ExplicitModule: |
Manman Ren | 11f2a47 | 2016-08-18 17:42:15 +0000 | [diff] [blame] | 2824 | case serialization::MK_PrebuiltModule: |
Benjamin Kramer | 9a9efba | 2015-07-25 12:14:04 +0000 | [diff] [blame] | 2825 | return true; // skip dependencies. |
| 2826 | case serialization::MK_PCH: |
| 2827 | Mod = &M; |
| 2828 | return true; // found it. |
| 2829 | case serialization::MK_Preamble: |
| 2830 | return false; // look in dependencies. |
| 2831 | case serialization::MK_MainFile: |
| 2832 | return false; // look in dependencies. |
| 2833 | } |
| 2834 | |
| 2835 | return true; |
| 2836 | }); |
| 2837 | if (Mod) |
| 2838 | return Mod->File; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2839 | |
Craig Topper | 49a2790 | 2014-05-22 04:46:25 +0000 | [diff] [blame] | 2840 | return nullptr; |
Argyrios Kyrtzidis | f484b13 | 2012-10-03 21:05:51 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Argyrios Kyrtzidis | e445c72 | 2012-10-10 02:12:47 +0000 | [diff] [blame] | 2843 | bool ASTUnit::isModuleFile() { |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 2844 | return isMainFileAST() && ASTFileLangOpts.isCompilingModule(); |
Argyrios Kyrtzidis | e445c72 | 2012-10-10 02:12:47 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2847 | void ASTUnit::PreambleData::countLines() const { |
| 2848 | NumLines = 0; |
| 2849 | if (empty()) |
| 2850 | return; |
| 2851 | |
Benjamin Kramer | 6a96ae5 | 2015-02-06 18:36:04 +0000 | [diff] [blame] | 2852 | NumLines = std::count(Buffer.begin(), Buffer.end(), '\n'); |
| 2853 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2854 | if (Buffer.back() != '\n') |
| 2855 | ++NumLines; |
| 2856 | } |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 2857 | |
| 2858 | #ifndef NDEBUG |
| 2859 | ASTUnit::ConcurrencyState::ConcurrencyState() { |
| 2860 | Mutex = new llvm::sys::MutexImpl(/*recursive=*/true); |
| 2861 | } |
| 2862 | |
| 2863 | ASTUnit::ConcurrencyState::~ConcurrencyState() { |
| 2864 | delete static_cast<llvm::sys::MutexImpl *>(Mutex); |
| 2865 | } |
| 2866 | |
| 2867 | void ASTUnit::ConcurrencyState::start() { |
| 2868 | bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire(); |
| 2869 | assert(acquired && "Concurrent access to ASTUnit!"); |
| 2870 | } |
| 2871 | |
| 2872 | void ASTUnit::ConcurrencyState::finish() { |
| 2873 | static_cast<llvm::sys::MutexImpl *>(Mutex)->release(); |
| 2874 | } |
| 2875 | |
| 2876 | #else // NDEBUG |
| 2877 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2878 | ASTUnit::ConcurrencyState::ConcurrencyState() { Mutex = nullptr; } |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 2879 | ASTUnit::ConcurrencyState::~ConcurrencyState() {} |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 2880 | void ASTUnit::ConcurrencyState::start() {} |
| 2881 | void ASTUnit::ConcurrencyState::finish() {} |
| 2882 | |
Hans Wennborg | dcfba33 | 2015-10-06 23:40:43 +0000 | [diff] [blame] | 2883 | #endif // NDEBUG |