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