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