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