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