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