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