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