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