Argyrios Kyrtzidis | 4b562cf | 2009-06-20 08:27:14 +0000 | [diff] [blame] | 1 | //===--- ASTUnit.cpp - ASTUnit utility ------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // ASTUnit Implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/PCHWriter.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclVisitor.h" |
| 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Compilation.h" |
| 21 | #include "clang/Driver/Driver.h" |
| 22 | #include "clang/Driver/Job.h" |
| 23 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/CompilerInstance.h" |
| 25 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendOptions.h" |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/PCHReader.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 29 | #include "clang/Lex/HeaderSearch.h" |
| 30 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 32 | #include "clang/Basic/TargetInfo.h" |
| 33 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 35 | #include "llvm/System/Host.h" |
Benjamin Kramer | 4a630d3 | 2009-10-18 11:34:14 +0000 | [diff] [blame] | 36 | #include "llvm/System/Path.h" |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Timer.h" |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 39 | #include <cstdlib> |
Zhongxing Xu | ad23ebe | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 40 | #include <cstdio> |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 41 | #include <sys/stat.h> |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 42 | using namespace clang; |
| 43 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 44 | /// \brief After failing to build a precompiled preamble (due to |
| 45 | /// errors in the source that occurs in the preamble), the number of |
| 46 | /// reparses during which we'll skip even trying to precompile the |
| 47 | /// preamble. |
| 48 | const unsigned DefaultPreambleRebuildInterval = 5; |
| 49 | |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 50 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 51 | : CaptureDiagnostics(false), MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 52 | CompleteTranslationUnit(true), ConcurrencyCheckValue(CheckUnlocked), |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 53 | PreambleRebuildCounter(0), SavedMainFileBuffer(0), |
| 54 | ShouldCacheCodeCompletionResults(false) { |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 55 | } |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 57 | ASTUnit::~ASTUnit() { |
Douglas Gregor | bdf6062 | 2010-03-05 21:16:25 +0000 | [diff] [blame] | 58 | ConcurrencyCheckValue = CheckLocked; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 59 | CleanTemporaryFiles(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 60 | if (!PreambleFile.empty()) |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 61 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 62 | |
| 63 | // Free the buffers associated with remapped files. We are required to |
| 64 | // perform this operation here because we explicitly request that the |
| 65 | // compiler instance *not* free these buffers for each invocation of the |
| 66 | // parser. |
| 67 | if (Invocation.get()) { |
| 68 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 69 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 70 | FB = PPOpts.remapped_file_buffer_begin(), |
| 71 | FBEnd = PPOpts.remapped_file_buffer_end(); |
| 72 | FB != FBEnd; |
| 73 | ++FB) |
| 74 | delete FB->second; |
| 75 | } |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 76 | |
| 77 | delete SavedMainFileBuffer; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 79 | ClearCachedCompletionResults(); |
| 80 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 81 | for (unsigned I = 0, N = Timers.size(); I != N; ++I) |
| 82 | delete Timers[I]; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 313e26c | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 86 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 87 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 88 | TemporaryFiles.clear(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 89 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 90 | |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 91 | /// \brief Determine the set of code-completion contexts in which this |
| 92 | /// declaration should be shown. |
| 93 | static unsigned getDeclShowContexts(NamedDecl *ND, |
| 94 | const LangOptions &LangOpts) { |
| 95 | if (isa<UsingShadowDecl>(ND)) |
| 96 | ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 97 | if (!ND) |
| 98 | return 0; |
| 99 | |
| 100 | unsigned Contexts = 0; |
| 101 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
| 102 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { |
| 103 | // Types can appear in these contexts. |
| 104 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
| 105 | Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 106 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 107 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 108 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 109 | | (1 << (CodeCompletionContext::CCC_Type - 1)); |
| 110 | |
| 111 | // In C++, types can appear in expressions contexts (for functional casts). |
| 112 | if (LangOpts.CPlusPlus) |
| 113 | Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1)); |
| 114 | |
| 115 | // In Objective-C, message sends can send interfaces. In Objective-C++, |
| 116 | // all types are available due to functional casts. |
| 117 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
| 118 | Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 119 | |
| 120 | // Deal with tag names. |
| 121 | if (isa<EnumDecl>(ND)) { |
| 122 | Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)); |
| 123 | |
| 124 | // Part of the nested-name-specifier. |
| 125 | if (LangOpts.CPlusPlus0x) |
| 126 | Contexts |= (1 << (CodeCompletionContext::CCC_MemberAccess - 1)); |
| 127 | } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { |
| 128 | if (Record->isUnion()) |
| 129 | Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1)); |
| 130 | else |
| 131 | Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
| 132 | |
| 133 | // Part of the nested-name-specifier. |
| 134 | if (LangOpts.CPlusPlus) |
| 135 | Contexts |= (1 << (CodeCompletionContext::CCC_MemberAccess - 1)); |
| 136 | } |
| 137 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
| 138 | // Values can appear in these contexts. |
| 139 | Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 140 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 141 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 142 | } else if (isa<ObjCProtocolDecl>(ND)) { |
| 143 | Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)); |
| 144 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
| 145 | Contexts = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 146 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 147 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 148 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 149 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 150 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 151 | | (1 << (CodeCompletionContext::CCC_Namespace - 1)); |
| 152 | |
| 153 | // Part of the nested-name-specifier. |
| 154 | Contexts |= (1 << (CodeCompletionContext::CCC_MemberAccess - 1)) |
| 155 | | (1 << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 156 | | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 157 | | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)) |
| 158 | | (1 << (CodeCompletionContext::CCC_Type - 1)); |
| 159 | } |
| 160 | |
| 161 | return Contexts; |
| 162 | } |
| 163 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 164 | void ASTUnit::CacheCodeCompletionResults() { |
| 165 | if (!TheSema) |
| 166 | return; |
| 167 | |
| 168 | llvm::Timer *CachingTimer = 0; |
| 169 | if (TimerGroup.get()) { |
| 170 | CachingTimer = new llvm::Timer("Cache global code completions", |
| 171 | *TimerGroup); |
| 172 | CachingTimer->startTimer(); |
| 173 | Timers.push_back(CachingTimer); |
| 174 | } |
| 175 | |
| 176 | // Clear out the previous results. |
| 177 | ClearCachedCompletionResults(); |
| 178 | |
| 179 | // Gather the set of global code completions. |
| 180 | typedef CodeCompleteConsumer::Result Result; |
| 181 | llvm::SmallVector<Result, 8> Results; |
| 182 | TheSema->GatherGlobalCodeCompletions(Results); |
| 183 | |
| 184 | // Translate global code completions into cached completions. |
| 185 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 186 | switch (Results[I].Kind) { |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 187 | case Result::RK_Declaration: { |
| 188 | CachedCodeCompletionResult CachedResult; |
| 189 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); |
| 190 | CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration, |
| 191 | Ctx->getLangOptions()); |
| 192 | CachedResult.Priority = Results[I].Priority; |
| 193 | CachedResult.Kind = Results[I].CursorKind; |
| 194 | CachedCompletionResults.push_back(CachedResult); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 195 | break; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 198 | case Result::RK_Keyword: |
| 199 | case Result::RK_Pattern: |
| 200 | // Ignore keywords and patterns; we don't care, since they are so |
| 201 | // easily regenerated. |
| 202 | break; |
| 203 | |
| 204 | case Result::RK_Macro: { |
| 205 | CachedCodeCompletionResult CachedResult; |
| 206 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema); |
| 207 | CachedResult.ShowInContexts |
| 208 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 209 | | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 210 | | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) |
| 211 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 212 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 213 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 214 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 215 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 216 | CachedResult.Priority = Results[I].Priority; |
| 217 | CachedResult.Kind = Results[I].CursorKind; |
| 218 | CachedCompletionResults.push_back(CachedResult); |
| 219 | break; |
| 220 | } |
| 221 | } |
| 222 | Results[I].Destroy(); |
| 223 | } |
| 224 | |
| 225 | if (CachingTimer) |
| 226 | CachingTimer->stopTimer(); |
| 227 | } |
| 228 | |
| 229 | void ASTUnit::ClearCachedCompletionResults() { |
| 230 | for (unsigned I = 0, N = CachedCompletionResults.size(); I != N; ++I) |
| 231 | delete CachedCompletionResults[I].Completion; |
| 232 | CachedCompletionResults.clear(); |
| 233 | } |
| 234 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 235 | namespace { |
| 236 | |
| 237 | /// \brief Gathers information from PCHReader that will be used to initialize |
| 238 | /// a Preprocessor. |
Benjamin Kramer | bd21828 | 2009-11-28 10:07:24 +0000 | [diff] [blame] | 239 | class PCHInfoCollector : public PCHReaderListener { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 240 | LangOptions &LangOpt; |
| 241 | HeaderSearch &HSI; |
| 242 | std::string &TargetTriple; |
| 243 | std::string &Predefines; |
| 244 | unsigned &Counter; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 246 | unsigned NumHeaderInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 247 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 248 | public: |
| 249 | PCHInfoCollector(LangOptions &LangOpt, HeaderSearch &HSI, |
| 250 | std::string &TargetTriple, std::string &Predefines, |
| 251 | unsigned &Counter) |
| 252 | : LangOpt(LangOpt), HSI(HSI), TargetTriple(TargetTriple), |
| 253 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 255 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
| 256 | LangOpt = LangOpts; |
| 257 | return false; |
| 258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | dc3c0d2 | 2009-11-11 00:52:11 +0000 | [diff] [blame] | 260 | virtual bool ReadTargetTriple(llvm::StringRef Triple) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 261 | TargetTriple = Triple; |
| 262 | return false; |
| 263 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 265 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Daniel Dunbar | 7b5a121 | 2009-11-11 05:29:04 +0000 | [diff] [blame] | 266 | llvm::StringRef OriginalFileName, |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 267 | std::string &SuggestedPredefines) { |
Sebastian Redl | cb481aa | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 268 | Predefines = Buffers[0].Data; |
| 269 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 270 | Predefines += Buffers[I].Data; |
| 271 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 272 | return false; |
| 273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Douglas Gregor | ec1afbf | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 275 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 276 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 278 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 279 | virtual void ReadCounter(unsigned Value) { |
| 280 | Counter = Value; |
| 281 | } |
| 282 | }; |
| 283 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 284 | class StoredDiagnosticClient : public DiagnosticClient { |
| 285 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
| 286 | |
| 287 | public: |
| 288 | explicit StoredDiagnosticClient( |
| 289 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 290 | : StoredDiags(StoredDiags) { } |
| 291 | |
| 292 | virtual void HandleDiagnostic(Diagnostic::Level Level, |
| 293 | const DiagnosticInfo &Info); |
| 294 | }; |
| 295 | |
| 296 | /// \brief RAII object that optionally captures diagnostics, if |
| 297 | /// there is no diagnostic client to capture them already. |
| 298 | class CaptureDroppedDiagnostics { |
| 299 | Diagnostic &Diags; |
| 300 | StoredDiagnosticClient Client; |
| 301 | DiagnosticClient *PreviousClient; |
| 302 | |
| 303 | public: |
| 304 | CaptureDroppedDiagnostics(bool RequestCapture, Diagnostic &Diags, |
| 305 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
| 306 | : Diags(Diags), Client(StoredDiags), PreviousClient(Diags.getClient()) |
| 307 | { |
| 308 | if (RequestCapture || Diags.getClient() == 0) |
| 309 | Diags.setClient(&Client); |
| 310 | } |
| 311 | |
| 312 | ~CaptureDroppedDiagnostics() { |
| 313 | Diags.setClient(PreviousClient); |
| 314 | } |
| 315 | }; |
| 316 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 317 | } // anonymous namespace |
| 318 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 319 | void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level, |
| 320 | const DiagnosticInfo &Info) { |
| 321 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 322 | } |
| 323 | |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 324 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 325 | return OriginalSourceFile; |
Steve Naroff | 77accc1 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 326 | } |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 327 | |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 328 | const std::string &ASTUnit::getPCHFileName() { |
Daniel Dunbar | c7822db | 2009-12-02 21:47:43 +0000 | [diff] [blame] | 329 | assert(isMainFileAST() && "Not an ASTUnit from a PCH file!"); |
Benjamin Kramer | 7297c18 | 2010-01-30 16:23:25 +0000 | [diff] [blame] | 330 | return static_cast<PCHReader *>(Ctx->getExternalSource())->getFileName(); |
Steve Naroff | e19944c | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 333 | ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 334 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Ted Kremenek | 5cf4876 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 335 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 336 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 337 | unsigned NumRemappedFiles, |
| 338 | bool CaptureDiagnostics) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 339 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
| 340 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 341 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 342 | // No diagnostics engine was provided, so create our own diagnostics object |
| 343 | // with the default options. |
| 344 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 345 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 346 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 347 | |
| 348 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7d1d49d | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 349 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 350 | AST->Diagnostics = Diags; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 351 | AST->FileMgr.reset(new FileManager); |
| 352 | AST->SourceMgr.reset(new SourceManager(AST->getDiagnostics())); |
Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 353 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 354 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 355 | // If requested, capture diagnostics in the ASTUnit. |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 356 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, AST->getDiagnostics(), |
Douglas Gregor | 405634b | 2010-04-05 18:10:21 +0000 | [diff] [blame] | 357 | AST->StoredDiagnostics); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 359 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 360 | // Create the file entry for the file that we're mapping from. |
| 361 | const FileEntry *FromFile |
| 362 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 363 | RemappedFiles[I].second->getBufferSize(), |
| 364 | 0); |
| 365 | if (!FromFile) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 366 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 367 | << RemappedFiles[I].first; |
Douglas Gregor | c8dfe5e | 2010-02-27 01:32:48 +0000 | [diff] [blame] | 368 | delete RemappedFiles[I].second; |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 369 | continue; |
| 370 | } |
| 371 | |
| 372 | // Override the contents of the "from" file with the contents of |
| 373 | // the "to" file. |
| 374 | AST->getSourceManager().overrideFileContents(FromFile, |
| 375 | RemappedFiles[I].second); |
| 376 | } |
| 377 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 378 | // Gather Info for preprocessor construction later on. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 380 | LangOptions LangInfo; |
| 381 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
| 382 | std::string TargetTriple; |
| 383 | std::string Predefines; |
| 384 | unsigned Counter; |
| 385 | |
Daniel Dunbar | bce6f62 | 2009-09-03 05:59:50 +0000 | [diff] [blame] | 386 | llvm::OwningPtr<PCHReader> Reader; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 387 | |
Ted Kremenek | fc06221 | 2009-10-19 21:44:57 +0000 | [diff] [blame] | 388 | Reader.reset(new PCHReader(AST->getSourceManager(), AST->getFileManager(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 389 | AST->getDiagnostics())); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 390 | Reader->setListener(new PCHInfoCollector(LangInfo, HeaderInfo, TargetTriple, |
| 391 | Predefines, Counter)); |
| 392 | |
| 393 | switch (Reader->ReadPCH(Filename)) { |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 394 | case PCHReader::Success: |
| 395 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 397 | case PCHReader::Failure: |
Argyrios Kyrtzidis | 106c998 | 2009-06-25 18:22:30 +0000 | [diff] [blame] | 398 | case PCHReader::IgnorePCH: |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 399 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 400 | return NULL; |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 401 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 402 | |
Daniel Dunbar | 68d40e2 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 403 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 404 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 405 | // PCH loaded successfully. Now create the preprocessor. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 407 | // Get information about the target being compiled for. |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 408 | // |
| 409 | // FIXME: This is broken, we should store the TargetOptions in the PCH. |
| 410 | TargetOptions TargetOpts; |
| 411 | TargetOpts.ABI = ""; |
Charles Davis | 98b7c5c | 2010-06-11 01:06:47 +0000 | [diff] [blame] | 412 | TargetOpts.CXXABI = "itanium"; |
Daniel Dunbar | d58c03f | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 413 | TargetOpts.CPU = ""; |
| 414 | TargetOpts.Features.clear(); |
| 415 | TargetOpts.Triple = TargetTriple; |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 416 | AST->Target.reset(TargetInfo::CreateTargetInfo(AST->getDiagnostics(), |
| 417 | TargetOpts)); |
| 418 | AST->PP.reset(new Preprocessor(AST->getDiagnostics(), LangInfo, |
| 419 | *AST->Target.get(), |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 420 | AST->getSourceManager(), HeaderInfo)); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 421 | Preprocessor &PP = *AST->PP.get(); |
| 422 | |
Daniel Dunbar | d5b6126 | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 423 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 424 | PP.setCounterValue(Counter); |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 425 | Reader->setPreprocessor(PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 427 | // Create and initialize the ASTContext. |
| 428 | |
| 429 | AST->Ctx.reset(new ASTContext(LangInfo, |
Daniel Dunbar | 31b87d8 | 2009-09-21 03:03:39 +0000 | [diff] [blame] | 430 | AST->getSourceManager(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 431 | *AST->Target.get(), |
| 432 | PP.getIdentifierTable(), |
| 433 | PP.getSelectorTable(), |
| 434 | PP.getBuiltinInfo(), |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 435 | /* size_reserve = */0)); |
| 436 | ASTContext &Context = *AST->Ctx.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Daniel Dunbar | cc31893 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 438 | Reader->InitializeContext(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 440 | // Attach the PCH reader to the AST context as an external AST |
| 441 | // source, so that declarations will be deserialized from the |
| 442 | // PCH file as needed. |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 443 | PCHReader *ReaderPtr = Reader.get(); |
| 444 | llvm::OwningPtr<ExternalASTSource> Source(Reader.take()); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 445 | Context.setExternalSource(Source); |
| 446 | |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 447 | // Create an AST consumer, even though it isn't used. |
| 448 | AST->Consumer.reset(new ASTConsumer); |
| 449 | |
| 450 | // Create a semantic analysis object and tell the PCH reader about it. |
| 451 | AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); |
| 452 | AST->TheSema->Initialize(); |
| 453 | ReaderPtr->InitializeSema(*AST->TheSema); |
| 454 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | return AST.take(); |
Argyrios Kyrtzidis | 0853a02 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 456 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 457 | |
| 458 | namespace { |
| 459 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 460 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 461 | ASTUnit &Unit; |
| 462 | |
| 463 | public: |
| 464 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit) : Unit(_Unit) {} |
| 465 | |
| 466 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 467 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 468 | Decl *D = *it; |
| 469 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 470 | // reported as top-level declarations, even though their DeclContext |
| 471 | // is the containing ObjC @interface/@implementation. This is a |
| 472 | // fundamental problem in the parser right now. |
| 473 | if (isa<ObjCMethodDecl>(D)) |
| 474 | continue; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 475 | Unit.addTopLevelDecl(D); |
Ted Kremenek | da5a428 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 476 | } |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 477 | } |
Sebastian Redl | 27372b4 | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 478 | |
| 479 | // We're not interested in "interesting" decls. |
| 480 | void HandleInterestingDecl(DeclGroupRef) {} |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 484 | public: |
| 485 | ASTUnit &Unit; |
| 486 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 487 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 488 | llvm::StringRef InFile) { |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 489 | return new TopLevelDeclTrackerConsumer(Unit); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | public: |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 493 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 494 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 495 | virtual bool hasCodeCompletionSupport() const { return false; } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 496 | virtual bool usesCompleteTranslationUnit() { |
| 497 | return Unit.isCompleteTranslationUnit(); |
| 498 | } |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 499 | }; |
| 500 | |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 501 | class PrecompilePreambleConsumer : public PCHGenerator { |
| 502 | ASTUnit &Unit; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 503 | std::vector<Decl *> TopLevelDecls; |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 504 | |
| 505 | public: |
| 506 | PrecompilePreambleConsumer(ASTUnit &Unit, |
| 507 | const Preprocessor &PP, bool Chaining, |
| 508 | const char *isysroot, llvm::raw_ostream *Out) |
| 509 | : PCHGenerator(PP, Chaining, isysroot, Out), Unit(Unit) { } |
| 510 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 511 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 512 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 513 | Decl *D = *it; |
| 514 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 515 | // reported as top-level declarations, even though their DeclContext |
| 516 | // is the containing ObjC @interface/@implementation. This is a |
| 517 | // fundamental problem in the parser right now. |
| 518 | if (isa<ObjCMethodDecl>(D)) |
| 519 | continue; |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 520 | TopLevelDecls.push_back(D); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 525 | PCHGenerator::HandleTranslationUnit(Ctx); |
| 526 | if (!Unit.getDiagnostics().hasErrorOccurred()) { |
| 527 | // Translate the top-level declarations we captured during |
| 528 | // parsing into declaration IDs in the precompiled |
| 529 | // preamble. This will allow us to deserialize those top-level |
| 530 | // declarations when requested. |
| 531 | for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) |
| 532 | Unit.addTopLevelDeclFromPreamble( |
| 533 | getWriter().getDeclID(TopLevelDecls[I])); |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | }; |
| 537 | |
| 538 | class PrecompilePreambleAction : public ASTFrontendAction { |
| 539 | ASTUnit &Unit; |
| 540 | |
| 541 | public: |
| 542 | explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} |
| 543 | |
| 544 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
| 545 | llvm::StringRef InFile) { |
| 546 | std::string Sysroot; |
| 547 | llvm::raw_ostream *OS = 0; |
| 548 | bool Chaining; |
| 549 | if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, |
| 550 | OS, Chaining)) |
| 551 | return 0; |
| 552 | |
| 553 | const char *isysroot = CI.getFrontendOpts().RelocatablePCH ? |
| 554 | Sysroot.c_str() : 0; |
| 555 | return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Chaining, |
| 556 | isysroot, OS); |
| 557 | } |
| 558 | |
| 559 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 560 | virtual bool hasASTFileSupport() const { return false; } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 561 | virtual bool usesCompleteTranslationUnit() { return false; } |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 562 | }; |
| 563 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 566 | /// Parse the source file into a translation unit using the given compiler |
| 567 | /// invocation, replacing the current translation unit. |
| 568 | /// |
| 569 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 570 | /// contain any translation-unit information, false otherwise. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 571 | bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 572 | delete SavedMainFileBuffer; |
| 573 | SavedMainFileBuffer = 0; |
| 574 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 575 | if (!Invocation.get()) |
| 576 | return true; |
| 577 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 578 | // Create the compiler instance to use for building the AST. |
Daniel Dunbar | cb6dda1 | 2009-12-02 08:43:56 +0000 | [diff] [blame] | 579 | CompilerInstance Clang; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 580 | Clang.setInvocation(Invocation.take()); |
| 581 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 582 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 583 | // Set up diagnostics, capturing any diagnostics that would |
| 584 | // otherwise be dropped. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 585 | Clang.setDiagnostics(&getDiagnostics()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 586 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 587 | getDiagnostics(), |
| 588 | StoredDiagnostics); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 589 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 590 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 591 | // Create the target instance. |
| 592 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 593 | Clang.getTargetOpts())); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 594 | if (!Clang.hasTarget()) { |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 595 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 596 | return true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 597 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 598 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 599 | // Inform the target of the language options. |
| 600 | // |
| 601 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 602 | // created. This complexity should be lifted elsewhere. |
| 603 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 604 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 605 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 606 | "Invocation must have exactly one source file!"); |
Daniel Dunbar | c34ce3f | 2010-06-07 23:22:09 +0000 | [diff] [blame] | 607 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 608 | "FIXME: AST inputs not yet supported here!"); |
Daniel Dunbar | faddc3e | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 609 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 610 | "IR inputs not support here!"); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 612 | // Configure the various subsystems. |
| 613 | // FIXME: Should we retain the previous file manager? |
| 614 | FileMgr.reset(new FileManager); |
| 615 | SourceMgr.reset(new SourceManager(getDiagnostics())); |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 616 | TheSema.reset(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 617 | Ctx.reset(); |
| 618 | PP.reset(); |
| 619 | |
| 620 | // Clear out old caches and data. |
| 621 | TopLevelDecls.clear(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 622 | CleanTemporaryFiles(); |
| 623 | PreprocessedEntitiesByFile.clear(); |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 624 | |
| 625 | if (!OverrideMainBuffer) |
| 626 | StoredDiagnostics.clear(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 627 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 628 | // Create a file manager object to provide access to and cache the filesystem. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 629 | Clang.setFileManager(&getFileManager()); |
| 630 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 631 | // Create the source manager. |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 632 | Clang.setSourceManager(&getSourceManager()); |
| 633 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 634 | // If the main file has been overridden due to the use of a preamble, |
| 635 | // make that override happen and introduce the preamble. |
| 636 | PreprocessorOptions &PreprocessorOpts = Clang.getPreprocessorOpts(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 637 | std::string PriorImplicitPCHInclude; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 638 | if (OverrideMainBuffer) { |
| 639 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 640 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 641 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 642 | = PreambleEndsAtStartOfLine; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 643 | PriorImplicitPCHInclude = PreprocessorOpts.ImplicitPCHInclude; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 644 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 645 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 2823342 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 646 | |
| 647 | // Keep track of the override buffer; |
| 648 | SavedMainFileBuffer = OverrideMainBuffer; |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 649 | |
| 650 | // The stored diagnostic has the old source manager in it; update |
| 651 | // the locations to refer into the new source manager. Since we've |
| 652 | // been careful to make sure that the source manager's state |
| 653 | // before and after are identical, so that we can reuse the source |
| 654 | // location itself. |
| 655 | for (unsigned I = 0, N = StoredDiagnostics.size(); I != N; ++I) { |
| 656 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), |
| 657 | getSourceManager()); |
| 658 | StoredDiagnostics[I].setLocation(Loc); |
| 659 | } |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 662 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act; |
| 663 | Act.reset(new TopLevelDeclTrackerAction(*this)); |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 664 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 665 | Clang.getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 666 | goto error; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 667 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 668 | Act->Execute(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 669 | |
Daniel Dunbar | 64a32ba | 2009-12-01 21:57:33 +0000 | [diff] [blame] | 670 | // Steal the created target, context, and preprocessor, and take back the |
| 671 | // source and file managers. |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 672 | TheSema.reset(Clang.takeSema()); |
| 673 | Consumer.reset(Clang.takeASTConsumer()); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 674 | Ctx.reset(Clang.takeASTContext()); |
| 675 | PP.reset(Clang.takePreprocessor()); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 676 | Clang.takeSourceManager(); |
| 677 | Clang.takeFileManager(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 678 | Target.reset(Clang.takeTarget()); |
| 679 | |
Daniel Dunbar | f772d1e | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 680 | Act->EndSourceFile(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 681 | |
| 682 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 683 | if (OverrideMainBuffer) { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 684 | PreprocessorOpts.eraseRemappedFile( |
| 685 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 686 | PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; |
| 687 | } |
| 688 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 689 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 690 | |
| 691 | Invocation.reset(Clang.takeInvocation()); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 692 | |
| 693 | // If we were asked to cache code-completion results and don't have any |
| 694 | // results yet, do so now. |
| 695 | if (ShouldCacheCodeCompletionResults && CachedCompletionResults.empty()) |
| 696 | CacheCodeCompletionResults(); |
| 697 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 698 | return false; |
| 699 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 700 | error: |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 701 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 702 | if (OverrideMainBuffer) { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 703 | PreprocessorOpts.eraseRemappedFile( |
| 704 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 705 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 706 | PreprocessorOpts.ImplicitPCHInclude = PriorImplicitPCHInclude; |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 707 | } |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 708 | |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 709 | Clang.takeSourceManager(); |
| 710 | Clang.takeFileManager(); |
| 711 | Clang.takeDiagnosticClient(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 712 | Invocation.reset(Clang.takeInvocation()); |
| 713 | return true; |
| 714 | } |
| 715 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 716 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 717 | static std::string GetPreamblePCHPath() { |
| 718 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 719 | // it should know how to find the temporary files dir). |
| 720 | // FIXME: This is really lame. I copied this code from the Driver! |
| 721 | std::string Error; |
| 722 | const char *TmpDir = ::getenv("TMPDIR"); |
| 723 | if (!TmpDir) |
| 724 | TmpDir = ::getenv("TEMP"); |
| 725 | if (!TmpDir) |
| 726 | TmpDir = ::getenv("TMP"); |
| 727 | if (!TmpDir) |
| 728 | TmpDir = "/tmp"; |
| 729 | llvm::sys::Path P(TmpDir); |
| 730 | P.appendComponent("preamble"); |
Douglas Gregor | 6bf1830 | 2010-08-11 13:06:56 +0000 | [diff] [blame] | 731 | P.appendSuffix("pch"); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 732 | if (P.createTemporaryFileOnDisk()) |
| 733 | return std::string(); |
| 734 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 735 | return P.str(); |
| 736 | } |
| 737 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 738 | /// \brief Compute the preamble for the main file, providing the source buffer |
| 739 | /// that corresponds to the main file along with a pair (bytes, start-of-line) |
| 740 | /// that describes the preamble. |
| 741 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 742 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, |
| 743 | unsigned MaxLines, bool &CreatedBuffer) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 744 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 745 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 746 | = Invocation.getPreprocessorOpts(); |
| 747 | CreatedBuffer = false; |
| 748 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 749 | // Try to determine if the main file has been remapped, either from the |
| 750 | // command line (to another file) or directly through the compiler invocation |
| 751 | // (to a memory buffer). |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 752 | llvm::MemoryBuffer *Buffer = 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 753 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 754 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 755 | // Check whether there is a file-file remapping of the main file |
| 756 | for (PreprocessorOptions::remapped_file_iterator |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 757 | M = PreprocessorOpts.remapped_file_begin(), |
| 758 | E = PreprocessorOpts.remapped_file_end(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 759 | M != E; |
| 760 | ++M) { |
| 761 | llvm::sys::PathWithStatus MPath(M->first); |
| 762 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 763 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 764 | // We found a remapping. Try to load the resulting, remapped source. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 765 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 766 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 767 | CreatedBuffer = false; |
| 768 | } |
| 769 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 770 | Buffer = llvm::MemoryBuffer::getFile(M->second); |
| 771 | if (!Buffer) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 772 | return std::make_pair((llvm::MemoryBuffer*)0, |
| 773 | std::make_pair(0, true)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 774 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 776 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 777 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 778 | E = PreprocessorOpts.remapped_file_end(); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // Check whether there is a file-buffer remapping. It supercedes the |
| 784 | // file-file remapping. |
| 785 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 786 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 787 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 788 | M != E; |
| 789 | ++M) { |
| 790 | llvm::sys::PathWithStatus MPath(M->first); |
| 791 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 792 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 793 | // We found a remapping. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 794 | if (CreatedBuffer) { |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 795 | delete Buffer; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 796 | CreatedBuffer = false; |
| 797 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 799 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
| 800 | |
| 801 | // Remove this remapping. We've captured the buffer already. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 802 | M = PreprocessorOpts.eraseRemappedFile(M); |
| 803 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 804 | } |
| 805 | } |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 806 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | // If the main source file was not remapped, load it now. |
| 810 | if (!Buffer) { |
| 811 | Buffer = llvm::MemoryBuffer::getFile(FrontendOpts.Inputs[0].second); |
| 812 | if (!Buffer) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 813 | return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 814 | |
| 815 | CreatedBuffer = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 818 | return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, MaxLines)); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 821 | static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, |
| 822 | bool DeleteOld, |
| 823 | unsigned NewSize, |
| 824 | llvm::StringRef NewName) { |
| 825 | llvm::MemoryBuffer *Result |
| 826 | = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); |
| 827 | memcpy(const_cast<char*>(Result->getBufferStart()), |
| 828 | Old->getBufferStart(), Old->getBufferSize()); |
| 829 | memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 830 | ' ', NewSize - Old->getBufferSize() - 1); |
| 831 | const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 832 | |
| 833 | if (DeleteOld) |
| 834 | delete Old; |
| 835 | |
| 836 | return Result; |
| 837 | } |
| 838 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 839 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 840 | /// the source file. |
| 841 | /// |
| 842 | /// This routine will compute the preamble of the main source file. If a |
| 843 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 844 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 845 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 846 | /// this routine will determine if it is still valid and, if so, avoid |
| 847 | /// rebuilding the precompiled preamble. |
| 848 | /// |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 849 | /// \param AllowRebuild When true (the default), this routine is |
| 850 | /// allowed to rebuild the precompiled preamble if it is found to be |
| 851 | /// out-of-date. |
| 852 | /// |
| 853 | /// \param MaxLines When non-zero, the maximum number of lines that |
| 854 | /// can occur within the preamble. |
| 855 | /// |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 856 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 857 | /// buffer that should be used in place of the main file when doing so. |
| 858 | /// Otherwise, returns a NULL pointer. |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 859 | llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( |
| 860 | bool AllowRebuild, |
| 861 | unsigned MaxLines) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 862 | CompilerInvocation PreambleInvocation(*Invocation); |
| 863 | FrontendOptions &FrontendOpts = PreambleInvocation.getFrontendOpts(); |
| 864 | PreprocessorOptions &PreprocessorOpts |
| 865 | = PreambleInvocation.getPreprocessorOpts(); |
| 866 | |
| 867 | bool CreatedPreambleBuffer = false; |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 868 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 869 | = ComputePreamble(PreambleInvocation, MaxLines, CreatedPreambleBuffer); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 870 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 871 | if (!NewPreamble.second.first) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 872 | // We couldn't find a preamble in the main source. Clear out the current |
| 873 | // preamble, if we have one. It's obviously no good any more. |
| 874 | Preamble.clear(); |
| 875 | if (!PreambleFile.empty()) { |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 876 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 877 | PreambleFile.clear(); |
| 878 | } |
| 879 | if (CreatedPreambleBuffer) |
| 880 | delete NewPreamble.first; |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 881 | |
| 882 | // The next time we actually see a preamble, precompile it. |
| 883 | PreambleRebuildCounter = 1; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 884 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | if (!Preamble.empty()) { |
| 888 | // We've previously computed a preamble. Check whether we have the same |
| 889 | // preamble now that we did before, and that there's enough space in |
| 890 | // the main-file buffer within the precompiled preamble to fit the |
| 891 | // new main file. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 892 | if (Preamble.size() == NewPreamble.second.first && |
| 893 | PreambleEndsAtStartOfLine == NewPreamble.second.second && |
Douglas Gregor | 592508e | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 894 | NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 895 | memcmp(&Preamble[0], NewPreamble.first->getBufferStart(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 896 | NewPreamble.second.first) == 0) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 897 | // The preamble has not changed. We may be able to re-use the precompiled |
| 898 | // preamble. |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 900 | // Check that none of the files used by the preamble have changed. |
| 901 | bool AnyFileChanged = false; |
| 902 | |
| 903 | // First, make a record of those files that have been overridden via |
| 904 | // remapping or unsaved_files. |
| 905 | llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; |
| 906 | for (PreprocessorOptions::remapped_file_iterator |
| 907 | R = PreprocessorOpts.remapped_file_begin(), |
| 908 | REnd = PreprocessorOpts.remapped_file_end(); |
| 909 | !AnyFileChanged && R != REnd; |
| 910 | ++R) { |
| 911 | struct stat StatBuf; |
| 912 | if (stat(R->second.c_str(), &StatBuf)) { |
| 913 | // If we can't stat the file we're remapping to, assume that something |
| 914 | // horrible happened. |
| 915 | AnyFileChanged = true; |
| 916 | break; |
| 917 | } |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 919 | OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, |
| 920 | StatBuf.st_mtime); |
| 921 | } |
| 922 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 923 | R = PreprocessorOpts.remapped_file_buffer_begin(), |
| 924 | REnd = PreprocessorOpts.remapped_file_buffer_end(); |
| 925 | !AnyFileChanged && R != REnd; |
| 926 | ++R) { |
| 927 | // FIXME: Should we actually compare the contents of file->buffer |
| 928 | // remappings? |
| 929 | OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), |
| 930 | 0); |
| 931 | } |
| 932 | |
| 933 | // Check whether anything has changed. |
| 934 | for (llvm::StringMap<std::pair<off_t, time_t> >::iterator |
| 935 | F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); |
| 936 | !AnyFileChanged && F != FEnd; |
| 937 | ++F) { |
| 938 | llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden |
| 939 | = OverriddenFiles.find(F->first()); |
| 940 | if (Overridden != OverriddenFiles.end()) { |
| 941 | // This file was remapped; check whether the newly-mapped file |
| 942 | // matches up with the previous mapping. |
| 943 | if (Overridden->second != F->second) |
| 944 | AnyFileChanged = true; |
| 945 | continue; |
| 946 | } |
| 947 | |
| 948 | // The file was not remapped; check whether it has changed on disk. |
| 949 | struct stat StatBuf; |
| 950 | if (stat(F->first(), &StatBuf)) { |
| 951 | // If we can't stat the file, assume that something horrible happened. |
| 952 | AnyFileChanged = true; |
| 953 | } else if (StatBuf.st_size != F->second.first || |
| 954 | StatBuf.st_mtime != F->second.second) |
| 955 | AnyFileChanged = true; |
| 956 | } |
| 957 | |
| 958 | if (!AnyFileChanged) { |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 959 | // Okay! We can re-use the precompiled preamble. |
| 960 | |
| 961 | // Set the state of the diagnostic object to mimic its state |
| 962 | // after parsing the preamble. |
| 963 | getDiagnostics().Reset(); |
| 964 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
| 965 | if (StoredDiagnostics.size() > NumStoredDiagnosticsInPreamble) |
| 966 | StoredDiagnostics.erase( |
| 967 | StoredDiagnostics.begin() + NumStoredDiagnosticsInPreamble, |
| 968 | StoredDiagnostics.end()); |
| 969 | |
| 970 | // Create a version of the main file buffer that is padded to |
| 971 | // buffer size we reserved when creating the preamble. |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 972 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 973 | CreatedPreambleBuffer, |
| 974 | PreambleReservedSize, |
| 975 | FrontendOpts.Inputs[0].second); |
| 976 | } |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 977 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 978 | |
| 979 | // If we aren't allowed to rebuild the precompiled preamble, just |
| 980 | // return now. |
| 981 | if (!AllowRebuild) |
| 982 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 983 | |
| 984 | // We can't reuse the previously-computed preamble. Build a new one. |
| 985 | Preamble.clear(); |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 986 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 987 | PreambleRebuildCounter = 1; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 988 | } else if (!AllowRebuild) { |
| 989 | // We aren't allowed to rebuild the precompiled preamble; just |
| 990 | // return now. |
| 991 | return 0; |
| 992 | } |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 993 | |
| 994 | // If the preamble rebuild counter > 1, it's because we previously |
| 995 | // failed to build a preamble and we're not yet ready to try |
| 996 | // again. Decrement the counter and return a failure. |
| 997 | if (PreambleRebuildCounter > 1) { |
| 998 | --PreambleRebuildCounter; |
| 999 | return 0; |
| 1000 | } |
| 1001 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1002 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1003 | llvm::Timer *PreambleTimer = 0; |
| 1004 | if (TimerGroup.get()) { |
| 1005 | PreambleTimer = new llvm::Timer("Precompiling preamble", *TimerGroup); |
| 1006 | PreambleTimer->startTimer(); |
| 1007 | Timers.push_back(PreambleTimer); |
| 1008 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1009 | |
| 1010 | // Create a new buffer that stores the preamble. The buffer also contains |
| 1011 | // extra space for the original contents of the file (which will be present |
| 1012 | // when we actually parse the file) along with more room in case the file |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1013 | // grows. |
| 1014 | PreambleReservedSize = NewPreamble.first->getBufferSize(); |
| 1015 | if (PreambleReservedSize < 4096) |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1016 | PreambleReservedSize = 8191; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1017 | else |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1018 | PreambleReservedSize *= 2; |
| 1019 | |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1020 | // Save the preamble text for later; we'll need to compare against it for |
| 1021 | // subsequent reparses. |
| 1022 | Preamble.assign(NewPreamble.first->getBufferStart(), |
| 1023 | NewPreamble.first->getBufferStart() |
| 1024 | + NewPreamble.second.first); |
| 1025 | PreambleEndsAtStartOfLine = NewPreamble.second.second; |
| 1026 | |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1027 | llvm::MemoryBuffer *PreambleBuffer |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1028 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1029 | FrontendOpts.Inputs[0].second); |
| 1030 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1031 | NewPreamble.first->getBufferStart(), Preamble.size()); |
| 1032 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1033 | ' ', PreambleReservedSize - Preamble.size() - 1); |
| 1034 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1035 | |
| 1036 | // Remap the main source file to the preamble buffer. |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1037 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1038 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 1039 | |
| 1040 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 1041 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
Sebastian Redl | f65339e | 2010-08-06 00:35:11 +0000 | [diff] [blame] | 1042 | // FIXME: Set ChainedPCH unconditionally, once it is ready. |
| 1043 | if (::getenv("LIBCLANG_CHAINING")) |
| 1044 | FrontendOpts.ChainedPCH = true; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1045 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1046 | FrontendOpts.OutputFile = GetPreamblePCHPath(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1047 | |
| 1048 | // Create the compiler instance to use for building the precompiled preamble. |
| 1049 | CompilerInstance Clang; |
| 1050 | Clang.setInvocation(&PreambleInvocation); |
| 1051 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 1052 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1053 | // Set up diagnostics, capturing all of the diagnostics produced. |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1054 | Clang.setDiagnostics(&getDiagnostics()); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1055 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, |
| 1056 | getDiagnostics(), |
| 1057 | StoredDiagnostics); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1058 | Clang.setDiagnosticClient(getDiagnostics().getClient()); |
| 1059 | |
| 1060 | // Create the target instance. |
| 1061 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 1062 | Clang.getTargetOpts())); |
| 1063 | if (!Clang.hasTarget()) { |
| 1064 | Clang.takeDiagnosticClient(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1065 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1066 | Preamble.clear(); |
| 1067 | if (CreatedPreambleBuffer) |
| 1068 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1069 | if (PreambleTimer) |
| 1070 | PreambleTimer->stopTimer(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1071 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1072 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | // Inform the target of the language options. |
| 1076 | // |
| 1077 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1078 | // created. This complexity should be lifted elsewhere. |
| 1079 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 1080 | |
| 1081 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 1082 | "Invocation must have exactly one source file!"); |
| 1083 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 1084 | "FIXME: AST inputs not yet supported here!"); |
| 1085 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 1086 | "IR inputs not support here!"); |
| 1087 | |
| 1088 | // Clear out old caches and data. |
| 1089 | StoredDiagnostics.clear(); |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1090 | TopLevelDecls.clear(); |
| 1091 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Create a file manager object to provide access to and cache the filesystem. |
| 1094 | Clang.setFileManager(new FileManager); |
| 1095 | |
| 1096 | // Create the source manager. |
| 1097 | Clang.setSourceManager(new SourceManager(getDiagnostics())); |
| 1098 | |
Douglas Gregor | 1d715ac | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 1099 | llvm::OwningPtr<PrecompilePreambleAction> Act; |
| 1100 | Act.reset(new PrecompilePreambleAction(*this)); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1101 | if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 1102 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 1103 | Clang.takeDiagnosticClient(); |
| 1104 | Clang.takeInvocation(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1105 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1106 | Preamble.clear(); |
| 1107 | if (CreatedPreambleBuffer) |
| 1108 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1109 | if (PreambleTimer) |
| 1110 | PreambleTimer->stopTimer(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1111 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1112 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1113 | return 0; |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | Act->Execute(); |
| 1117 | Act->EndSourceFile(); |
| 1118 | Clang.takeDiagnosticClient(); |
| 1119 | Clang.takeInvocation(); |
| 1120 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1121 | if (Diagnostics->hasErrorOccurred()) { |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1122 | // There were errors parsing the preamble, so no precompiled header was |
| 1123 | // generated. Forget that we even tried. |
| 1124 | // FIXME: Should we leave a note for ourselves to try again? |
| 1125 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1126 | Preamble.clear(); |
| 1127 | if (CreatedPreambleBuffer) |
| 1128 | delete NewPreamble.first; |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1129 | if (PreambleTimer) |
| 1130 | PreambleTimer->stopTimer(); |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1131 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1132 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1133 | return 0; |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | // Keep track of the preamble we precompiled. |
| 1137 | PreambleFile = FrontendOpts.OutputFile; |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1138 | NumStoredDiagnosticsInPreamble = StoredDiagnostics.size(); |
| 1139 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Keep track of all of the files that the source manager knows about, |
| 1142 | // so we can verify whether they have changed or not. |
| 1143 | FilesInPreamble.clear(); |
| 1144 | SourceManager &SourceMgr = Clang.getSourceManager(); |
| 1145 | const llvm::MemoryBuffer *MainFileBuffer |
| 1146 | = SourceMgr.getBuffer(SourceMgr.getMainFileID()); |
| 1147 | for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), |
| 1148 | FEnd = SourceMgr.fileinfo_end(); |
| 1149 | F != FEnd; |
| 1150 | ++F) { |
| 1151 | const FileEntry *File = F->second->Entry; |
| 1152 | if (!File || F->second->getRawBuffer() == MainFileBuffer) |
| 1153 | continue; |
| 1154 | |
| 1155 | FilesInPreamble[File->getName()] |
| 1156 | = std::make_pair(F->second->getSize(), File->getModificationTime()); |
| 1157 | } |
| 1158 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1159 | if (PreambleTimer) |
| 1160 | PreambleTimer->stopTimer(); |
| 1161 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1162 | PreambleRebuildCounter = 1; |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1163 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
| 1164 | CreatedPreambleBuffer, |
| 1165 | PreambleReservedSize, |
| 1166 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1167 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | eb8837b | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1169 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
| 1170 | std::vector<Decl *> Resolved; |
| 1171 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 1172 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
| 1173 | for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { |
| 1174 | // Resolve the declaration ID to an actual declaration, possibly |
| 1175 | // deserializing the declaration in the process. |
| 1176 | Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); |
| 1177 | if (D) |
| 1178 | Resolved.push_back(D); |
| 1179 | } |
| 1180 | TopLevelDeclsInPreamble.clear(); |
| 1181 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 1182 | } |
| 1183 | |
| 1184 | unsigned ASTUnit::getMaxPCHLevel() const { |
| 1185 | if (!getOnlyLocalDecls()) |
| 1186 | return Decl::MaxPCHLevel; |
| 1187 | |
| 1188 | unsigned Result = 0; |
| 1189 | if (isMainFileAST() || SavedMainFileBuffer) |
| 1190 | ++Result; |
| 1191 | return Result; |
| 1192 | } |
| 1193 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1194 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
| 1195 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
| 1196 | bool OnlyLocalDecls, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1197 | bool CaptureDiagnostics, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1198 | bool PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1199 | bool CompleteTranslationUnit, |
| 1200 | bool CacheCodeCompletionResults) { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1201 | if (!Diags.getPtr()) { |
| 1202 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1203 | // with the default options. |
| 1204 | DiagnosticOptions DiagOpts; |
| 1205 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
| 1206 | } |
| 1207 | |
| 1208 | // Create the AST unit. |
| 1209 | llvm::OwningPtr<ASTUnit> AST; |
| 1210 | AST.reset(new ASTUnit(false)); |
| 1211 | AST->Diagnostics = Diags; |
| 1212 | AST->CaptureDiagnostics = CaptureDiagnostics; |
| 1213 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1214 | AST->CompleteTranslationUnit = CompleteTranslationUnit; |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1215 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1216 | AST->Invocation.reset(CI); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1217 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1219 | if (getenv("LIBCLANG_TIMING")) |
| 1220 | AST->TimerGroup.reset( |
| 1221 | new llvm::TimerGroup(CI->getFrontendOpts().Inputs[0].second)); |
| 1222 | |
| 1223 | |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1224 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | fd0b870 | 2010-07-28 22:12:37 +0000 | [diff] [blame] | 1225 | // FIXME: When C++ PCH is ready, allow use of it for a precompiled preamble. |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1226 | if (PrecompilePreamble && !CI->getLangOpts().CPlusPlus) { |
| 1227 | AST->PreambleRebuildCounter = 1; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1228 | OverrideMainBuffer = AST->getMainBufferWithPrecompiledPreamble(); |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1229 | } |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1230 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1231 | llvm::Timer *ParsingTimer = 0; |
| 1232 | if (AST->TimerGroup.get()) { |
| 1233 | ParsingTimer = new llvm::Timer("Initial parse", *AST->TimerGroup); |
| 1234 | ParsingTimer->startTimer(); |
| 1235 | AST->Timers.push_back(ParsingTimer); |
| 1236 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1237 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1238 | bool Failed = AST->Parse(OverrideMainBuffer); |
| 1239 | if (ParsingTimer) |
| 1240 | ParsingTimer->stopTimer(); |
| 1241 | |
| 1242 | return Failed? 0 : AST.take(); |
Daniel Dunbar | 521bf9c | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1243 | } |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1244 | |
| 1245 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 1246 | const char **ArgEnd, |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1247 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags, |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1248 | llvm::StringRef ResourceFilesPath, |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1249 | bool OnlyLocalDecls, |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1250 | RemappedFile *RemappedFiles, |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1251 | unsigned NumRemappedFiles, |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1252 | bool CaptureDiagnostics, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1253 | bool PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1254 | bool CompleteTranslationUnit, |
| 1255 | bool CacheCodeCompletionResults) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1256 | if (!Diags.getPtr()) { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1257 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1258 | // with the default options. |
| 1259 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1260 | Diags = CompilerInstance::createDiagnostics(DiagOpts, 0, 0); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1261 | } |
| 1262 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1263 | llvm::SmallVector<const char *, 16> Args; |
| 1264 | Args.push_back("<clang>"); // FIXME: Remove dummy argument. |
| 1265 | Args.insert(Args.end(), ArgBegin, ArgEnd); |
| 1266 | |
| 1267 | // FIXME: Find a cleaner way to force the driver into restricted modes. We |
| 1268 | // also want to force it to use clang. |
| 1269 | Args.push_back("-fsyntax-only"); |
| 1270 | |
Daniel Dunbar | 869824e | 2009-12-13 03:46:13 +0000 | [diff] [blame] | 1271 | // FIXME: We shouldn't have to pass in the path info. |
Daniel Dunbar | 0bbad51 | 2010-07-19 00:44:04 +0000 | [diff] [blame] | 1272 | driver::Driver TheDriver("clang", llvm::sys::getHostTriple(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1273 | "a.out", false, false, *Diags); |
Daniel Dunbar | 3bd54cc | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1274 | |
| 1275 | // Don't check that inputs exist, they have been remapped. |
| 1276 | TheDriver.setCheckInputsExist(false); |
| 1277 | |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1278 | llvm::OwningPtr<driver::Compilation> C( |
| 1279 | TheDriver.BuildCompilation(Args.size(), Args.data())); |
| 1280 | |
| 1281 | // We expect to get back exactly one command job, if we didn't something |
| 1282 | // failed. |
| 1283 | const driver::JobList &Jobs = C->getJobs(); |
| 1284 | if (Jobs.size() != 1 || !isa<driver::Command>(Jobs.begin())) { |
| 1285 | llvm::SmallString<256> Msg; |
| 1286 | llvm::raw_svector_ostream OS(Msg); |
| 1287 | C->PrintJob(OS, C->getJobs(), "; ", true); |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1288 | Diags->Report(diag::err_fe_expected_compiler_job) << OS.str(); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin()); |
| 1293 | if (llvm::StringRef(Cmd->getCreator().getName()) != "clang") { |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1294 | Diags->Report(diag::err_fe_expected_clang_command); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | const driver::ArgStringList &CCArgs = Cmd->getArguments(); |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1299 | llvm::OwningPtr<CompilerInvocation> CI(new CompilerInvocation); |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 1300 | CompilerInvocation::CreateFromArgs(*CI, |
| 1301 | const_cast<const char **>(CCArgs.data()), |
| 1302 | const_cast<const char **>(CCArgs.data()) + |
Douglas Gregor | 44c181a | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1303 | CCArgs.size(), |
Douglas Gregor | 3687e9d | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1304 | *Diags); |
Daniel Dunbar | 1e69fe3 | 2009-12-13 03:45:58 +0000 | [diff] [blame] | 1305 | |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1306 | // Override any files that need remapping |
| 1307 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1308 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 1309 | RemappedFiles[I].second); |
Douglas Gregor | 4db64a4 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1310 | |
Daniel Dunbar | 8b9adfe | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1311 | // Override the resources path. |
Daniel Dunbar | 807b061 | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1312 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1313 | |
Daniel Dunbar | b26d483 | 2010-02-16 01:55:04 +0000 | [diff] [blame] | 1314 | CI->getFrontendOpts().DisableFree = true; |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1315 | return LoadFromCompilerInvocation(CI.take(), Diags, OnlyLocalDecls, |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1316 | CaptureDiagnostics, PrecompilePreamble, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1317 | CompleteTranslationUnit, |
| 1318 | CacheCodeCompletionResults); |
Daniel Dunbar | 7b55668 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1319 | } |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1320 | |
| 1321 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
| 1322 | if (!Invocation.get()) |
| 1323 | return true; |
| 1324 | |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1325 | llvm::Timer *ReparsingTimer = 0; |
| 1326 | if (TimerGroup.get()) { |
| 1327 | ReparsingTimer = new llvm::Timer("Reparse", *TimerGroup); |
| 1328 | ReparsingTimer->startTimer(); |
| 1329 | Timers.push_back(ReparsingTimer); |
| 1330 | } |
| 1331 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1332 | // Remap files. |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1333 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
| 1334 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 1335 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 1336 | RemappedFiles[I].second); |
| 1337 | |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1338 | // If we have a preamble file lying around, or if we might try to |
| 1339 | // build a precompiled preamble, do so now. |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1340 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | eababfb | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1341 | if (!PreambleFile.empty() || PreambleRebuildCounter > 0) |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1342 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1343 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1344 | // Clear out the diagnostics state. |
Douglas Gregor | c0659ec | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1345 | if (!OverrideMainBuffer) |
| 1346 | getDiagnostics().Reset(); |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1347 | |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1348 | // Parse the sources |
Douglas Gregor | 754f349 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1349 | bool Result = Parse(OverrideMainBuffer); |
Douglas Gregor | 385103b | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1350 | if (ReparsingTimer) |
| 1351 | ReparsingTimer->stopTimer(); |
Douglas Gregor | 175c4a9 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1352 | return Result; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1353 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1355 | //----------------------------------------------------------------------------// |
| 1356 | // Code completion |
| 1357 | //----------------------------------------------------------------------------// |
| 1358 | |
| 1359 | namespace { |
| 1360 | /// \brief Code completion consumer that combines the cached code-completion |
| 1361 | /// results from an ASTUnit with the code-completion results provided to it, |
| 1362 | /// then passes the result on to |
| 1363 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
| 1364 | unsigned NormalContexts; |
| 1365 | ASTUnit &AST; |
| 1366 | CodeCompleteConsumer &Next; |
| 1367 | |
| 1368 | public: |
| 1369 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1370 | bool IncludeMacros, bool IncludeCodePatterns, |
| 1371 | bool IncludeGlobals) |
| 1372 | : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals, |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1373 | Next.isOutputBinary()), AST(AST), Next(Next) |
| 1374 | { |
| 1375 | // Compute the set of contexts in which we will look when we don't have |
| 1376 | // any information about the specific context. |
| 1377 | NormalContexts |
| 1378 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 1379 | | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 1380 | | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) |
| 1381 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 1382 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 1383 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 1384 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 1385 | | (1 << (CodeCompletionContext::CCC_MemberAccess - 1)) |
| 1386 | | (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)); |
| 1387 | |
| 1388 | if (AST.getASTContext().getLangOptions().CPlusPlus) |
| 1389 | NormalContexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 1390 | | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 1391 | | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
| 1392 | } |
| 1393 | |
| 1394 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 1395 | CodeCompletionContext Context, |
| 1396 | Result *Results, |
| 1397 | unsigned NumResults) { |
| 1398 | // Merge the results we were given with the results we cached. |
| 1399 | bool AddedResult = false; |
| 1400 | unsigned InContexts = |
| 1401 | (Context.getKind() == CodeCompletionContext::CCC_Other? NormalContexts |
| 1402 | : (1 << (Context.getKind() - 1))); |
| 1403 | typedef CodeCompleteConsumer::Result Result; |
| 1404 | llvm::SmallVector<Result, 8> AllResults; |
| 1405 | for (ASTUnit::cached_completion_iterator |
| 1406 | C = AST.cached_completion_begin(), |
| 1407 | CEnd = AST.cached_completion_end(); |
| 1408 | C != CEnd; ++C) { |
| 1409 | // If the context we are in matches any of the contexts we are |
| 1410 | // interested in, we'll add this result. |
| 1411 | if ((C->ShowInContexts & InContexts) == 0) |
| 1412 | continue; |
| 1413 | |
| 1414 | // If we haven't added any results previously, do so now. |
| 1415 | if (!AddedResult) { |
| 1416 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
| 1417 | AddedResult = true; |
| 1418 | } |
| 1419 | |
| 1420 | AllResults.push_back(Result(C->Completion, C->Priority, C->Kind)); |
| 1421 | } |
| 1422 | |
| 1423 | // If we did not add any cached completion results, just forward the |
| 1424 | // results we were given to the next consumer. |
| 1425 | if (!AddedResult) { |
| 1426 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
| 1427 | return; |
| 1428 | } |
| 1429 | |
| 1430 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
| 1431 | AllResults.size()); |
| 1432 | } |
| 1433 | |
| 1434 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 1435 | OverloadCandidate *Candidates, |
| 1436 | unsigned NumCandidates) { |
| 1437 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); |
| 1438 | } |
| 1439 | }; |
| 1440 | } |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1441 | void ASTUnit::CodeComplete(llvm::StringRef File, unsigned Line, unsigned Column, |
| 1442 | RemappedFile *RemappedFiles, |
| 1443 | unsigned NumRemappedFiles, |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1444 | bool IncludeMacros, |
| 1445 | bool IncludeCodePatterns, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1446 | CodeCompleteConsumer &Consumer, |
| 1447 | Diagnostic &Diag, LangOptions &LangOpts, |
| 1448 | SourceManager &SourceMgr, FileManager &FileMgr, |
| 1449 | llvm::SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics) { |
| 1450 | if (!Invocation.get()) |
| 1451 | return; |
| 1452 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1453 | llvm::Timer *CompletionTimer = 0; |
| 1454 | if (TimerGroup.get()) { |
| 1455 | llvm::SmallString<128> TimerName; |
| 1456 | llvm::raw_svector_ostream TimerNameOut(TimerName); |
| 1457 | TimerNameOut << "Code completion @ " << File << ":" << Line << ":" |
| 1458 | << Column; |
| 1459 | CompletionTimer = new llvm::Timer(TimerNameOut.str(), *TimerGroup); |
| 1460 | CompletionTimer->startTimer(); |
| 1461 | Timers.push_back(CompletionTimer); |
| 1462 | } |
| 1463 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1464 | CompilerInvocation CCInvocation(*Invocation); |
| 1465 | FrontendOptions &FrontendOpts = CCInvocation.getFrontendOpts(); |
| 1466 | PreprocessorOptions &PreprocessorOpts = CCInvocation.getPreprocessorOpts(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1468 | FrontendOpts.ShowMacrosInCodeCompletion |
| 1469 | = IncludeMacros && CachedCompletionResults.empty(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1470 | FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns; |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1471 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion |
| 1472 | = CachedCompletionResults.empty(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1473 | FrontendOpts.CodeCompletionAt.FileName = File; |
| 1474 | FrontendOpts.CodeCompletionAt.Line = Line; |
| 1475 | FrontendOpts.CodeCompletionAt.Column = Column; |
| 1476 | |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1477 | // Turn on spell-checking when performing code completion. It leads |
| 1478 | // to better results. |
| 1479 | unsigned SpellChecking = CCInvocation.getLangOpts().SpellChecking; |
| 1480 | CCInvocation.getLangOpts().SpellChecking = 1; |
| 1481 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1482 | // Set the language options appropriately. |
| 1483 | LangOpts = CCInvocation.getLangOpts(); |
| 1484 | |
| 1485 | CompilerInstance Clang; |
| 1486 | Clang.setInvocation(&CCInvocation); |
| 1487 | OriginalSourceFile = Clang.getFrontendOpts().Inputs[0].second; |
| 1488 | |
| 1489 | // Set up diagnostics, capturing any diagnostics produced. |
| 1490 | Clang.setDiagnostics(&Diag); |
| 1491 | CaptureDroppedDiagnostics Capture(true, |
| 1492 | Clang.getDiagnostics(), |
| 1493 | StoredDiagnostics); |
| 1494 | Clang.setDiagnosticClient(Diag.getClient()); |
| 1495 | |
| 1496 | // Create the target instance. |
| 1497 | Clang.setTarget(TargetInfo::CreateTargetInfo(Clang.getDiagnostics(), |
| 1498 | Clang.getTargetOpts())); |
| 1499 | if (!Clang.hasTarget()) { |
| 1500 | Clang.takeDiagnosticClient(); |
| 1501 | Clang.takeInvocation(); |
| 1502 | } |
| 1503 | |
| 1504 | // Inform the target of the language options. |
| 1505 | // |
| 1506 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1507 | // created. This complexity should be lifted elsewhere. |
| 1508 | Clang.getTarget().setForcedLangOptions(Clang.getLangOpts()); |
| 1509 | |
| 1510 | assert(Clang.getFrontendOpts().Inputs.size() == 1 && |
| 1511 | "Invocation must have exactly one source file!"); |
| 1512 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && |
| 1513 | "FIXME: AST inputs not yet supported here!"); |
| 1514 | assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 1515 | "IR inputs not support here!"); |
| 1516 | |
| 1517 | |
| 1518 | // Use the source and file managers that we were given. |
| 1519 | Clang.setFileManager(&FileMgr); |
| 1520 | Clang.setSourceManager(&SourceMgr); |
| 1521 | |
| 1522 | // Remap files. |
| 1523 | PreprocessorOpts.clearRemappedFiles(); |
Douglas Gregor | b75d3df | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 1524 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1525 | for (unsigned I = 0; I != NumRemappedFiles; ++I) |
| 1526 | PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, |
| 1527 | RemappedFiles[I].second); |
| 1528 | |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1529 | // Use the code completion consumer we were given, but adding any cached |
| 1530 | // code-completion results. |
| 1531 | AugmentedCodeCompleteConsumer |
| 1532 | AugmentedConsumer(*this, Consumer, FrontendOpts.ShowMacrosInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1533 | FrontendOpts.ShowCodePatternsInCodeCompletion, |
| 1534 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion); |
Douglas Gregor | 87c08a5 | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1535 | Clang.setCodeCompletionConsumer(&AugmentedConsumer); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1536 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1537 | // If we have a precompiled preamble, try to use it. We only allow |
| 1538 | // the use of the precompiled preamble if we're if the completion |
| 1539 | // point is within the main file, after the end of the precompiled |
| 1540 | // preamble. |
| 1541 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
| 1542 | if (!PreambleFile.empty()) { |
| 1543 | using llvm::sys::FileStatus; |
| 1544 | llvm::sys::PathWithStatus CompleteFilePath(File); |
| 1545 | llvm::sys::PathWithStatus MainPath(OriginalSourceFile); |
| 1546 | if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus()) |
| 1547 | if (const FileStatus *MainStatus = MainPath.getFileStatus()) |
| 1548 | if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID()) |
| 1549 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(false, |
| 1550 | Line); |
| 1551 | } |
| 1552 | |
| 1553 | // If the main file has been overridden due to the use of a preamble, |
| 1554 | // make that override happen and introduce the preamble. |
| 1555 | if (OverrideMainBuffer) { |
| 1556 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 1557 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 1558 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 1559 | = PreambleEndsAtStartOfLine; |
| 1560 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
| 1561 | PreprocessorOpts.DisablePCHValidation = true; |
| 1562 | |
| 1563 | // The stored diagnostics have the old source manager. Copy them |
| 1564 | // to our output set of stored diagnostics, updating the source |
| 1565 | // manager to the one we were given. |
| 1566 | for (unsigned I = 0, N = this->StoredDiagnostics.size(); I != N; ++I) { |
| 1567 | StoredDiagnostics.push_back(this->StoredDiagnostics[I]); |
| 1568 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), SourceMgr); |
| 1569 | StoredDiagnostics[I].setLocation(Loc); |
| 1570 | } |
| 1571 | } |
| 1572 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1573 | llvm::OwningPtr<SyntaxOnlyAction> Act; |
| 1574 | Act.reset(new SyntaxOnlyAction); |
| 1575 | if (Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, |
| 1576 | Clang.getFrontendOpts().Inputs[0].first)) { |
| 1577 | Act->Execute(); |
| 1578 | Act->EndSourceFile(); |
| 1579 | } |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (CompletionTimer) |
| 1582 | CompletionTimer->stopTimer(); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1583 | |
| 1584 | // Steal back our resources. |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1585 | delete OverrideMainBuffer; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1586 | Clang.takeFileManager(); |
| 1587 | Clang.takeSourceManager(); |
| 1588 | Clang.takeInvocation(); |
| 1589 | Clang.takeDiagnosticClient(); |
| 1590 | Clang.takeCodeCompletionConsumer(); |
Douglas Gregor | cee235c | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 1591 | CCInvocation.getLangOpts().SpellChecking = SpellChecking; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1592 | } |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1593 | |
| 1594 | bool ASTUnit::Save(llvm::StringRef File) { |
| 1595 | if (getDiagnostics().hasErrorOccurred()) |
| 1596 | return true; |
| 1597 | |
| 1598 | // FIXME: Can we somehow regenerate the stat cache here, or do we need to |
| 1599 | // unconditionally create a stat cache when we parse the file? |
| 1600 | std::string ErrorInfo; |
Benjamin Kramer | 1395c5d | 2010-08-15 16:54:31 +0000 | [diff] [blame^] | 1601 | llvm::raw_fd_ostream Out(File.str().c_str(), ErrorInfo, |
| 1602 | llvm::raw_fd_ostream::F_Binary); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1603 | if (!ErrorInfo.empty() || Out.has_error()) |
| 1604 | return true; |
| 1605 | |
| 1606 | std::vector<unsigned char> Buffer; |
| 1607 | llvm::BitstreamWriter Stream(Buffer); |
| 1608 | PCHWriter Writer(Stream); |
| 1609 | Writer.WritePCH(getSema(), 0, 0); |
| 1610 | |
| 1611 | // Write the generated bitstream to "Out". |
| 1612 | Out.write((char *)&Buffer.front(), Buffer.size()); |
Douglas Gregor | 7ae2faa | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 1613 | Out.close(); |
| 1614 | return Out.has_error(); |
| 1615 | } |