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