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