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