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