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