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