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