Argyrios Kyrtzidis | 3a08ec1 | 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 | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/ASTUnit.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTConsumer.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclVisitor.h" |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeOrdering.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Daniel Dunbar | 55a17b6 | 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" |
Argyrios Kyrtzidis | bc1f48f | 2011-03-07 22:45:01 +0000 | [diff] [blame] | 23 | #include "clang/Driver/ArgList.h" |
| 24 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 25 | #include "clang/Driver/Tool.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CompilerInstance.h" |
| 27 | #include "clang/Frontend/FrontendActions.h" |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/FrontendOptions.h" |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | f5b1346 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 31 | #include "clang/Serialization/ASTReader.h" |
Sebastian Redl | 1914c6f | 2010-08-18 23:56:37 +0000 | [diff] [blame] | 32 | #include "clang/Serialization/ASTWriter.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 33 | #include "clang/Lex/HeaderSearch.h" |
| 34 | #include "clang/Lex/Preprocessor.h" |
Daniel Dunbar | b9bbd54 | 2009-11-15 06:48:46 +0000 | [diff] [blame] | 35 | #include "clang/Basic/TargetOptions.h" |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 36 | #include "clang/Basic/TargetInfo.h" |
| 37 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | ce6c42f | 2011-03-23 04:04:01 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/ArrayRef.h" |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/StringExtras.h" |
Douglas Gregor | 40a5a7d | 2010-08-16 23:08:34 +0000 | [diff] [blame] | 40 | #include "llvm/ADT/StringSet.h" |
Douglas Gregor | 9aeaa4d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Atomic.h" |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 42 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 8aaf499 | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Host.h" |
| 44 | #include "llvm/Support/Path.h" |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Timer.h" |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 47 | #include "llvm/Support/FileSystem.h" |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Mutex.h" |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CrashRecoveryContext.h" |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 50 | #include <cstdlib> |
Zhongxing Xu | 318e403 | 2010-07-23 02:15:08 +0000 | [diff] [blame] | 51 | #include <cstdio> |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 52 | #include <sys/stat.h> |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 53 | using namespace clang; |
| 54 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 55 | using llvm::TimeRecord; |
| 56 | |
| 57 | namespace { |
| 58 | class SimpleTimer { |
| 59 | bool WantTiming; |
| 60 | TimeRecord Start; |
| 61 | std::string Output; |
| 62 | |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 63 | public: |
Douglas Gregor | 1cbdd95 | 2010-11-01 13:48:43 +0000 | [diff] [blame] | 64 | explicit SimpleTimer(bool WantTiming) : WantTiming(WantTiming) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 65 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 66 | Start = TimeRecord::getCurrentTime(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 69 | void setOutput(const Twine &Output) { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 70 | if (WantTiming) |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 71 | this->Output = Output.str(); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 74 | ~SimpleTimer() { |
| 75 | if (WantTiming) { |
| 76 | TimeRecord Elapsed = TimeRecord::getCurrentTime(); |
| 77 | Elapsed -= Start; |
| 78 | llvm::errs() << Output << ':'; |
| 79 | Elapsed.print(Elapsed, llvm::errs()); |
| 80 | llvm::errs() << '\n'; |
| 81 | } |
| 82 | } |
| 83 | }; |
| 84 | } |
| 85 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 86 | /// \brief After failing to build a precompiled preamble (due to |
| 87 | /// errors in the source that occurs in the preamble), the number of |
| 88 | /// reparses during which we'll skip even trying to precompile the |
| 89 | /// preamble. |
| 90 | const unsigned DefaultPreambleRebuildInterval = 5; |
| 91 | |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 92 | /// \brief Tracks the number of ASTUnit objects that are currently active. |
| 93 | /// |
| 94 | /// Used for debugging purposes only. |
Douglas Gregor | 9aeaa4d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 95 | static llvm::sys::cas_flag ActiveASTUnitObjects; |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 97 | ASTUnit::ASTUnit(bool _MainFileIsAST) |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 98 | : OnlyLocalDecls(false), CaptureDiagnostics(false), |
| 99 | MainFileIsAST(_MainFileIsAST), |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 100 | TUKind(TU_Complete), WantTiming(getenv("LIBCLANG_TIMING")), |
Argyrios Kyrtzidis | 4954bc1 | 2011-03-05 01:03:48 +0000 | [diff] [blame] | 101 | OwnsRemappedFileBuffers(true), |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 102 | NumStoredDiagnosticsFromDriver(0), |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 103 | PreambleRebuildCounter(0), SavedMainFileBuffer(0), PreambleBuffer(0), |
Douglas Gregor | 2c8bd47 | 2010-08-17 00:40:40 +0000 | [diff] [blame] | 104 | ShouldCacheCodeCompletionResults(false), |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 105 | NestedMacroExpansions(true), |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 106 | CompletionCacheTopLevelHashValue(0), |
| 107 | PreambleTopLevelHashValue(0), |
| 108 | CurrentTopLevelHashValue(0), |
Douglas Gregor | 4740c45 | 2010-08-19 00:45:44 +0000 | [diff] [blame] | 109 | UnsafeToFree(false) { |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 110 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 9aeaa4d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 111 | llvm::sys::AtomicIncrement(&ActiveASTUnitObjects); |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 112 | fprintf(stderr, "+++ %d translation units\n", ActiveASTUnitObjects); |
| 113 | } |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 114 | } |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 116 | ASTUnit::~ASTUnit() { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 117 | CleanTemporaryFiles(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 118 | if (!PreambleFile.empty()) |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 119 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 120 | |
| 121 | // Free the buffers associated with remapped files. We are required to |
| 122 | // perform this operation here because we explicitly request that the |
| 123 | // compiler instance *not* free these buffers for each invocation of the |
| 124 | // parser. |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 125 | if (Invocation.getPtr() && OwnsRemappedFileBuffers) { |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 126 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
| 127 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 128 | FB = PPOpts.remapped_file_buffer_begin(), |
| 129 | FBEnd = PPOpts.remapped_file_buffer_end(); |
| 130 | FB != FBEnd; |
| 131 | ++FB) |
| 132 | delete FB->second; |
| 133 | } |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 134 | |
| 135 | delete SavedMainFileBuffer; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 136 | delete PreambleBuffer; |
| 137 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 138 | ClearCachedCompletionResults(); |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 139 | |
| 140 | if (getenv("LIBCLANG_OBJTRACKING")) { |
Douglas Gregor | 9aeaa4d | 2010-12-07 00:05:48 +0000 | [diff] [blame] | 141 | llvm::sys::AtomicDecrement(&ActiveASTUnitObjects); |
Douglas Gregor | 68dbaea | 2010-11-17 00:13:31 +0000 | [diff] [blame] | 142 | fprintf(stderr, "--- %d translation units\n", ActiveASTUnitObjects); |
| 143 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void ASTUnit::CleanTemporaryFiles() { |
Douglas Gregor | 6cb5ba4 | 2010-02-18 23:35:40 +0000 | [diff] [blame] | 147 | for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) |
| 148 | TemporaryFiles[I].eraseFromDisk(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 149 | TemporaryFiles.clear(); |
Steve Naroff | 44cd60e | 2009-10-15 22:23:48 +0000 | [diff] [blame] | 150 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 151 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 152 | /// \brief Determine the set of code-completion contexts in which this |
| 153 | /// declaration should be shown. |
| 154 | static unsigned getDeclShowContexts(NamedDecl *ND, |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 155 | const LangOptions &LangOpts, |
| 156 | bool &IsNestedNameSpecifier) { |
| 157 | IsNestedNameSpecifier = false; |
| 158 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 159 | if (isa<UsingShadowDecl>(ND)) |
| 160 | ND = dyn_cast<NamedDecl>(ND->getUnderlyingDecl()); |
| 161 | if (!ND) |
| 162 | return 0; |
| 163 | |
| 164 | unsigned Contexts = 0; |
| 165 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND) || |
| 166 | isa<ClassTemplateDecl>(ND) || isa<TemplateTemplateParmDecl>(ND)) { |
| 167 | // Types can appear in these contexts. |
| 168 | if (LangOpts.CPlusPlus || !isa<TagDecl>(ND)) |
| 169 | Contexts |= (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 170 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 171 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 172 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 173 | | (1 << (CodeCompletionContext::CCC_Type - 1)) |
| 174 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 175 | |
| 176 | // In C++, types can appear in expressions contexts (for functional casts). |
| 177 | if (LangOpts.CPlusPlus) |
| 178 | Contexts |= (1 << (CodeCompletionContext::CCC_Expression - 1)); |
| 179 | |
| 180 | // In Objective-C, message sends can send interfaces. In Objective-C++, |
| 181 | // all types are available due to functional casts. |
| 182 | if (LangOpts.CPlusPlus || isa<ObjCInterfaceDecl>(ND)) |
| 183 | Contexts |= (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 184 | |
| 185 | // In Objective-C, you can only be a subclass of another Objective-C class |
| 186 | if (isa<ObjCInterfaceDecl>(ND)) |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 187 | Contexts |= (1 << (CodeCompletionContext::CCC_ObjCInterfaceName - 1)); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 188 | |
| 189 | // Deal with tag names. |
| 190 | if (isa<EnumDecl>(ND)) { |
| 191 | Contexts |= (1 << (CodeCompletionContext::CCC_EnumTag - 1)); |
| 192 | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 193 | // Part of the nested-name-specifier in C++0x. |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 194 | if (LangOpts.CPlusPlus0x) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 195 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 196 | } else if (RecordDecl *Record = dyn_cast<RecordDecl>(ND)) { |
| 197 | if (Record->isUnion()) |
| 198 | Contexts |= (1 << (CodeCompletionContext::CCC_UnionTag - 1)); |
| 199 | else |
| 200 | Contexts |= (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
| 201 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 202 | if (LangOpts.CPlusPlus) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 203 | IsNestedNameSpecifier = true; |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 204 | } else if (isa<ClassTemplateDecl>(ND)) |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 205 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 206 | } else if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
| 207 | // Values can appear in these contexts. |
| 208 | Contexts = (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 209 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 210 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 211 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)); |
| 212 | } else if (isa<ObjCProtocolDecl>(ND)) { |
| 213 | Contexts = (1 << (CodeCompletionContext::CCC_ObjCProtocolName - 1)); |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 214 | } else if (isa<ObjCCategoryDecl>(ND)) { |
| 215 | Contexts = (1 << (CodeCompletionContext::CCC_ObjCCategoryName - 1)); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 216 | } else if (isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND)) { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 217 | Contexts = (1 << (CodeCompletionContext::CCC_Namespace - 1)); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 218 | |
| 219 | // Part of the nested-name-specifier. |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 220 | IsNestedNameSpecifier = true; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | return Contexts; |
| 224 | } |
| 225 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 226 | void ASTUnit::CacheCodeCompletionResults() { |
| 227 | if (!TheSema) |
| 228 | return; |
| 229 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 230 | SimpleTimer Timer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 231 | Timer.setOutput("Cache global code completions for " + getMainFileName()); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 232 | |
| 233 | // Clear out the previous results. |
| 234 | ClearCachedCompletionResults(); |
| 235 | |
| 236 | // Gather the set of global code completions. |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 237 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 238 | SmallVector<Result, 8> Results; |
Douglas Gregor | 162b712 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 239 | CachedCompletionAllocator = new GlobalCodeCompletionAllocator; |
| 240 | TheSema->GatherGlobalCodeCompletions(*CachedCompletionAllocator, Results); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 241 | |
| 242 | // Translate global code completions into cached completions. |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 243 | llvm::DenseMap<CanQualType, unsigned> CompletionTypes; |
| 244 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 245 | for (unsigned I = 0, N = Results.size(); I != N; ++I) { |
| 246 | switch (Results[I].Kind) { |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 247 | case Result::RK_Declaration: { |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 248 | bool IsNestedNameSpecifier = false; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 249 | CachedCodeCompletionResult CachedResult; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 250 | CachedResult.Completion = Results[I].CreateCodeCompletionString(*TheSema, |
Douglas Gregor | 162b712 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 251 | *CachedCompletionAllocator); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 252 | CachedResult.ShowInContexts = getDeclShowContexts(Results[I].Declaration, |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 253 | Ctx->getLangOptions(), |
| 254 | IsNestedNameSpecifier); |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 255 | CachedResult.Priority = Results[I].Priority; |
| 256 | CachedResult.Kind = Results[I].CursorKind; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 257 | CachedResult.Availability = Results[I].Availability; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 259 | // Keep track of the type of this completion in an ASTContext-agnostic |
| 260 | // way. |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 261 | QualType UsageType = getDeclUsageType(*Ctx, Results[I].Declaration); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 262 | if (UsageType.isNull()) { |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 263 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 264 | CachedResult.Type = 0; |
| 265 | } else { |
| 266 | CanQualType CanUsageType |
| 267 | = Ctx->getCanonicalType(UsageType.getUnqualifiedType()); |
| 268 | CachedResult.TypeClass = getSimplifiedTypeClass(CanUsageType); |
| 269 | |
| 270 | // Determine whether we have already seen this type. If so, we save |
| 271 | // ourselves the work of formatting the type string by using the |
| 272 | // temporary, CanQualType-based hash table to find the associated value. |
| 273 | unsigned &TypeValue = CompletionTypes[CanUsageType]; |
| 274 | if (TypeValue == 0) { |
| 275 | TypeValue = CompletionTypes.size(); |
| 276 | CachedCompletionTypes[QualType(CanUsageType).getAsString()] |
| 277 | = TypeValue; |
| 278 | } |
| 279 | |
| 280 | CachedResult.Type = TypeValue; |
Douglas Gregor | 2474740 | 2010-08-16 16:46:30 +0000 | [diff] [blame] | 281 | } |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 283 | CachedCompletionResults.push_back(CachedResult); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 284 | |
| 285 | /// Handle nested-name-specifiers in C++. |
| 286 | if (TheSema->Context.getLangOptions().CPlusPlus && |
| 287 | IsNestedNameSpecifier && !Results[I].StartsNestedNameSpecifier) { |
| 288 | // The contexts in which a nested-name-specifier can appear in C++. |
| 289 | unsigned NNSContexts |
| 290 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 291 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 292 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 293 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 294 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
| 295 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 296 | | (1 << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 297 | | (1 << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 298 | | (1 << (CodeCompletionContext::CCC_ClassOrStructTag - 1)) |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 299 | | (1 << (CodeCompletionContext::CCC_Type - 1)) |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 300 | | (1 << (CodeCompletionContext::CCC_PotentiallyQualifiedName - 1)) |
| 301 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 302 | |
| 303 | if (isa<NamespaceDecl>(Results[I].Declaration) || |
| 304 | isa<NamespaceAliasDecl>(Results[I].Declaration)) |
| 305 | NNSContexts |= (1 << (CodeCompletionContext::CCC_Namespace - 1)); |
| 306 | |
| 307 | if (unsigned RemainingContexts |
| 308 | = NNSContexts & ~CachedResult.ShowInContexts) { |
| 309 | // If there any contexts where this completion can be a |
| 310 | // nested-name-specifier but isn't already an option, create a |
| 311 | // nested-name-specifier completion. |
| 312 | Results[I].StartsNestedNameSpecifier = true; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 313 | CachedResult.Completion |
| 314 | = Results[I].CreateCodeCompletionString(*TheSema, |
Douglas Gregor | 162b712 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 315 | *CachedCompletionAllocator); |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 316 | CachedResult.ShowInContexts = RemainingContexts; |
| 317 | CachedResult.Priority = CCP_NestedNameSpecifier; |
| 318 | CachedResult.TypeClass = STC_Void; |
| 319 | CachedResult.Type = 0; |
| 320 | CachedCompletionResults.push_back(CachedResult); |
| 321 | } |
| 322 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 323 | break; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 326 | case Result::RK_Keyword: |
| 327 | case Result::RK_Pattern: |
| 328 | // Ignore keywords and patterns; we don't care, since they are so |
| 329 | // easily regenerated. |
| 330 | break; |
| 331 | |
| 332 | case Result::RK_Macro: { |
| 333 | CachedCodeCompletionResult CachedResult; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 334 | CachedResult.Completion |
| 335 | = Results[I].CreateCodeCompletionString(*TheSema, |
Douglas Gregor | 162b712 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 336 | *CachedCompletionAllocator); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 337 | CachedResult.ShowInContexts |
| 338 | = (1 << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 339 | | (1 << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 340 | | (1 << (CodeCompletionContext::CCC_ObjCImplementation - 1)) |
| 341 | | (1 << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 342 | | (1 << (CodeCompletionContext::CCC_ClassStructUnion - 1)) |
| 343 | | (1 << (CodeCompletionContext::CCC_Statement - 1)) |
| 344 | | (1 << (CodeCompletionContext::CCC_Expression - 1)) |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 345 | | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 346 | | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1)) |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 347 | | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1)) |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 348 | | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) |
| 349 | | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1)); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 350 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 351 | CachedResult.Priority = Results[I].Priority; |
| 352 | CachedResult.Kind = Results[I].CursorKind; |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 353 | CachedResult.Availability = Results[I].Availability; |
Douglas Gregor | 6e24033 | 2010-08-16 16:18:59 +0000 | [diff] [blame] | 354 | CachedResult.TypeClass = STC_Void; |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 355 | CachedResult.Type = 0; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 356 | CachedCompletionResults.push_back(CachedResult); |
| 357 | break; |
| 358 | } |
| 359 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 360 | } |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 361 | |
| 362 | // Save the current top-level hash value. |
| 363 | CompletionCacheTopLevelHashValue = CurrentTopLevelHashValue; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void ASTUnit::ClearCachedCompletionResults() { |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 367 | CachedCompletionResults.clear(); |
Douglas Gregor | b61c07a | 2010-08-16 18:08:11 +0000 | [diff] [blame] | 368 | CachedCompletionTypes.clear(); |
Douglas Gregor | 162b712 | 2011-02-16 19:08:06 +0000 | [diff] [blame] | 369 | CachedCompletionAllocator = 0; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 372 | namespace { |
| 373 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 374 | /// \brief Gathers information from ASTReader that will be used to initialize |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 375 | /// a Preprocessor. |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 376 | class ASTInfoCollector : public ASTReaderListener { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 377 | Preprocessor &PP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 378 | ASTContext &Context; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 379 | LangOptions &LangOpt; |
| 380 | HeaderSearch &HSI; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 381 | llvm::IntrusiveRefCntPtr<TargetInfo> &Target; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 382 | std::string &Predefines; |
| 383 | unsigned &Counter; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 385 | unsigned NumHeaderInfos; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 387 | bool InitializedLanguage; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 388 | public: |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 389 | ASTInfoCollector(Preprocessor &PP, ASTContext &Context, LangOptions &LangOpt, |
| 390 | HeaderSearch &HSI, |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 391 | llvm::IntrusiveRefCntPtr<TargetInfo> &Target, |
| 392 | std::string &Predefines, |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 393 | unsigned &Counter) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 394 | : PP(PP), Context(Context), LangOpt(LangOpt), HSI(HSI), Target(Target), |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 395 | Predefines(Predefines), Counter(Counter), NumHeaderInfos(0), |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 396 | InitializedLanguage(false) {} |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 398 | virtual bool ReadLanguageOptions(const LangOptions &LangOpts) { |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 399 | if (InitializedLanguage) |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 400 | return false; |
| 401 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 402 | LangOpt = LangOpts; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 403 | |
| 404 | // Initialize the preprocessor. |
| 405 | PP.Initialize(*Target); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 406 | |
| 407 | // Initialize the ASTContext |
| 408 | Context.InitBuiltinTypes(*Target); |
| 409 | |
| 410 | InitializedLanguage = true; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 414 | virtual bool ReadTargetTriple(StringRef Triple) { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 415 | // If we've already initialized the target, don't do it again. |
| 416 | if (Target) |
| 417 | return false; |
| 418 | |
| 419 | // FIXME: This is broken, we should store the TargetOptions in the AST file. |
| 420 | TargetOptions TargetOpts; |
| 421 | TargetOpts.ABI = ""; |
| 422 | TargetOpts.CXXABI = ""; |
| 423 | TargetOpts.CPU = ""; |
| 424 | TargetOpts.Features.clear(); |
| 425 | TargetOpts.Triple = Triple; |
| 426 | Target = TargetInfo::CreateTargetInfo(PP.getDiagnostics(), TargetOpts); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 427 | return false; |
| 428 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Sebastian Redl | 8b41f30 | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 430 | virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 431 | StringRef OriginalFileName, |
Nick Lewycky | 3607989 | 2011-02-23 21:16:44 +0000 | [diff] [blame] | 432 | std::string &SuggestedPredefines, |
| 433 | FileManager &FileMgr) { |
Sebastian Redl | 8b41f30 | 2010-07-14 23:29:55 +0000 | [diff] [blame] | 434 | Predefines = Buffers[0].Data; |
| 435 | for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { |
| 436 | Predefines += Buffers[I].Data; |
| 437 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 438 | return false; |
| 439 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | a2f4945 | 2010-03-16 19:09:18 +0000 | [diff] [blame] | 441 | virtual void ReadHeaderFileInfo(const HeaderFileInfo &HFI, unsigned ID) { |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 442 | HSI.setHeaderFileInfoForUID(HFI, NumHeaderInfos++); |
| 443 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 445 | virtual void ReadCounter(unsigned Value) { |
| 446 | Counter = Value; |
| 447 | } |
| 448 | }; |
| 449 | |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 450 | class StoredDiagnosticConsumer : public DiagnosticConsumer { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 451 | SmallVectorImpl<StoredDiagnostic> &StoredDiags; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 452 | |
| 453 | public: |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 454 | explicit StoredDiagnosticConsumer( |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 455 | SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 456 | : StoredDiags(StoredDiags) { } |
| 457 | |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 458 | virtual void HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 459 | const Diagnostic &Info); |
Douglas Gregor | d0e9e3a | 2011-09-29 00:38:00 +0000 | [diff] [blame] | 460 | |
| 461 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 462 | // Just drop any diagnostics that come from cloned consumers; they'll |
| 463 | // have different source managers anyway. |
| 464 | return new IgnoringDiagConsumer(); |
| 465 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | /// \brief RAII object that optionally captures diagnostics, if |
| 469 | /// there is no diagnostic client to capture them already. |
| 470 | class CaptureDroppedDiagnostics { |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 471 | DiagnosticsEngine &Diags; |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 472 | StoredDiagnosticConsumer Client; |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 473 | DiagnosticConsumer *PreviousClient; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 474 | |
| 475 | public: |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 476 | CaptureDroppedDiagnostics(bool RequestCapture, DiagnosticsEngine &Diags, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 477 | SmallVectorImpl<StoredDiagnostic> &StoredDiags) |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 478 | : Diags(Diags), Client(StoredDiags), PreviousClient(0) |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 479 | { |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 480 | if (RequestCapture || Diags.getClient() == 0) { |
| 481 | PreviousClient = Diags.takeClient(); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 482 | Diags.setClient(&Client); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 483 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | ~CaptureDroppedDiagnostics() { |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 487 | if (Diags.getClient() == &Client) { |
| 488 | Diags.takeClient(); |
| 489 | Diags.setClient(PreviousClient); |
| 490 | } |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 491 | } |
| 492 | }; |
| 493 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 494 | } // anonymous namespace |
| 495 | |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 496 | void StoredDiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level Level, |
David Blaikie | b578432 | 2011-09-26 01:18:08 +0000 | [diff] [blame] | 497 | const Diagnostic &Info) { |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 498 | // Default implementation (Warnings/errors count). |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 499 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
Argyrios Kyrtzidis | c79346a | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 501 | StoredDiags.push_back(StoredDiagnostic(Level, Info)); |
| 502 | } |
| 503 | |
Steve Naroff | c0683b9 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 504 | const std::string &ASTUnit::getOriginalSourceFileName() { |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 505 | return OriginalSourceFile; |
Steve Naroff | c0683b9 | 2009-09-03 18:19:54 +0000 | [diff] [blame] | 506 | } |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 507 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 508 | llvm::MemoryBuffer *ASTUnit::getBufferForFile(StringRef Filename, |
Chris Lattner | 26b5c19 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 509 | std::string *ErrorStr) { |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 510 | assert(FileMgr); |
Chris Lattner | 26b5c19 | 2010-11-23 09:19:42 +0000 | [diff] [blame] | 511 | return FileMgr->getBufferForFile(Filename, ErrorStr); |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 514 | /// \brief Configure the diagnostics object for use with ASTUnit. |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 515 | void ASTUnit::ConfigureDiags(llvm::IntrusiveRefCntPtr<DiagnosticsEngine> &Diags, |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 516 | const char **ArgBegin, const char **ArgEnd, |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 517 | ASTUnit &AST, bool CaptureDiagnostics) { |
| 518 | if (!Diags.getPtr()) { |
| 519 | // No diagnostics engine was provided, so create our own diagnostics object |
| 520 | // with the default options. |
| 521 | DiagnosticOptions DiagOpts; |
David Blaikie | e2eefae | 2011-09-25 23:39:51 +0000 | [diff] [blame] | 522 | DiagnosticConsumer *Client = 0; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 523 | if (CaptureDiagnostics) |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 524 | Client = new StoredDiagnosticConsumer(AST.StoredDiagnostics); |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 525 | Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd- ArgBegin, |
| 526 | ArgBegin, Client); |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 527 | } else if (CaptureDiagnostics) { |
David Blaikie | f18d91a | 2011-09-26 00:01:39 +0000 | [diff] [blame] | 528 | Diags->setClient(new StoredDiagnosticConsumer(AST.StoredDiagnostics)); |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 532 | ASTUnit *ASTUnit::LoadFromASTFile(const std::string &Filename, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 533 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 534 | const FileSystemOptions &FileSystemOpts, |
Ted Kremenek | 8bcb1c6 | 2009-10-17 00:34:24 +0000 | [diff] [blame] | 535 | bool OnlyLocalDecls, |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 536 | RemappedFile *RemappedFiles, |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 537 | unsigned NumRemappedFiles, |
| 538 | bool CaptureDiagnostics) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 539 | llvm::OwningPtr<ASTUnit> AST(new ASTUnit(true)); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 540 | |
| 541 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 542 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 543 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 544 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 545 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 546 | DiagCleanup(Diags.getPtr()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 548 | ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 549 | |
Douglas Gregor | 16bef85 | 2009-10-16 20:01:17 +0000 | [diff] [blame] | 550 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 551 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 552 | AST->Diagnostics = Diags; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 553 | AST->FileMgr = new FileManager(FileSystemOpts); |
| 554 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), |
| 555 | AST->getFileManager()); |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 556 | AST->HeaderInfo.reset(new HeaderSearch(AST->getFileManager())); |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 558 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 559 | FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; |
| 560 | if (const llvm::MemoryBuffer * |
| 561 | memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { |
| 562 | // Create the file entry for the file that we're mapping from. |
| 563 | const FileEntry *FromFile |
| 564 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 565 | memBuf->getBufferSize(), |
| 566 | 0); |
| 567 | if (!FromFile) { |
| 568 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
| 569 | << RemappedFiles[I].first; |
| 570 | delete memBuf; |
| 571 | continue; |
| 572 | } |
| 573 | |
| 574 | // Override the contents of the "from" file with the contents of |
| 575 | // the "to" file. |
| 576 | AST->getSourceManager().overrideFileContents(FromFile, memBuf); |
| 577 | |
| 578 | } else { |
| 579 | const char *fname = fileOrBuf.get<const char *>(); |
| 580 | const FileEntry *ToFile = AST->FileMgr->getFile(fname); |
| 581 | if (!ToFile) { |
| 582 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_to_file) |
| 583 | << RemappedFiles[I].first << fname; |
| 584 | continue; |
| 585 | } |
| 586 | |
| 587 | // Create the file entry for the file that we're mapping from. |
| 588 | const FileEntry *FromFile |
| 589 | = AST->getFileManager().getVirtualFile(RemappedFiles[I].first, |
| 590 | ToFile->getSize(), |
| 591 | 0); |
| 592 | if (!FromFile) { |
| 593 | AST->getDiagnostics().Report(diag::err_fe_remap_missing_from_file) |
| 594 | << RemappedFiles[I].first; |
| 595 | delete memBuf; |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | // Override the contents of the "from" file with the contents of |
| 600 | // the "to" file. |
| 601 | AST->getSourceManager().overrideFileContents(FromFile, ToFile); |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 602 | } |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 605 | // Gather Info for preprocessor construction later on. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 607 | HeaderSearch &HeaderInfo = *AST->HeaderInfo.get(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 608 | std::string Predefines; |
| 609 | unsigned Counter; |
| 610 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 611 | llvm::OwningPtr<ASTReader> Reader; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 613 | AST->PP = new Preprocessor(AST->getDiagnostics(), AST->ASTFileLangOpts, |
| 614 | /*Target=*/0, AST->getSourceManager(), HeaderInfo, |
| 615 | *AST, |
| 616 | /*IILookup=*/0, |
| 617 | /*OwnsHeaderSearch=*/false, |
| 618 | /*DelayInitialization=*/true); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 619 | Preprocessor &PP = *AST->PP; |
| 620 | |
| 621 | AST->Ctx = new ASTContext(AST->ASTFileLangOpts, |
| 622 | AST->getSourceManager(), |
| 623 | /*Target=*/0, |
| 624 | PP.getIdentifierTable(), |
| 625 | PP.getSelectorTable(), |
| 626 | PP.getBuiltinInfo(), |
| 627 | /* size_reserve = */0, |
| 628 | /*DelayInitialization=*/true); |
| 629 | ASTContext &Context = *AST->Ctx; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 630 | |
Douglas Gregor | 8835e03 | 2011-09-02 00:26:20 +0000 | [diff] [blame] | 631 | Reader.reset(new ASTReader(PP, Context)); |
Ted Kremenek | 2159b8d | 2011-05-04 23:27:12 +0000 | [diff] [blame] | 632 | |
| 633 | // Recover resources if we crash before exiting this method. |
| 634 | llvm::CrashRecoveryContextCleanupRegistrar<ASTReader> |
| 635 | ReaderCleanup(Reader.get()); |
| 636 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 637 | Reader->setListener(new ASTInfoCollector(*AST->PP, Context, |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 638 | AST->ASTFileLangOpts, HeaderInfo, |
| 639 | AST->Target, Predefines, Counter)); |
Daniel Dunbar | 2d9c740 | 2009-09-03 05:59:35 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | a6895d8 | 2011-07-22 16:00:58 +0000 | [diff] [blame] | 641 | switch (Reader->ReadAST(Filename, serialization::MK_MainFile)) { |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 642 | case ASTReader::Success: |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 643 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 645 | case ASTReader::Failure: |
| 646 | case ASTReader::IgnorePCH: |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 647 | AST->getDiagnostics().Report(diag::err_fe_unable_to_load_pch); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 648 | return NULL; |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 649 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 650 | |
Daniel Dunbar | a8a5093 | 2009-12-02 08:44:16 +0000 | [diff] [blame] | 651 | AST->OriginalSourceFile = Reader->getOriginalSourceFile(); |
| 652 | |
Daniel Dunbar | b7bbfdd | 2009-09-21 03:03:47 +0000 | [diff] [blame] | 653 | PP.setPredefines(Reader->getSuggestedPredefines()); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 654 | PP.setCounterValue(Counter); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 656 | // Attach the AST reader to the AST context as an external AST |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 657 | // source, so that declarations will be deserialized from the |
Sebastian Redl | d44cd6a | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 658 | // AST file as needed. |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 659 | ASTReader *ReaderPtr = Reader.get(); |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 660 | llvm::OwningPtr<ExternalASTSource> Source(Reader.take()); |
Ted Kremenek | 2159b8d | 2011-05-04 23:27:12 +0000 | [diff] [blame] | 661 | |
| 662 | // Unregister the cleanup for ASTReader. It will get cleaned up |
| 663 | // by the ASTUnit cleanup. |
| 664 | ReaderCleanup.unregister(); |
| 665 | |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 666 | Context.setExternalSource(Source); |
| 667 | |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 668 | // Create an AST consumer, even though it isn't used. |
| 669 | AST->Consumer.reset(new ASTConsumer); |
| 670 | |
Sebastian Redl | 2c499f6 | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 671 | // Create a semantic analysis object and tell the AST reader about it. |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 672 | AST->TheSema.reset(new Sema(PP, Context, *AST->Consumer)); |
| 673 | AST->TheSema->Initialize(); |
| 674 | ReaderPtr->InitializeSema(*AST->TheSema); |
| 675 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | return AST.take(); |
Argyrios Kyrtzidis | ce37975 | 2009-06-20 08:08:23 +0000 | [diff] [blame] | 677 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 678 | |
| 679 | namespace { |
| 680 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 681 | /// \brief Preprocessor callback class that updates a hash value with the names |
| 682 | /// of all macros that have been defined by the translation unit. |
| 683 | class MacroDefinitionTrackerPPCallbacks : public PPCallbacks { |
| 684 | unsigned &Hash; |
| 685 | |
| 686 | public: |
| 687 | explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { } |
| 688 | |
| 689 | virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) { |
| 690 | Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash); |
| 691 | } |
| 692 | }; |
| 693 | |
| 694 | /// \brief Add the given declaration to the hash of all top-level entities. |
| 695 | void AddTopLevelDeclarationToHash(Decl *D, unsigned &Hash) { |
| 696 | if (!D) |
| 697 | return; |
| 698 | |
| 699 | DeclContext *DC = D->getDeclContext(); |
| 700 | if (!DC) |
| 701 | return; |
| 702 | |
| 703 | if (!(DC->isTranslationUnit() || DC->getLookupParent()->isTranslationUnit())) |
| 704 | return; |
| 705 | |
| 706 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 707 | if (ND->getIdentifier()) |
| 708 | Hash = llvm::HashString(ND->getIdentifier()->getName(), Hash); |
| 709 | else if (DeclarationName Name = ND->getDeclName()) { |
| 710 | std::string NameStr = Name.getAsString(); |
| 711 | Hash = llvm::HashString(NameStr, Hash); |
| 712 | } |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | if (ObjCForwardProtocolDecl *Forward |
| 717 | = dyn_cast<ObjCForwardProtocolDecl>(D)) { |
| 718 | for (ObjCForwardProtocolDecl::protocol_iterator |
| 719 | P = Forward->protocol_begin(), |
| 720 | PEnd = Forward->protocol_end(); |
| 721 | P != PEnd; ++P) |
| 722 | AddTopLevelDeclarationToHash(*P, Hash); |
| 723 | return; |
| 724 | } |
| 725 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 726 | if (ObjCClassDecl *Class = dyn_cast<ObjCClassDecl>(D)) { |
Fariborz Jahanian | 3a039e3 | 2011-08-27 20:50:59 +0000 | [diff] [blame] | 727 | AddTopLevelDeclarationToHash(Class->getForwardInterfaceDecl(), Hash); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | } |
| 731 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 732 | class TopLevelDeclTrackerConsumer : public ASTConsumer { |
| 733 | ASTUnit &Unit; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 734 | unsigned &Hash; |
| 735 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 736 | public: |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 737 | TopLevelDeclTrackerConsumer(ASTUnit &_Unit, unsigned &Hash) |
| 738 | : Unit(_Unit), Hash(Hash) { |
| 739 | Hash = 0; |
| 740 | } |
| 741 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 742 | void HandleTopLevelDecl(DeclGroupRef D) { |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 743 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 744 | Decl *D = *it; |
| 745 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 746 | // reported as top-level declarations, even though their DeclContext |
| 747 | // is the containing ObjC @interface/@implementation. This is a |
| 748 | // fundamental problem in the parser right now. |
| 749 | if (isa<ObjCMethodDecl>(D)) |
| 750 | continue; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 751 | |
| 752 | AddTopLevelDeclarationToHash(D, Hash); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 753 | Unit.addTopLevelDecl(D); |
Ted Kremenek | acc59c3 | 2010-05-03 20:16:35 +0000 | [diff] [blame] | 754 | } |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 755 | } |
Sebastian Redl | eaa4ade | 2010-08-11 18:52:41 +0000 | [diff] [blame] | 756 | |
| 757 | // We're not interested in "interesting" decls. |
| 758 | void HandleInterestingDecl(DeclGroupRef) {} |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 759 | }; |
| 760 | |
| 761 | class TopLevelDeclTrackerAction : public ASTFrontendAction { |
| 762 | public: |
| 763 | ASTUnit &Unit; |
| 764 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 765 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 766 | StringRef InFile) { |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 767 | CI.getPreprocessor().addPPCallbacks( |
| 768 | new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); |
| 769 | return new TopLevelDeclTrackerConsumer(Unit, |
| 770 | Unit.getCurrentTopLevelHashValue()); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | public: |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 774 | TopLevelDeclTrackerAction(ASTUnit &_Unit) : Unit(_Unit) {} |
| 775 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 776 | virtual bool hasCodeCompletionSupport() const { return false; } |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 777 | virtual TranslationUnitKind getTranslationUnitKind() { |
| 778 | return Unit.getTranslationUnitKind(); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 779 | } |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 780 | }; |
| 781 | |
Argyrios Kyrtzidis | 5733271 | 2011-09-19 20:40:48 +0000 | [diff] [blame] | 782 | class PrecompilePreambleConsumer : public PCHGenerator { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 783 | ASTUnit &Unit; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 784 | unsigned &Hash; |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 785 | std::vector<Decl *> TopLevelDecls; |
Douglas Gregor | f88e35b | 2010-11-30 06:16:57 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 787 | public: |
Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 788 | PrecompilePreambleConsumer(ASTUnit &Unit, const Preprocessor &PP, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 789 | StringRef isysroot, raw_ostream *Out) |
Douglas Gregor | 4a69c2e | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 790 | : PCHGenerator(PP, "", /*IsModule=*/false, isysroot, Out), Unit(Unit), |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 791 | Hash(Unit.getCurrentTopLevelHashValue()) { |
| 792 | Hash = 0; |
| 793 | } |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 795 | virtual void HandleTopLevelDecl(DeclGroupRef D) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 796 | for (DeclGroupRef::iterator it = D.begin(), ie = D.end(); it != ie; ++it) { |
| 797 | Decl *D = *it; |
| 798 | // FIXME: Currently ObjC method declarations are incorrectly being |
| 799 | // reported as top-level declarations, even though their DeclContext |
| 800 | // is the containing ObjC @interface/@implementation. This is a |
| 801 | // fundamental problem in the parser right now. |
| 802 | if (isa<ObjCMethodDecl>(D)) |
| 803 | continue; |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 804 | AddTopLevelDeclarationToHash(D, Hash); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 805 | TopLevelDecls.push_back(D); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | virtual void HandleTranslationUnit(ASTContext &Ctx) { |
| 810 | PCHGenerator::HandleTranslationUnit(Ctx); |
| 811 | if (!Unit.getDiagnostics().hasErrorOccurred()) { |
| 812 | // Translate the top-level declarations we captured during |
| 813 | // parsing into declaration IDs in the precompiled |
| 814 | // preamble. This will allow us to deserialize those top-level |
| 815 | // declarations when requested. |
| 816 | for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) |
| 817 | Unit.addTopLevelDeclFromPreamble( |
| 818 | getWriter().getDeclID(TopLevelDecls[I])); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | }; |
| 822 | |
| 823 | class PrecompilePreambleAction : public ASTFrontendAction { |
| 824 | ASTUnit &Unit; |
| 825 | |
| 826 | public: |
| 827 | explicit PrecompilePreambleAction(ASTUnit &Unit) : Unit(Unit) {} |
| 828 | |
| 829 | virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 830 | StringRef InFile) { |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 831 | std::string Sysroot; |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 832 | std::string OutputFile; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 833 | raw_ostream *OS = 0; |
Argyrios Kyrtzidis | 10b2368 | 2011-02-15 17:54:22 +0000 | [diff] [blame] | 834 | if (GeneratePCHAction::ComputeASTConsumerArguments(CI, InFile, Sysroot, |
| 835 | OutputFile, |
Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 836 | OS)) |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 837 | return 0; |
| 838 | |
Douglas Gregor | c567ba2 | 2011-07-22 16:35:34 +0000 | [diff] [blame] | 839 | if (!CI.getFrontendOpts().RelocatablePCH) |
| 840 | Sysroot.clear(); |
| 841 | |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 842 | CI.getPreprocessor().addPPCallbacks( |
| 843 | new MacroDefinitionTrackerPPCallbacks(Unit.getCurrentTopLevelHashValue())); |
Douglas Gregor | 36db4f9 | 2011-08-25 22:35:51 +0000 | [diff] [blame] | 844 | return new PrecompilePreambleConsumer(Unit, CI.getPreprocessor(), Sysroot, |
| 845 | OS); |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | virtual bool hasCodeCompletionSupport() const { return false; } |
| 849 | virtual bool hasASTFileSupport() const { return false; } |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 850 | virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 851 | }; |
| 852 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 855 | /// Parse the source file into a translation unit using the given compiler |
| 856 | /// invocation, replacing the current translation unit. |
| 857 | /// |
| 858 | /// \returns True if a failure occurred that causes the ASTUnit not to |
| 859 | /// contain any translation-unit information, false otherwise. |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 860 | bool ASTUnit::Parse(llvm::MemoryBuffer *OverrideMainBuffer) { |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 861 | delete SavedMainFileBuffer; |
| 862 | SavedMainFileBuffer = 0; |
| 863 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 864 | if (!Invocation) { |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 865 | delete OverrideMainBuffer; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 866 | return true; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 867 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 868 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 869 | // Create the compiler instance to use for building the AST. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 870 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
| 871 | |
| 872 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 873 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 874 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 875 | |
Argyrios Kyrtzidis | 14c32e8 | 2011-09-12 18:09:38 +0000 | [diff] [blame] | 876 | llvm::IntrusiveRefCntPtr<CompilerInvocation> |
| 877 | CCInvocation(new CompilerInvocation(*Invocation)); |
| 878 | |
| 879 | Clang->setInvocation(CCInvocation.getPtr()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 880 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 882 | // Set up diagnostics, capturing any diagnostics that would |
| 883 | // otherwise be dropped. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 884 | Clang->setDiagnostics(&getDiagnostics()); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 885 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 886 | // Create the target instance. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 887 | Clang->getTargetOpts().Features = TargetFeatures; |
| 888 | Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 889 | Clang->getTargetOpts())); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 890 | if (!Clang->hasTarget()) { |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 891 | delete OverrideMainBuffer; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 892 | return true; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 895 | // Inform the target of the language options. |
| 896 | // |
| 897 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 898 | // created. This complexity should be lifted elsewhere. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 899 | Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 901 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 902 | "Invocation must have exactly one source file!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 903 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST && |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 904 | "FIXME: AST inputs not yet supported here!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 905 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
Daniel Dunbar | 9507f9c | 2010-06-07 23:26:47 +0000 | [diff] [blame] | 906 | "IR inputs not support here!"); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 908 | // Configure the various subsystems. |
| 909 | // FIXME: Should we retain the previous file manager? |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 910 | FileSystemOpts = Clang->getFileSystemOpts(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 911 | FileMgr = new FileManager(FileSystemOpts); |
| 912 | SourceMgr = new SourceManager(getDiagnostics(), *FileMgr); |
Douglas Gregor | 6fd55e0 | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 913 | TheSema.reset(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 914 | Ctx = 0; |
| 915 | PP = 0; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 916 | |
| 917 | // Clear out old caches and data. |
| 918 | TopLevelDecls.clear(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 919 | CleanTemporaryFiles(); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 921 | if (!OverrideMainBuffer) { |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 922 | StoredDiagnostics.erase( |
| 923 | StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver, |
| 924 | StoredDiagnostics.end()); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 925 | TopLevelDeclsInPreamble.clear(); |
| 926 | } |
| 927 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 928 | // Create a file manager object to provide access to and cache the filesystem. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 929 | Clang->setFileManager(&getFileManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 930 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 931 | // Create the source manager. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 932 | Clang->setSourceManager(&getSourceManager()); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 933 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 934 | // If the main file has been overridden due to the use of a preamble, |
| 935 | // make that override happen and introduce the preamble. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 936 | PreprocessorOptions &PreprocessorOpts = Clang->getPreprocessorOpts(); |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 937 | PreprocessorOpts.DetailedRecordIncludesNestedMacroExpansions |
| 938 | = NestedMacroExpansions; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 939 | if (OverrideMainBuffer) { |
| 940 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 941 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 942 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 943 | = PreambleEndsAtStartOfLine; |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 944 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 945 | PreprocessorOpts.DisablePCHValidation = true; |
Douglas Gregor | 96c0426 | 2010-07-27 14:52:07 +0000 | [diff] [blame] | 946 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 947 | // The stored diagnostic has the old source manager in it; update |
| 948 | // the locations to refer into the new source manager. Since we've |
| 949 | // been careful to make sure that the source manager's state |
| 950 | // before and after are identical, so that we can reuse the source |
| 951 | // location itself. |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 952 | for (unsigned I = NumStoredDiagnosticsFromDriver, |
| 953 | N = StoredDiagnostics.size(); |
| 954 | I < N; ++I) { |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 955 | FullSourceLoc Loc(StoredDiagnostics[I].getLocation(), |
| 956 | getSourceManager()); |
| 957 | StoredDiagnostics[I].setLocation(Loc); |
| 958 | } |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 959 | |
| 960 | // Keep track of the override buffer; |
| 961 | SavedMainFileBuffer = OverrideMainBuffer; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 964 | llvm::OwningPtr<TopLevelDeclTrackerAction> Act( |
| 965 | new TopLevelDeclTrackerAction(*this)); |
| 966 | |
| 967 | // Recover resources if we crash before exiting this method. |
| 968 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 969 | ActCleanup(Act.get()); |
| 970 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 971 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second, |
| 972 | Clang->getFrontendOpts().Inputs[0].first)) |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 973 | goto error; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 974 | |
| 975 | if (OverrideMainBuffer) { |
Jonathan D. Turner | 2214acd | 2011-07-22 17:25:03 +0000 | [diff] [blame] | 976 | std::string ModName = PreambleFile; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 977 | TranslateStoredDiagnostics(Clang->getModuleManager(), ModName, |
| 978 | getSourceManager(), PreambleDiagnostics, |
| 979 | StoredDiagnostics); |
| 980 | } |
| 981 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 982 | Act->Execute(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 983 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 984 | // Steal the created target, context, and preprocessor. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 985 | TheSema.reset(Clang->takeSema()); |
| 986 | Consumer.reset(Clang->takeASTConsumer()); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 987 | Ctx = &Clang->getASTContext(); |
| 988 | PP = &Clang->getPreprocessor(); |
| 989 | Clang->setSourceManager(0); |
| 990 | Clang->setFileManager(0); |
| 991 | Target = &Clang->getTarget(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 992 | |
Daniel Dunbar | 644dca0 | 2009-12-04 08:17:33 +0000 | [diff] [blame] | 993 | Act->EndSourceFile(); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 994 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 995 | return false; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 997 | error: |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 998 | // Remove the overridden buffer we used for the preamble. |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 999 | if (OverrideMainBuffer) { |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1000 | delete OverrideMainBuffer; |
Douglas Gregor | a3d3ba1 | 2010-10-06 21:11:08 +0000 | [diff] [blame] | 1001 | SavedMainFileBuffer = 0; |
Douglas Gregor | ce3a829 | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 1002 | } |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | efc4695 | 2010-10-12 16:25:54 +0000 | [diff] [blame] | 1004 | StoredDiagnostics.clear(); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1005 | return true; |
| 1006 | } |
| 1007 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1008 | /// \brief Simple function to retrieve a path for a preamble precompiled header. |
| 1009 | static std::string GetPreamblePCHPath() { |
| 1010 | // FIXME: This is lame; sys::Path should provide this function (in particular, |
| 1011 | // it should know how to find the temporary files dir). |
| 1012 | // FIXME: This is really lame. I copied this code from the Driver! |
Douglas Gregor | 250ab1d | 2010-09-11 18:05:19 +0000 | [diff] [blame] | 1013 | // FIXME: This is a hack so that we can override the preamble file during |
| 1014 | // crash-recovery testing, which is the only case where the preamble files |
| 1015 | // are not necessarily cleaned up. |
| 1016 | const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); |
| 1017 | if (TmpFile) |
| 1018 | return TmpFile; |
| 1019 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1020 | std::string Error; |
| 1021 | const char *TmpDir = ::getenv("TMPDIR"); |
| 1022 | if (!TmpDir) |
| 1023 | TmpDir = ::getenv("TEMP"); |
| 1024 | if (!TmpDir) |
| 1025 | TmpDir = ::getenv("TMP"); |
Douglas Gregor | ce3449f | 2010-09-11 17:51:16 +0000 | [diff] [blame] | 1026 | #ifdef LLVM_ON_WIN32 |
| 1027 | if (!TmpDir) |
| 1028 | TmpDir = ::getenv("USERPROFILE"); |
| 1029 | #endif |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1030 | if (!TmpDir) |
| 1031 | TmpDir = "/tmp"; |
| 1032 | llvm::sys::Path P(TmpDir); |
Douglas Gregor | ce3449f | 2010-09-11 17:51:16 +0000 | [diff] [blame] | 1033 | P.createDirectoryOnDisk(true); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1034 | P.appendComponent("preamble"); |
Douglas Gregor | 20975b2 | 2010-08-11 13:06:56 +0000 | [diff] [blame] | 1035 | P.appendSuffix("pch"); |
Argyrios Kyrtzidis | ff9a550 | 2011-07-21 18:44:46 +0000 | [diff] [blame] | 1036 | if (P.makeUnique(/*reuse_current=*/false, /*ErrMsg*/0)) |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1037 | return std::string(); |
| 1038 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1039 | return P.str(); |
| 1040 | } |
| 1041 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1042 | /// \brief Compute the preamble for the main file, providing the source buffer |
| 1043 | /// that corresponds to the main file along with a pair (bytes, start-of-line) |
| 1044 | /// that describes the preamble. |
| 1045 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1046 | ASTUnit::ComputePreamble(CompilerInvocation &Invocation, |
| 1047 | unsigned MaxLines, bool &CreatedBuffer) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1048 | FrontendOptions &FrontendOpts = Invocation.getFrontendOpts(); |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 1049 | PreprocessorOptions &PreprocessorOpts = Invocation.getPreprocessorOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1050 | CreatedBuffer = false; |
| 1051 | |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1052 | // Try to determine if the main file has been remapped, either from the |
| 1053 | // command line (to another file) or directly through the compiler invocation |
| 1054 | // (to a memory buffer). |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1055 | llvm::MemoryBuffer *Buffer = 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1056 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
| 1057 | if (const llvm::sys::FileStatus *MainFileStatus = MainFilePath.getFileStatus()) { |
| 1058 | // Check whether there is a file-file remapping of the main file |
| 1059 | for (PreprocessorOptions::remapped_file_iterator |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1060 | M = PreprocessorOpts.remapped_file_begin(), |
| 1061 | E = PreprocessorOpts.remapped_file_end(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1062 | M != E; |
| 1063 | ++M) { |
| 1064 | llvm::sys::PathWithStatus MPath(M->first); |
| 1065 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 1066 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 1067 | // We found a remapping. Try to load the resulting, remapped source. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1068 | if (CreatedBuffer) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1069 | delete Buffer; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1070 | CreatedBuffer = false; |
| 1071 | } |
| 1072 | |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 1073 | Buffer = getBufferForFile(M->second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1074 | if (!Buffer) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1075 | return std::make_pair((llvm::MemoryBuffer*)0, |
| 1076 | std::make_pair(0, true)); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1077 | CreatedBuffer = true; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | // Check whether there is a file-buffer remapping. It supercedes the |
| 1083 | // file-file remapping. |
| 1084 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 1085 | M = PreprocessorOpts.remapped_file_buffer_begin(), |
| 1086 | E = PreprocessorOpts.remapped_file_buffer_end(); |
| 1087 | M != E; |
| 1088 | ++M) { |
| 1089 | llvm::sys::PathWithStatus MPath(M->first); |
| 1090 | if (const llvm::sys::FileStatus *MStatus = MPath.getFileStatus()) { |
| 1091 | if (MainFileStatus->uniqueID == MStatus->uniqueID) { |
| 1092 | // We found a remapping. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1093 | if (CreatedBuffer) { |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1094 | delete Buffer; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1095 | CreatedBuffer = false; |
| 1096 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1098 | Buffer = const_cast<llvm::MemoryBuffer *>(M->second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1101 | } |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | // If the main source file was not remapped, load it now. |
| 1105 | if (!Buffer) { |
Argyrios Kyrtzidis | 71731d6 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 1106 | Buffer = getBufferForFile(FrontendOpts.Inputs[0].second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1107 | if (!Buffer) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1108 | return std::make_pair((llvm::MemoryBuffer*)0, std::make_pair(0, true)); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1109 | |
| 1110 | CreatedBuffer = true; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
Argyrios Kyrtzidis | 7aecbc7 | 2011-08-25 20:39:19 +0000 | [diff] [blame] | 1113 | return std::make_pair(Buffer, Lexer::ComputePreamble(Buffer, |
| 1114 | Invocation.getLangOpts(), |
| 1115 | MaxLines)); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1118 | static llvm::MemoryBuffer *CreatePaddedMainFileBuffer(llvm::MemoryBuffer *Old, |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1119 | unsigned NewSize, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1120 | StringRef NewName) { |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1121 | llvm::MemoryBuffer *Result |
| 1122 | = llvm::MemoryBuffer::getNewUninitMemBuffer(NewSize, NewName); |
| 1123 | memcpy(const_cast<char*>(Result->getBufferStart()), |
| 1124 | Old->getBufferStart(), Old->getBufferSize()); |
| 1125 | memset(const_cast<char*>(Result->getBufferStart()) + Old->getBufferSize(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1126 | ' ', NewSize - Old->getBufferSize() - 1); |
| 1127 | const_cast<char*>(Result->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1129 | return Result; |
| 1130 | } |
| 1131 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1132 | /// \brief Attempt to build or re-use a precompiled preamble when (re-)parsing |
| 1133 | /// the source file. |
| 1134 | /// |
| 1135 | /// This routine will compute the preamble of the main source file. If a |
| 1136 | /// non-trivial preamble is found, it will precompile that preamble into a |
| 1137 | /// precompiled header so that the precompiled preamble can be used to reduce |
| 1138 | /// reparsing time. If a precompiled preamble has already been constructed, |
| 1139 | /// this routine will determine if it is still valid and, if so, avoid |
| 1140 | /// rebuilding the precompiled preamble. |
| 1141 | /// |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1142 | /// \param AllowRebuild When true (the default), this routine is |
| 1143 | /// allowed to rebuild the precompiled preamble if it is found to be |
| 1144 | /// out-of-date. |
| 1145 | /// |
| 1146 | /// \param MaxLines When non-zero, the maximum number of lines that |
| 1147 | /// can occur within the preamble. |
| 1148 | /// |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1149 | /// \returns If the precompiled preamble can be used, returns a newly-allocated |
| 1150 | /// buffer that should be used in place of the main file when doing so. |
| 1151 | /// Otherwise, returns a NULL pointer. |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1152 | llvm::MemoryBuffer *ASTUnit::getMainBufferWithPrecompiledPreamble( |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1153 | const CompilerInvocation &PreambleInvocationIn, |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1154 | bool AllowRebuild, |
| 1155 | unsigned MaxLines) { |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1156 | |
| 1157 | llvm::IntrusiveRefCntPtr<CompilerInvocation> |
| 1158 | PreambleInvocation(new CompilerInvocation(PreambleInvocationIn)); |
| 1159 | FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1160 | PreprocessorOptions &PreprocessorOpts |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1161 | = PreambleInvocation->getPreprocessorOpts(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1162 | |
| 1163 | bool CreatedPreambleBuffer = false; |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1164 | std::pair<llvm::MemoryBuffer *, std::pair<unsigned, bool> > NewPreamble |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1165 | = ComputePreamble(*PreambleInvocation, MaxLines, CreatedPreambleBuffer); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1166 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1167 | // If ComputePreamble() Take ownership of the preamble buffer. |
Douglas Gregor | 3edb167 | 2010-11-16 20:45:51 +0000 | [diff] [blame] | 1168 | llvm::OwningPtr<llvm::MemoryBuffer> OwnedPreambleBuffer; |
| 1169 | if (CreatedPreambleBuffer) |
| 1170 | OwnedPreambleBuffer.reset(NewPreamble.first); |
| 1171 | |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1172 | if (!NewPreamble.second.first) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1173 | // We couldn't find a preamble in the main source. Clear out the current |
| 1174 | // preamble, if we have one. It's obviously no good any more. |
| 1175 | Preamble.clear(); |
| 1176 | if (!PreambleFile.empty()) { |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1177 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1178 | PreambleFile.clear(); |
| 1179 | } |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1180 | |
| 1181 | // The next time we actually see a preamble, precompile it. |
| 1182 | PreambleRebuildCounter = 1; |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1183 | return 0; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | if (!Preamble.empty()) { |
| 1187 | // We've previously computed a preamble. Check whether we have the same |
| 1188 | // preamble now that we did before, and that there's enough space in |
| 1189 | // the main-file buffer within the precompiled preamble to fit the |
| 1190 | // new main file. |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1191 | if (Preamble.size() == NewPreamble.second.first && |
| 1192 | PreambleEndsAtStartOfLine == NewPreamble.second.second && |
Douglas Gregor | f5275a8 | 2010-07-24 00:42:07 +0000 | [diff] [blame] | 1193 | NewPreamble.first->getBufferSize() < PreambleReservedSize-2 && |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1194 | memcmp(Preamble.getBufferStart(), NewPreamble.first->getBufferStart(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1195 | NewPreamble.second.first) == 0) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1196 | // The preamble has not changed. We may be able to re-use the precompiled |
| 1197 | // preamble. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1198 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1199 | // Check that none of the files used by the preamble have changed. |
| 1200 | bool AnyFileChanged = false; |
| 1201 | |
| 1202 | // First, make a record of those files that have been overridden via |
| 1203 | // remapping or unsaved_files. |
| 1204 | llvm::StringMap<std::pair<off_t, time_t> > OverriddenFiles; |
| 1205 | for (PreprocessorOptions::remapped_file_iterator |
| 1206 | R = PreprocessorOpts.remapped_file_begin(), |
| 1207 | REnd = PreprocessorOpts.remapped_file_end(); |
| 1208 | !AnyFileChanged && R != REnd; |
| 1209 | ++R) { |
| 1210 | struct stat StatBuf; |
Anders Carlsson | 9583f79 | 2011-03-18 19:23:38 +0000 | [diff] [blame] | 1211 | if (FileMgr->getNoncachedStatValue(R->second, StatBuf)) { |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1212 | // If we can't stat the file we're remapping to, assume that something |
| 1213 | // horrible happened. |
| 1214 | AnyFileChanged = true; |
| 1215 | break; |
| 1216 | } |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1217 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1218 | OverriddenFiles[R->first] = std::make_pair(StatBuf.st_size, |
| 1219 | StatBuf.st_mtime); |
| 1220 | } |
| 1221 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 1222 | R = PreprocessorOpts.remapped_file_buffer_begin(), |
| 1223 | REnd = PreprocessorOpts.remapped_file_buffer_end(); |
| 1224 | !AnyFileChanged && R != REnd; |
| 1225 | ++R) { |
| 1226 | // FIXME: Should we actually compare the contents of file->buffer |
| 1227 | // remappings? |
| 1228 | OverriddenFiles[R->first] = std::make_pair(R->second->getBufferSize(), |
| 1229 | 0); |
| 1230 | } |
| 1231 | |
| 1232 | // Check whether anything has changed. |
| 1233 | for (llvm::StringMap<std::pair<off_t, time_t> >::iterator |
| 1234 | F = FilesInPreamble.begin(), FEnd = FilesInPreamble.end(); |
| 1235 | !AnyFileChanged && F != FEnd; |
| 1236 | ++F) { |
| 1237 | llvm::StringMap<std::pair<off_t, time_t> >::iterator Overridden |
| 1238 | = OverriddenFiles.find(F->first()); |
| 1239 | if (Overridden != OverriddenFiles.end()) { |
| 1240 | // This file was remapped; check whether the newly-mapped file |
| 1241 | // matches up with the previous mapping. |
| 1242 | if (Overridden->second != F->second) |
| 1243 | AnyFileChanged = true; |
| 1244 | continue; |
| 1245 | } |
| 1246 | |
| 1247 | // The file was not remapped; check whether it has changed on disk. |
| 1248 | struct stat StatBuf; |
Anders Carlsson | 9583f79 | 2011-03-18 19:23:38 +0000 | [diff] [blame] | 1249 | if (FileMgr->getNoncachedStatValue(F->first(), StatBuf)) { |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1250 | // If we can't stat the file, assume that something horrible happened. |
| 1251 | AnyFileChanged = true; |
| 1252 | } else if (StatBuf.st_size != F->second.first || |
| 1253 | StatBuf.st_mtime != F->second.second) |
| 1254 | AnyFileChanged = true; |
| 1255 | } |
| 1256 | |
| 1257 | if (!AnyFileChanged) { |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1258 | // Okay! We can re-use the precompiled preamble. |
| 1259 | |
| 1260 | // Set the state of the diagnostic object to mimic its state |
| 1261 | // after parsing the preamble. |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1262 | // FIXME: This won't catch any #pragma push warning changes that |
| 1263 | // have occurred in the preamble. |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1264 | getDiagnostics().Reset(); |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1265 | ProcessWarningOptions(getDiagnostics(), |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1266 | PreambleInvocation->getDiagnosticOpts()); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1267 | getDiagnostics().setNumWarnings(NumWarningsInPreamble); |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1268 | |
| 1269 | // Create a version of the main file buffer that is padded to |
| 1270 | // buffer size we reserved when creating the preamble. |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1271 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1272 | PreambleReservedSize, |
| 1273 | FrontendOpts.Inputs[0].second); |
| 1274 | } |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1275 | } |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1276 | |
| 1277 | // If we aren't allowed to rebuild the precompiled preamble, just |
| 1278 | // return now. |
| 1279 | if (!AllowRebuild) |
| 1280 | return 0; |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1282 | // We can't reuse the previously-computed preamble. Build a new one. |
| 1283 | Preamble.clear(); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1284 | PreambleDiagnostics.clear(); |
Douglas Gregor | 15ba0b3 | 2010-07-30 20:58:08 +0000 | [diff] [blame] | 1285 | llvm::sys::Path(PreambleFile).eraseFromDisk(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1286 | PreambleRebuildCounter = 1; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1287 | } else if (!AllowRebuild) { |
| 1288 | // We aren't allowed to rebuild the precompiled preamble; just |
| 1289 | // return now. |
| 1290 | return 0; |
| 1291 | } |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1292 | |
| 1293 | // If the preamble rebuild counter > 1, it's because we previously |
| 1294 | // failed to build a preamble and we're not yet ready to try |
| 1295 | // again. Decrement the counter and return a failure. |
| 1296 | if (PreambleRebuildCounter > 1) { |
| 1297 | --PreambleRebuildCounter; |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
Douglas Gregor | e10f0e5 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1301 | // Create a temporary file for the precompiled preamble. In rare |
| 1302 | // circumstances, this can fail. |
| 1303 | std::string PreamblePCHPath = GetPreamblePCHPath(); |
| 1304 | if (PreamblePCHPath.empty()) { |
| 1305 | // Try again next time. |
| 1306 | PreambleRebuildCounter = 1; |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1310 | // We did not previously compute a preamble, or it can't be reused anyway. |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1311 | SimpleTimer PreambleTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1312 | PreambleTimer.setOutput("Precompiling preamble"); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1313 | |
| 1314 | // Create a new buffer that stores the preamble. The buffer also contains |
| 1315 | // extra space for the original contents of the file (which will be present |
| 1316 | // when we actually parse the file) along with more room in case the file |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1317 | // grows. |
| 1318 | PreambleReservedSize = NewPreamble.first->getBufferSize(); |
| 1319 | if (PreambleReservedSize < 4096) |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1320 | PreambleReservedSize = 8191; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1321 | else |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1322 | PreambleReservedSize *= 2; |
| 1323 | |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1324 | // Save the preamble text for later; we'll need to compare against it for |
| 1325 | // subsequent reparses. |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 1326 | StringRef MainFilename = PreambleInvocation->getFrontendOpts().Inputs[0].second; |
| 1327 | Preamble.assign(FileMgr->getFile(MainFilename), |
| 1328 | NewPreamble.first->getBufferStart(), |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1329 | NewPreamble.first->getBufferStart() |
| 1330 | + NewPreamble.second.first); |
| 1331 | PreambleEndsAtStartOfLine = NewPreamble.second.second; |
| 1332 | |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1333 | delete PreambleBuffer; |
| 1334 | PreambleBuffer |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1335 | = llvm::MemoryBuffer::getNewUninitMemBuffer(PreambleReservedSize, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1336 | FrontendOpts.Inputs[0].second); |
| 1337 | memcpy(const_cast<char*>(PreambleBuffer->getBufferStart()), |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1338 | NewPreamble.first->getBufferStart(), Preamble.size()); |
| 1339 | memset(const_cast<char*>(PreambleBuffer->getBufferStart()) + Preamble.size(), |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1340 | ' ', PreambleReservedSize - Preamble.size() - 1); |
| 1341 | const_cast<char*>(PreambleBuffer->getBufferEnd())[-1] = '\n'; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1342 | |
| 1343 | // Remap the main source file to the preamble buffer. |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1344 | llvm::sys::PathWithStatus MainFilePath(FrontendOpts.Inputs[0].second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1345 | PreprocessorOpts.addRemappedFile(MainFilePath.str(), PreambleBuffer); |
| 1346 | |
| 1347 | // Tell the compiler invocation to generate a temporary precompiled header. |
| 1348 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1349 | // FIXME: Generate the precompiled header into memory? |
Douglas Gregor | e10f0e5 | 2010-09-11 17:56:52 +0000 | [diff] [blame] | 1350 | FrontendOpts.OutputFile = PreamblePCHPath; |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1351 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 1352 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1353 | |
| 1354 | // Create the compiler instance to use for building the precompiled preamble. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1355 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
| 1356 | |
| 1357 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1358 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1359 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1360 | |
Douglas Gregor | 3cc1581 | 2011-07-01 18:22:13 +0000 | [diff] [blame] | 1361 | Clang->setInvocation(&*PreambleInvocation); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1362 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1364 | // Set up diagnostics, capturing all of the diagnostics produced. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1365 | Clang->setDiagnostics(&getDiagnostics()); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Create the target instance. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1368 | Clang->getTargetOpts().Features = TargetFeatures; |
| 1369 | Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), |
| 1370 | Clang->getTargetOpts())); |
| 1371 | if (!Clang->hasTarget()) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1372 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1373 | Preamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1374 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1375 | PreprocessorOpts.eraseRemappedFile( |
| 1376 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1377 | return 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | // Inform the target of the language options. |
| 1381 | // |
| 1382 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1383 | // created. This complexity should be lifted elsewhere. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1384 | Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1385 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1386 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1387 | "Invocation must have exactly one source file!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1388 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1389 | "FIXME: AST inputs not yet supported here!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1390 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1391 | "IR inputs not support here!"); |
| 1392 | |
| 1393 | // Clear out old caches and data. |
Douglas Gregor | bb6a881 | 2010-10-08 04:03:57 +0000 | [diff] [blame] | 1394 | getDiagnostics().Reset(); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1395 | ProcessWarningOptions(getDiagnostics(), Clang->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1396 | StoredDiagnostics.erase( |
| 1397 | StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver, |
| 1398 | StoredDiagnostics.end()); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1399 | TopLevelDecls.clear(); |
| 1400 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Create a file manager object to provide access to and cache the filesystem. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1403 | Clang->setFileManager(new FileManager(Clang->getFileSystemOpts())); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1404 | |
| 1405 | // Create the source manager. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1406 | Clang->setSourceManager(new SourceManager(getDiagnostics(), |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1407 | Clang->getFileManager())); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | 48c8cd3 | 2010-08-03 08:14:03 +0000 | [diff] [blame] | 1409 | llvm::OwningPtr<PrecompilePreambleAction> Act; |
| 1410 | Act.reset(new PrecompilePreambleAction(*this)); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1411 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second, |
| 1412 | Clang->getFrontendOpts().Inputs[0].first)) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1413 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1414 | Preamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1415 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1416 | PreprocessorOpts.eraseRemappedFile( |
| 1417 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1418 | return 0; |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | Act->Execute(); |
| 1422 | Act->EndSourceFile(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1424 | if (Diagnostics->hasErrorOccurred()) { |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1425 | // There were errors parsing the preamble, so no precompiled header was |
| 1426 | // generated. Forget that we even tried. |
Douglas Gregor | a6f74e2 | 2010-09-27 16:43:25 +0000 | [diff] [blame] | 1427 | // FIXME: Should we leave a note for ourselves to try again? |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1428 | llvm::sys::Path(FrontendOpts.OutputFile).eraseFromDisk(); |
| 1429 | Preamble.clear(); |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1430 | TopLevelDeclsInPreamble.clear(); |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1431 | PreambleRebuildCounter = DefaultPreambleRebuildInterval; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1432 | PreprocessorOpts.eraseRemappedFile( |
| 1433 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1434 | return 0; |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1437 | // Transfer any diagnostics generated when parsing the preamble into the set |
| 1438 | // of preamble diagnostics. |
| 1439 | PreambleDiagnostics.clear(); |
| 1440 | PreambleDiagnostics.insert(PreambleDiagnostics.end(), |
| 1441 | StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver, |
| 1442 | StoredDiagnostics.end()); |
| 1443 | StoredDiagnostics.erase( |
| 1444 | StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver, |
| 1445 | StoredDiagnostics.end()); |
| 1446 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1447 | // Keep track of the preamble we precompiled. |
| 1448 | PreambleFile = FrontendOpts.OutputFile; |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1449 | NumWarningsInPreamble = getDiagnostics().getNumWarnings(); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1450 | |
| 1451 | // Keep track of all of the files that the source manager knows about, |
| 1452 | // so we can verify whether they have changed or not. |
| 1453 | FilesInPreamble.clear(); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 1454 | SourceManager &SourceMgr = Clang->getSourceManager(); |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1455 | const llvm::MemoryBuffer *MainFileBuffer |
| 1456 | = SourceMgr.getBuffer(SourceMgr.getMainFileID()); |
| 1457 | for (SourceManager::fileinfo_iterator F = SourceMgr.fileinfo_begin(), |
| 1458 | FEnd = SourceMgr.fileinfo_end(); |
| 1459 | F != FEnd; |
| 1460 | ++F) { |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1461 | const FileEntry *File = F->second->OrigEntry; |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1462 | if (!File || F->second->getRawBuffer() == MainFileBuffer) |
| 1463 | continue; |
| 1464 | |
| 1465 | FilesInPreamble[File->getName()] |
| 1466 | = std::make_pair(F->second->getSize(), File->getModificationTime()); |
| 1467 | } |
| 1468 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1469 | PreambleRebuildCounter = 1; |
Douglas Gregor | a0734c5 | 2010-08-19 01:33:06 +0000 | [diff] [blame] | 1470 | PreprocessorOpts.eraseRemappedFile( |
| 1471 | PreprocessorOpts.remapped_file_buffer_end() - 1); |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 1472 | |
| 1473 | // If the hash of top-level entities differs from the hash of the top-level |
| 1474 | // entities the last time we rebuilt the preamble, clear out the completion |
| 1475 | // cache. |
| 1476 | if (CurrentTopLevelHashValue != PreambleTopLevelHashValue) { |
| 1477 | CompletionCacheTopLevelHashValue = 0; |
| 1478 | PreambleTopLevelHashValue = CurrentTopLevelHashValue; |
| 1479 | } |
| 1480 | |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1481 | return CreatePaddedMainFileBuffer(NewPreamble.first, |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1482 | PreambleReservedSize, |
| 1483 | FrontendOpts.Inputs[0].second); |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1484 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | e9db88f | 2010-08-03 19:06:41 +0000 | [diff] [blame] | 1486 | void ASTUnit::RealizeTopLevelDeclsFromPreamble() { |
| 1487 | std::vector<Decl *> Resolved; |
| 1488 | Resolved.reserve(TopLevelDeclsInPreamble.size()); |
| 1489 | ExternalASTSource &Source = *getASTContext().getExternalSource(); |
| 1490 | for (unsigned I = 0, N = TopLevelDeclsInPreamble.size(); I != N; ++I) { |
| 1491 | // Resolve the declaration ID to an actual declaration, possibly |
| 1492 | // deserializing the declaration in the process. |
| 1493 | Decl *D = Source.GetExternalDecl(TopLevelDeclsInPreamble[I]); |
| 1494 | if (D) |
| 1495 | Resolved.push_back(D); |
| 1496 | } |
| 1497 | TopLevelDeclsInPreamble.clear(); |
| 1498 | TopLevelDecls.insert(TopLevelDecls.begin(), Resolved.begin(), Resolved.end()); |
| 1499 | } |
| 1500 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1501 | StringRef ASTUnit::getMainFileName() const { |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1502 | return Invocation->getFrontendOpts().Inputs[0].second; |
| 1503 | } |
| 1504 | |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1505 | ASTUnit *ASTUnit::create(CompilerInvocation *CI, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1506 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags) { |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1507 | llvm::OwningPtr<ASTUnit> AST; |
| 1508 | AST.reset(new ASTUnit(false)); |
| 1509 | ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics=*/false); |
| 1510 | AST->Diagnostics = Diags; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1511 | AST->Invocation = CI; |
Anders Carlsson | c30dcec | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 1512 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1513 | AST->FileMgr = new FileManager(AST->FileSystemOpts); |
| 1514 | AST->SourceMgr = new SourceManager(*Diags, *AST->FileMgr); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 1515 | |
| 1516 | return AST.take(); |
| 1517 | } |
| 1518 | |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1519 | ASTUnit *ASTUnit::LoadFromCompilerInvocationAction(CompilerInvocation *CI, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1520 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1521 | ASTFrontendAction *Action) { |
| 1522 | assert(CI && "A CompilerInvocation is required"); |
| 1523 | |
| 1524 | // Create the AST unit. |
| 1525 | llvm::OwningPtr<ASTUnit> AST; |
| 1526 | AST.reset(new ASTUnit(false)); |
| 1527 | ConfigureDiags(Diags, 0, 0, *AST, /*CaptureDiagnostics*/false); |
| 1528 | AST->Diagnostics = Diags; |
| 1529 | AST->OnlyLocalDecls = false; |
| 1530 | AST->CaptureDiagnostics = false; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1531 | AST->TUKind = Action ? Action->getTranslationUnitKind() : TU_Complete; |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1532 | AST->ShouldCacheCodeCompletionResults = false; |
| 1533 | AST->Invocation = CI; |
| 1534 | |
| 1535 | // Recover resources if we crash before exiting this method. |
| 1536 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1537 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1538 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1539 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Argyrios Kyrtzidis | f1f6759 | 2011-05-03 23:26:34 +0000 | [diff] [blame] | 1540 | DiagCleanup(Diags.getPtr()); |
| 1541 | |
| 1542 | // We'll manage file buffers ourselves. |
| 1543 | CI->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1544 | CI->getFrontendOpts().DisableFree = false; |
| 1545 | ProcessWarningOptions(AST->getDiagnostics(), CI->getDiagnosticOpts()); |
| 1546 | |
| 1547 | // Save the target features. |
| 1548 | AST->TargetFeatures = CI->getTargetOpts().Features; |
| 1549 | |
| 1550 | // Create the compiler instance to use for building the AST. |
| 1551 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
| 1552 | |
| 1553 | // Recover resources if we crash before exiting this method. |
| 1554 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 1555 | CICleanup(Clang.get()); |
| 1556 | |
| 1557 | Clang->setInvocation(CI); |
| 1558 | AST->OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second; |
| 1559 | |
| 1560 | // Set up diagnostics, capturing any diagnostics that would |
| 1561 | // otherwise be dropped. |
| 1562 | Clang->setDiagnostics(&AST->getDiagnostics()); |
| 1563 | |
| 1564 | // Create the target instance. |
| 1565 | Clang->getTargetOpts().Features = AST->TargetFeatures; |
| 1566 | Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), |
| 1567 | Clang->getTargetOpts())); |
| 1568 | if (!Clang->hasTarget()) |
| 1569 | return 0; |
| 1570 | |
| 1571 | // Inform the target of the language options. |
| 1572 | // |
| 1573 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 1574 | // created. This complexity should be lifted elsewhere. |
| 1575 | Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); |
| 1576 | |
| 1577 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
| 1578 | "Invocation must have exactly one source file!"); |
| 1579 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST && |
| 1580 | "FIXME: AST inputs not yet supported here!"); |
| 1581 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
| 1582 | "IR inputs not supported here!"); |
| 1583 | |
| 1584 | // Configure the various subsystems. |
| 1585 | AST->FileSystemOpts = Clang->getFileSystemOpts(); |
| 1586 | AST->FileMgr = new FileManager(AST->FileSystemOpts); |
| 1587 | AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr); |
| 1588 | AST->TheSema.reset(); |
| 1589 | AST->Ctx = 0; |
| 1590 | AST->PP = 0; |
| 1591 | |
| 1592 | // Create a file manager object to provide access to and cache the filesystem. |
| 1593 | Clang->setFileManager(&AST->getFileManager()); |
| 1594 | |
| 1595 | // Create the source manager. |
| 1596 | Clang->setSourceManager(&AST->getSourceManager()); |
| 1597 | |
| 1598 | ASTFrontendAction *Act = Action; |
| 1599 | |
| 1600 | llvm::OwningPtr<TopLevelDeclTrackerAction> TrackerAct; |
| 1601 | if (!Act) { |
| 1602 | TrackerAct.reset(new TopLevelDeclTrackerAction(*AST)); |
| 1603 | Act = TrackerAct.get(); |
| 1604 | } |
| 1605 | |
| 1606 | // Recover resources if we crash before exiting this method. |
| 1607 | llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction> |
| 1608 | ActCleanup(TrackerAct.get()); |
| 1609 | |
| 1610 | if (!Act->BeginSourceFile(*Clang.get(), |
| 1611 | Clang->getFrontendOpts().Inputs[0].second, |
| 1612 | Clang->getFrontendOpts().Inputs[0].first)) |
| 1613 | return 0; |
| 1614 | |
| 1615 | Act->Execute(); |
| 1616 | |
| 1617 | // Steal the created target, context, and preprocessor. |
| 1618 | AST->TheSema.reset(Clang->takeSema()); |
| 1619 | AST->Consumer.reset(Clang->takeASTConsumer()); |
| 1620 | AST->Ctx = &Clang->getASTContext(); |
| 1621 | AST->PP = &Clang->getPreprocessor(); |
| 1622 | Clang->setSourceManager(0); |
| 1623 | Clang->setFileManager(0); |
| 1624 | AST->Target = &Clang->getTarget(); |
| 1625 | |
| 1626 | Act->EndSourceFile(); |
| 1627 | |
| 1628 | return AST.take(); |
| 1629 | } |
| 1630 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1631 | bool ASTUnit::LoadFromCompilerInvocation(bool PrecompilePreamble) { |
| 1632 | if (!Invocation) |
| 1633 | return true; |
| 1634 | |
| 1635 | // We'll manage file buffers ourselves. |
| 1636 | Invocation->getPreprocessorOpts().RetainRemappedFileBuffers = true; |
| 1637 | Invocation->getFrontendOpts().DisableFree = false; |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1638 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | ffd6dc4 | 2011-01-27 18:02:58 +0000 | [diff] [blame] | 1640 | // Save the target features. |
| 1641 | TargetFeatures = Invocation->getTargetOpts().Features; |
| 1642 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1643 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | f5a1854 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 1644 | if (PrecompilePreamble) { |
Douglas Gregor | c659292 | 2010-11-15 23:00:34 +0000 | [diff] [blame] | 1645 | PreambleRebuildCounter = 2; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1646 | OverrideMainBuffer |
| 1647 | = getMainBufferWithPrecompiledPreamble(*Invocation); |
| 1648 | } |
| 1649 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1650 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1651 | ParsingTimer.setOutput("Parsing " + getMainFileName()); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1652 | |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1653 | // Recover resources if we crash before exiting this method. |
| 1654 | llvm::CrashRecoveryContextCleanupRegistrar<llvm::MemoryBuffer> |
| 1655 | MemBufferCleanup(OverrideMainBuffer); |
| 1656 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1657 | return Parse(OverrideMainBuffer); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1660 | ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1661 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1662 | bool OnlyLocalDecls, |
Douglas Gregor | be2d8c6 | 2010-07-23 00:33:23 +0000 | [diff] [blame] | 1663 | bool CaptureDiagnostics, |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1664 | bool PrecompilePreamble, |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1665 | TranslationUnitKind TUKind, |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 1666 | bool CacheCodeCompletionResults, |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 1667 | bool NestedMacroExpansions) { |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1668 | // Create the AST unit. |
| 1669 | llvm::OwningPtr<ASTUnit> AST; |
| 1670 | AST.reset(new ASTUnit(false)); |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1671 | ConfigureDiags(Diags, 0, 0, *AST, CaptureDiagnostics); |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1672 | AST->Diagnostics = Diags; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1673 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1674 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1675 | AST->TUKind = TUKind; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1676 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1677 | AST->Invocation = CI; |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 1678 | AST->NestedMacroExpansions = NestedMacroExpansions; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1679 | |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1680 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1681 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1682 | ASTUnitCleanup(AST.get()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1683 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1684 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1685 | DiagCleanup(Diags.getPtr()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1686 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1687 | return AST->LoadFromCompilerInvocation(PrecompilePreamble)? 0 : AST.take(); |
Daniel Dunbar | 764c082 | 2009-12-01 09:51:01 +0000 | [diff] [blame] | 1688 | } |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1689 | |
| 1690 | ASTUnit *ASTUnit::LoadFromCommandLine(const char **ArgBegin, |
| 1691 | const char **ArgEnd, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1692 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1693 | StringRef ResourceFilesPath, |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1694 | bool OnlyLocalDecls, |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1695 | bool CaptureDiagnostics, |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1696 | RemappedFile *RemappedFiles, |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1697 | unsigned NumRemappedFiles, |
Argyrios Kyrtzidis | 97d3a38 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 1698 | bool RemappedFilesKeepOriginalName, |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 1699 | bool PrecompilePreamble, |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1700 | TranslationUnitKind TUKind, |
Douglas Gregor | f5a1854 | 2010-10-27 17:24:53 +0000 | [diff] [blame] | 1701 | bool CacheCodeCompletionResults, |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 1702 | bool NestedMacroExpansions) { |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 1703 | if (!Diags.getPtr()) { |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1704 | // No diagnostics engine was provided, so create our own diagnostics object |
| 1705 | // with the default options. |
| 1706 | DiagnosticOptions DiagOpts; |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1707 | Diags = CompilerInstance::createDiagnostics(DiagOpts, ArgEnd - ArgBegin, |
| 1708 | ArgBegin); |
Douglas Gregor | d03e823 | 2010-04-05 21:10:19 +0000 | [diff] [blame] | 1709 | } |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1711 | SmallVector<StoredDiagnostic, 4> StoredDiagnostics; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1712 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1713 | llvm::IntrusiveRefCntPtr<CompilerInvocation> CI; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1715 | { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1716 | |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1717 | CaptureDroppedDiagnostics Capture(CaptureDiagnostics, *Diags, |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1718 | StoredDiagnostics); |
Daniel Dunbar | fcf2d42 | 2010-01-25 00:44:02 +0000 | [diff] [blame] | 1719 | |
Argyrios Kyrtzidis | 5cf423e | 2011-04-04 23:11:45 +0000 | [diff] [blame] | 1720 | CI = clang::createInvocationFromCommandLine( |
Frits van Bommel | 717d7ed | 2011-07-18 12:00:32 +0000 | [diff] [blame] | 1721 | llvm::makeArrayRef(ArgBegin, ArgEnd), |
| 1722 | Diags); |
Argyrios Kyrtzidis | f606b82 | 2011-04-04 21:38:51 +0000 | [diff] [blame] | 1723 | if (!CI) |
Argyrios Kyrtzidis | bc1f48f | 2011-03-07 22:45:01 +0000 | [diff] [blame] | 1724 | return 0; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1725 | } |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1727 | // Override any files that need remapping |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1728 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 1729 | FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; |
| 1730 | if (const llvm::MemoryBuffer * |
| 1731 | memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { |
| 1732 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, memBuf); |
| 1733 | } else { |
| 1734 | const char *fname = fileOrBuf.get<const char *>(); |
| 1735 | CI->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, fname); |
| 1736 | } |
| 1737 | } |
Argyrios Kyrtzidis | 97d3a38 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 1738 | CI->getPreprocessorOpts().RemappedFilesKeepOriginalName = |
| 1739 | RemappedFilesKeepOriginalName; |
Douglas Gregor | aa98ed9 | 2010-01-23 00:14:00 +0000 | [diff] [blame] | 1740 | |
Daniel Dunbar | a5a166d | 2009-12-15 00:06:45 +0000 | [diff] [blame] | 1741 | // Override the resources path. |
Daniel Dunbar | 6b03ece | 2010-01-30 21:47:16 +0000 | [diff] [blame] | 1742 | CI->getHeaderSearchOpts().ResourceDir = ResourceFilesPath; |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1744 | // Create the AST unit. |
| 1745 | llvm::OwningPtr<ASTUnit> AST; |
| 1746 | AST.reset(new ASTUnit(false)); |
Douglas Gregor | 345c1bc | 2011-01-19 01:02:47 +0000 | [diff] [blame] | 1747 | ConfigureDiags(Diags, ArgBegin, ArgEnd, *AST, CaptureDiagnostics); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1748 | AST->Diagnostics = Diags; |
Anders Carlsson | c30dcec | 2011-03-18 18:22:40 +0000 | [diff] [blame] | 1749 | |
| 1750 | AST->FileSystemOpts = CI->getFileSystemOpts(); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1751 | AST->FileMgr = new FileManager(AST->FileSystemOpts); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1752 | AST->OnlyLocalDecls = OnlyLocalDecls; |
Douglas Gregor | 44c6ee7 | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 1753 | AST->CaptureDiagnostics = CaptureDiagnostics; |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 1754 | AST->TUKind = TUKind; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1755 | AST->ShouldCacheCodeCompletionResults = CacheCodeCompletionResults; |
| 1756 | AST->NumStoredDiagnosticsFromDriver = StoredDiagnostics.size(); |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 1757 | AST->StoredDiagnostics.swap(StoredDiagnostics); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1758 | AST->Invocation = CI; |
Chandler Carruth | de81fc8 | 2011-07-14 09:02:10 +0000 | [diff] [blame] | 1759 | AST->NestedMacroExpansions = NestedMacroExpansions; |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1760 | |
| 1761 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1762 | llvm::CrashRecoveryContextCleanupRegistrar<ASTUnit> |
| 1763 | ASTUnitCleanup(AST.get()); |
| 1764 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInvocation, |
| 1765 | llvm::CrashRecoveryContextReleaseRefCleanup<CompilerInvocation> > |
| 1766 | CICleanup(CI.getPtr()); |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 1767 | llvm::CrashRecoveryContextCleanupRegistrar<DiagnosticsEngine, |
| 1768 | llvm::CrashRecoveryContextReleaseRefCleanup<DiagnosticsEngine> > |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 1769 | DiagCleanup(Diags.getPtr()); |
Ted Kremenek | 4422bfe | 2011-03-18 02:06:56 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | 5159f61 | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 1771 | return AST->LoadFromCompilerInvocation(PrecompilePreamble) ? 0 : AST.take(); |
Daniel Dunbar | 55a17b6 | 2009-12-02 03:23:45 +0000 | [diff] [blame] | 1772 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1773 | |
| 1774 | bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 1775 | if (!Invocation) |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1776 | return true; |
| 1777 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1778 | SimpleTimer ParsingTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 1779 | ParsingTimer.setOutput("Reparsing " + getMainFileName()); |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 1780 | |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1781 | // Remap files. |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1782 | PreprocessorOptions &PPOpts = Invocation->getPreprocessorOpts(); |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 1783 | PPOpts.DisableStatCache = true; |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 1784 | for (PreprocessorOptions::remapped_file_buffer_iterator |
| 1785 | R = PPOpts.remapped_file_buffer_begin(), |
| 1786 | REnd = PPOpts.remapped_file_buffer_end(); |
| 1787 | R != REnd; |
| 1788 | ++R) { |
| 1789 | delete R->second; |
| 1790 | } |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1791 | Invocation->getPreprocessorOpts().clearRemappedFiles(); |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 1792 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
| 1793 | FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; |
| 1794 | if (const llvm::MemoryBuffer * |
| 1795 | memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { |
| 1796 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 1797 | memBuf); |
| 1798 | } else { |
| 1799 | const char *fname = fileOrBuf.get<const char *>(); |
| 1800 | Invocation->getPreprocessorOpts().addRemappedFile(RemappedFiles[I].first, |
| 1801 | fname); |
| 1802 | } |
| 1803 | } |
Douglas Gregor | 0e11955 | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1805 | // If we have a preamble file lying around, or if we might try to |
| 1806 | // build a precompiled preamble, do so now. |
Douglas Gregor | 6481ef1 | 2010-07-24 00:38:13 +0000 | [diff] [blame] | 1807 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
Douglas Gregor | bb420ab | 2010-08-04 05:53:38 +0000 | [diff] [blame] | 1808 | if (!PreambleFile.empty() || PreambleRebuildCounter > 0) |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 1809 | OverrideMainBuffer = getMainBufferWithPrecompiledPreamble(*Invocation); |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1811 | // Clear out the diagnostics state. |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1812 | if (!OverrideMainBuffer) { |
Douglas Gregor | d9a30af | 2010-08-02 20:51:39 +0000 | [diff] [blame] | 1813 | getDiagnostics().Reset(); |
Douglas Gregor | 36e3b5c | 2010-10-11 21:37:58 +0000 | [diff] [blame] | 1814 | ProcessWarningOptions(getDiagnostics(), Invocation->getDiagnosticOpts()); |
| 1815 | } |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1817 | // Parse the sources |
Douglas Gregor | df7a79a | 2011-02-16 18:16:54 +0000 | [diff] [blame] | 1818 | bool Result = Parse(OverrideMainBuffer); |
| 1819 | |
| 1820 | // If we're caching global code-completion results, and the top-level |
| 1821 | // declarations have changed, clear out the code-completion cache. |
| 1822 | if (!Result && ShouldCacheCodeCompletionResults && |
| 1823 | CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue) |
| 1824 | CacheCodeCompletionResults(); |
| 1825 | |
Douglas Gregor | 3f35bb2 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1826 | // We now need to clear out the completion allocator for |
| 1827 | // clang_getCursorCompletionString; it'll be recreated if necessary. |
| 1828 | CursorCompletionAllocator = 0; |
| 1829 | |
Douglas Gregor | 4dde749 | 2010-07-23 23:58:40 +0000 | [diff] [blame] | 1830 | return Result; |
Douglas Gregor | aa21cc4 | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 1831 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1833 | //----------------------------------------------------------------------------// |
| 1834 | // Code completion |
| 1835 | //----------------------------------------------------------------------------// |
| 1836 | |
| 1837 | namespace { |
| 1838 | /// \brief Code completion consumer that combines the cached code-completion |
| 1839 | /// results from an ASTUnit with the code-completion results provided to it, |
| 1840 | /// then passes the result on to |
| 1841 | class AugmentedCodeCompleteConsumer : public CodeCompleteConsumer { |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1842 | unsigned long long NormalContexts; |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1843 | ASTUnit &AST; |
| 1844 | CodeCompleteConsumer &Next; |
| 1845 | |
| 1846 | public: |
| 1847 | AugmentedCodeCompleteConsumer(ASTUnit &AST, CodeCompleteConsumer &Next, |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 1848 | bool IncludeMacros, bool IncludeCodePatterns, |
| 1849 | bool IncludeGlobals) |
| 1850 | : CodeCompleteConsumer(IncludeMacros, IncludeCodePatterns, IncludeGlobals, |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1851 | Next.isOutputBinary()), AST(AST), Next(Next) |
| 1852 | { |
| 1853 | // Compute the set of contexts in which we will look when we don't have |
| 1854 | // any information about the specific context. |
| 1855 | NormalContexts |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1856 | = (1LL << (CodeCompletionContext::CCC_TopLevel - 1)) |
| 1857 | | (1LL << (CodeCompletionContext::CCC_ObjCInterface - 1)) |
| 1858 | | (1LL << (CodeCompletionContext::CCC_ObjCImplementation - 1)) |
| 1859 | | (1LL << (CodeCompletionContext::CCC_ObjCIvarList - 1)) |
| 1860 | | (1LL << (CodeCompletionContext::CCC_Statement - 1)) |
| 1861 | | (1LL << (CodeCompletionContext::CCC_Expression - 1)) |
| 1862 | | (1LL << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1)) |
| 1863 | | (1LL << (CodeCompletionContext::CCC_DotMemberAccess - 1)) |
| 1864 | | (1LL << (CodeCompletionContext::CCC_ArrowMemberAccess - 1)) |
| 1865 | | (1LL << (CodeCompletionContext::CCC_ObjCPropertyAccess - 1)) |
| 1866 | | (1LL << (CodeCompletionContext::CCC_ObjCProtocolName - 1)) |
| 1867 | | (1LL << (CodeCompletionContext::CCC_ParenthesizedExpression - 1)) |
| 1868 | | (1LL << (CodeCompletionContext::CCC_Recovery - 1)); |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1869 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1870 | if (AST.getASTContext().getLangOptions().CPlusPlus) |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1871 | NormalContexts |= (1LL << (CodeCompletionContext::CCC_EnumTag - 1)) |
| 1872 | | (1LL << (CodeCompletionContext::CCC_UnionTag - 1)) |
| 1873 | | (1LL << (CodeCompletionContext::CCC_ClassOrStructTag - 1)); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | virtual void ProcessCodeCompleteResults(Sema &S, |
| 1877 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1878 | CodeCompletionResult *Results, |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1879 | unsigned NumResults); |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1880 | |
| 1881 | virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg, |
| 1882 | OverloadCandidate *Candidates, |
| 1883 | unsigned NumCandidates) { |
| 1884 | Next.ProcessOverloadCandidates(S, CurrentArg, Candidates, NumCandidates); |
| 1885 | } |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | bcbf46c | 2011-02-01 22:57:45 +0000 | [diff] [blame] | 1887 | virtual CodeCompletionAllocator &getAllocator() { |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 1888 | return Next.getAllocator(); |
| 1889 | } |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 1890 | }; |
| 1891 | } |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1893 | /// \brief Helper function that computes which global names are hidden by the |
| 1894 | /// local code-completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 1895 | static void CalculateHiddenNames(const CodeCompletionContext &Context, |
| 1896 | CodeCompletionResult *Results, |
| 1897 | unsigned NumResults, |
| 1898 | ASTContext &Ctx, |
| 1899 | llvm::StringSet<llvm::BumpPtrAllocator> &HiddenNames){ |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1900 | bool OnlyTagNames = false; |
| 1901 | switch (Context.getKind()) { |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1902 | case CodeCompletionContext::CCC_Recovery: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1903 | case CodeCompletionContext::CCC_TopLevel: |
| 1904 | case CodeCompletionContext::CCC_ObjCInterface: |
| 1905 | case CodeCompletionContext::CCC_ObjCImplementation: |
| 1906 | case CodeCompletionContext::CCC_ObjCIvarList: |
| 1907 | case CodeCompletionContext::CCC_ClassStructUnion: |
| 1908 | case CodeCompletionContext::CCC_Statement: |
| 1909 | case CodeCompletionContext::CCC_Expression: |
| 1910 | case CodeCompletionContext::CCC_ObjCMessageReceiver: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1911 | case CodeCompletionContext::CCC_DotMemberAccess: |
| 1912 | case CodeCompletionContext::CCC_ArrowMemberAccess: |
| 1913 | case CodeCompletionContext::CCC_ObjCPropertyAccess: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1914 | case CodeCompletionContext::CCC_Namespace: |
| 1915 | case CodeCompletionContext::CCC_Type: |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1916 | case CodeCompletionContext::CCC_Name: |
| 1917 | case CodeCompletionContext::CCC_PotentiallyQualifiedName: |
Douglas Gregor | 5e35d59 | 2010-09-14 23:59:36 +0000 | [diff] [blame] | 1918 | case CodeCompletionContext::CCC_ParenthesizedExpression: |
Douglas Gregor | 2c595ad | 2011-07-30 06:55:39 +0000 | [diff] [blame] | 1919 | case CodeCompletionContext::CCC_ObjCInterfaceName: |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1920 | break; |
| 1921 | |
| 1922 | case CodeCompletionContext::CCC_EnumTag: |
| 1923 | case CodeCompletionContext::CCC_UnionTag: |
| 1924 | case CodeCompletionContext::CCC_ClassOrStructTag: |
| 1925 | OnlyTagNames = true; |
| 1926 | break; |
| 1927 | |
| 1928 | case CodeCompletionContext::CCC_ObjCProtocolName: |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 1929 | case CodeCompletionContext::CCC_MacroName: |
| 1930 | case CodeCompletionContext::CCC_MacroNameUse: |
Douglas Gregor | ec00a26 | 2010-08-24 22:20:20 +0000 | [diff] [blame] | 1931 | case CodeCompletionContext::CCC_PreprocessorExpression: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1932 | case CodeCompletionContext::CCC_PreprocessorDirective: |
Douglas Gregor | ea14705 | 2010-08-25 18:04:30 +0000 | [diff] [blame] | 1933 | case CodeCompletionContext::CCC_NaturalLanguage: |
Douglas Gregor | 67c692c | 2010-08-26 15:07:07 +0000 | [diff] [blame] | 1934 | case CodeCompletionContext::CCC_SelectorName: |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 1935 | case CodeCompletionContext::CCC_TypeQualifiers: |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1936 | case CodeCompletionContext::CCC_Other: |
Douglas Gregor | 3a69eaf | 2011-02-18 23:30:37 +0000 | [diff] [blame] | 1937 | case CodeCompletionContext::CCC_OtherWithMacros: |
Douglas Gregor | 2132584 | 2011-07-07 16:03:39 +0000 | [diff] [blame] | 1938 | case CodeCompletionContext::CCC_ObjCInstanceMessage: |
| 1939 | case CodeCompletionContext::CCC_ObjCClassMessage: |
| 1940 | case CodeCompletionContext::CCC_ObjCCategoryName: |
Douglas Gregor | 0de55ce | 2010-08-25 18:41:16 +0000 | [diff] [blame] | 1941 | // We're looking for nothing, or we're looking for names that cannot |
| 1942 | // be hidden. |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1943 | return; |
| 1944 | } |
| 1945 | |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1946 | typedef CodeCompletionResult Result; |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1947 | for (unsigned I = 0; I != NumResults; ++I) { |
| 1948 | if (Results[I].Kind != Result::RK_Declaration) |
| 1949 | continue; |
| 1950 | |
| 1951 | unsigned IDNS |
| 1952 | = Results[I].Declaration->getUnderlyingDecl()->getIdentifierNamespace(); |
| 1953 | |
| 1954 | bool Hiding = false; |
| 1955 | if (OnlyTagNames) |
| 1956 | Hiding = (IDNS & Decl::IDNS_Tag); |
| 1957 | else { |
| 1958 | unsigned HiddenIDNS = (Decl::IDNS_Type | Decl::IDNS_Member | |
Douglas Gregor | 59cab55 | 2010-08-16 23:05:20 +0000 | [diff] [blame] | 1959 | Decl::IDNS_Namespace | Decl::IDNS_Ordinary | |
| 1960 | Decl::IDNS_NonMemberOperator); |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1961 | if (Ctx.getLangOptions().CPlusPlus) |
| 1962 | HiddenIDNS |= Decl::IDNS_Tag; |
| 1963 | Hiding = (IDNS & HiddenIDNS); |
| 1964 | } |
| 1965 | |
| 1966 | if (!Hiding) |
| 1967 | continue; |
| 1968 | |
| 1969 | DeclarationName Name = Results[I].Declaration->getDeclName(); |
| 1970 | if (IdentifierInfo *Identifier = Name.getAsIdentifierInfo()) |
| 1971 | HiddenNames.insert(Identifier->getName()); |
| 1972 | else |
| 1973 | HiddenNames.insert(Name.getAsString()); |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1978 | void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, |
| 1979 | CodeCompletionContext Context, |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1980 | CodeCompletionResult *Results, |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1981 | unsigned NumResults) { |
| 1982 | // Merge the results we were given with the results we cached. |
| 1983 | bool AddedResult = false; |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1984 | unsigned InContexts |
Douglas Gregor | 0ac4138 | 2010-09-23 23:01:17 +0000 | [diff] [blame] | 1985 | = (Context.getKind() == CodeCompletionContext::CCC_Recovery? NormalContexts |
NAKAMURA Takumi | 203f87c | 2011-08-17 01:46:16 +0000 | [diff] [blame] | 1986 | : (1ULL << (Context.getKind() - 1))); |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 1987 | // Contains the set of names that are hidden by "local" completion results. |
Ted Kremenek | 6a15337 | 2010-11-07 06:11:36 +0000 | [diff] [blame] | 1988 | llvm::StringSet<llvm::BumpPtrAllocator> HiddenNames; |
John McCall | 276321a | 2010-08-25 06:19:51 +0000 | [diff] [blame] | 1989 | typedef CodeCompletionResult Result; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1990 | SmallVector<Result, 8> AllResults; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1991 | for (ASTUnit::cached_completion_iterator |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 1992 | C = AST.cached_completion_begin(), |
| 1993 | CEnd = AST.cached_completion_end(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 1994 | C != CEnd; ++C) { |
| 1995 | // If the context we are in matches any of the contexts we are |
| 1996 | // interested in, we'll add this result. |
| 1997 | if ((C->ShowInContexts & InContexts) == 0) |
| 1998 | continue; |
| 1999 | |
| 2000 | // If we haven't added any results previously, do so now. |
| 2001 | if (!AddedResult) { |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2002 | CalculateHiddenNames(Context, Results, NumResults, S.Context, |
| 2003 | HiddenNames); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2004 | AllResults.insert(AllResults.end(), Results, Results + NumResults); |
| 2005 | AddedResult = true; |
| 2006 | } |
| 2007 | |
Douglas Gregor | 6199f2d | 2010-08-16 21:18:39 +0000 | [diff] [blame] | 2008 | // Determine whether this global completion result is hidden by a local |
| 2009 | // completion result. If so, skip it. |
| 2010 | if (C->Kind != CXCursor_MacroDefinition && |
| 2011 | HiddenNames.count(C->Completion->getTypedText())) |
| 2012 | continue; |
| 2013 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2014 | // Adjust priority based on similar type classes. |
| 2015 | unsigned Priority = C->Priority; |
Douglas Gregor | 8850aa3 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 2016 | CXCursorKind CursorKind = C->Kind; |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2017 | CodeCompletionString *Completion = C->Completion; |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2018 | if (!Context.getPreferredType().isNull()) { |
| 2019 | if (C->Kind == CXCursor_MacroDefinition) { |
| 2020 | Priority = getMacroUsagePriority(C->Completion->getTypedText(), |
Douglas Gregor | 9dcf58a | 2010-09-20 21:11:48 +0000 | [diff] [blame] | 2021 | S.getLangOptions(), |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2022 | Context.getPreferredType()->isAnyPointerType()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2023 | } else if (C->Type) { |
| 2024 | CanQualType Expected |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2025 | = S.Context.getCanonicalType( |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2026 | Context.getPreferredType().getUnqualifiedType()); |
| 2027 | SimplifiedTypeClass ExpectedSTC = getSimplifiedTypeClass(Expected); |
| 2028 | if (ExpectedSTC == C->TypeClass) { |
| 2029 | // We know this type is similar; check for an exact match. |
| 2030 | llvm::StringMap<unsigned> &CachedCompletionTypes |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2031 | = AST.getCachedCompletionTypes(); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2032 | llvm::StringMap<unsigned>::iterator Pos |
Douglas Gregor | df23967 | 2010-08-16 21:23:13 +0000 | [diff] [blame] | 2033 | = CachedCompletionTypes.find(QualType(Expected).getAsString()); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2034 | if (Pos != CachedCompletionTypes.end() && Pos->second == C->Type) |
| 2035 | Priority /= CCF_ExactTypeMatch; |
| 2036 | else |
| 2037 | Priority /= CCF_SimilarTypeMatch; |
| 2038 | } |
| 2039 | } |
| 2040 | } |
| 2041 | |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2042 | // Adjust the completion string, if required. |
| 2043 | if (C->Kind == CXCursor_MacroDefinition && |
| 2044 | Context.getKind() == CodeCompletionContext::CCC_MacroNameUse) { |
| 2045 | // Create a new code-completion string that just contains the |
| 2046 | // macro name, without its arguments. |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2047 | CodeCompletionBuilder Builder(getAllocator(), CCP_CodePattern, |
| 2048 | C->Availability); |
| 2049 | Builder.AddTypedTextChunk(C->Completion->getTypedText()); |
Douglas Gregor | 8850aa3 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 2050 | CursorKind = CXCursor_NotImplemented; |
| 2051 | Priority = CCP_CodePattern; |
Douglas Gregor | b278aaf | 2011-02-01 19:23:04 +0000 | [diff] [blame] | 2052 | Completion = Builder.TakeString(); |
Douglas Gregor | 1278510 | 2010-08-24 20:21:13 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Douglas Gregor | 8850aa3 | 2010-08-25 18:03:13 +0000 | [diff] [blame] | 2055 | AllResults.push_back(Result(Completion, Priority, CursorKind, |
Douglas Gregor | f757a12 | 2010-08-23 23:00:57 +0000 | [diff] [blame] | 2056 | C->Availability)); |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | // If we did not add any cached completion results, just forward the |
| 2060 | // results we were given to the next consumer. |
| 2061 | if (!AddedResult) { |
| 2062 | Next.ProcessCodeCompleteResults(S, Context, Results, NumResults); |
| 2063 | return; |
| 2064 | } |
Douglas Gregor | 49f67ce | 2010-08-26 13:48:20 +0000 | [diff] [blame] | 2065 | |
Douglas Gregor | d46cf18 | 2010-08-16 20:01:48 +0000 | [diff] [blame] | 2066 | Next.ProcessCodeCompleteResults(S, Context, AllResults.data(), |
| 2067 | AllResults.size()); |
| 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2072 | void ASTUnit::CodeComplete(StringRef File, unsigned Line, unsigned Column, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2073 | RemappedFile *RemappedFiles, |
| 2074 | unsigned NumRemappedFiles, |
Douglas Gregor | b68bc59 | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 2075 | bool IncludeMacros, |
| 2076 | bool IncludeCodePatterns, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2077 | CodeCompleteConsumer &Consumer, |
David Blaikie | 9c902b5 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 2078 | DiagnosticsEngine &Diag, LangOptions &LangOpts, |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2079 | SourceManager &SourceMgr, FileManager &FileMgr, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2080 | SmallVectorImpl<StoredDiagnostic> &StoredDiagnostics, |
| 2081 | SmallVectorImpl<const llvm::MemoryBuffer *> &OwnedBuffers) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2082 | if (!Invocation) |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2083 | return; |
| 2084 | |
Douglas Gregor | 16896c4 | 2010-10-28 15:44:59 +0000 | [diff] [blame] | 2085 | SimpleTimer CompletionTimer(WantTiming); |
Benjamin Kramer | f2e5a91 | 2010-11-09 20:00:56 +0000 | [diff] [blame] | 2086 | CompletionTimer.setOutput("Code completion @ " + File + ":" + |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2087 | Twine(Line) + ":" + Twine(Column)); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2088 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2089 | llvm::IntrusiveRefCntPtr<CompilerInvocation> |
| 2090 | CCInvocation(new CompilerInvocation(*Invocation)); |
| 2091 | |
| 2092 | FrontendOptions &FrontendOpts = CCInvocation->getFrontendOpts(); |
| 2093 | PreprocessorOptions &PreprocessorOpts = CCInvocation->getPreprocessorOpts(); |
Douglas Gregor | b68bc59 | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 2094 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2095 | FrontendOpts.ShowMacrosInCodeCompletion |
| 2096 | = IncludeMacros && CachedCompletionResults.empty(); |
Douglas Gregor | b68bc59 | 2010-08-05 09:09:23 +0000 | [diff] [blame] | 2097 | FrontendOpts.ShowCodePatternsInCodeCompletion = IncludeCodePatterns; |
Douglas Gregor | 3998219 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 2098 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion |
| 2099 | = CachedCompletionResults.empty(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2100 | FrontendOpts.CodeCompletionAt.FileName = File; |
| 2101 | FrontendOpts.CodeCompletionAt.Line = Line; |
| 2102 | FrontendOpts.CodeCompletionAt.Column = Column; |
| 2103 | |
| 2104 | // Set the language options appropriately. |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2105 | LangOpts = CCInvocation->getLangOpts(); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2106 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2107 | llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance()); |
| 2108 | |
| 2109 | // Recover resources if we crash before exiting this method. |
Ted Kremenek | 022a490 | 2011-03-22 01:15:24 +0000 | [diff] [blame] | 2110 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> |
| 2111 | CICleanup(Clang.get()); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2112 | |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2113 | Clang->setInvocation(&*CCInvocation); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2114 | OriginalSourceFile = Clang->getFrontendOpts().Inputs[0].second; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Set up diagnostics, capturing any diagnostics produced. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2117 | Clang->setDiagnostics(&Diag); |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2118 | ProcessWarningOptions(Diag, CCInvocation->getDiagnosticOpts()); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2119 | CaptureDroppedDiagnostics Capture(true, |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2120 | Clang->getDiagnostics(), |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2121 | StoredDiagnostics); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2122 | |
| 2123 | // Create the target instance. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2124 | Clang->getTargetOpts().Features = TargetFeatures; |
| 2125 | Clang->setTarget(TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), |
| 2126 | Clang->getTargetOpts())); |
| 2127 | if (!Clang->hasTarget()) { |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2128 | Clang->setInvocation(0); |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 2129 | return; |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | // Inform the target of the language options. |
| 2133 | // |
| 2134 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 2135 | // created. This complexity should be lifted elsewhere. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2136 | Clang->getTarget().setForcedLangOptions(Clang->getLangOpts()); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2137 | |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2138 | assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2139 | "Invocation must have exactly one source file!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2140 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_AST && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2141 | "FIXME: AST inputs not yet supported here!"); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2142 | assert(Clang->getFrontendOpts().Inputs[0].first != IK_LLVM_IR && |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2143 | "IR inputs not support here!"); |
| 2144 | |
| 2145 | |
| 2146 | // Use the source and file managers that we were given. |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2147 | Clang->setFileManager(&FileMgr); |
| 2148 | Clang->setSourceManager(&SourceMgr); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2149 | |
| 2150 | // Remap files. |
| 2151 | PreprocessorOpts.clearRemappedFiles(); |
Douglas Gregor | d8a5dba | 2010-08-04 17:07:00 +0000 | [diff] [blame] | 2152 | PreprocessorOpts.RetainRemappedFileBuffers = true; |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2153 | for (unsigned I = 0; I != NumRemappedFiles; ++I) { |
Argyrios Kyrtzidis | 11e6f0a | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 2154 | FilenameOrMemBuf fileOrBuf = RemappedFiles[I].second; |
| 2155 | if (const llvm::MemoryBuffer * |
| 2156 | memBuf = fileOrBuf.dyn_cast<const llvm::MemoryBuffer *>()) { |
| 2157 | PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, memBuf); |
| 2158 | OwnedBuffers.push_back(memBuf); |
| 2159 | } else { |
| 2160 | const char *fname = fileOrBuf.get<const char *>(); |
| 2161 | PreprocessorOpts.addRemappedFile(RemappedFiles[I].first, fname); |
| 2162 | } |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2163 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | b14904c | 2010-08-13 22:48:40 +0000 | [diff] [blame] | 2165 | // Use the code completion consumer we were given, but adding any cached |
| 2166 | // code-completion results. |
Douglas Gregor | e9186e6 | 2010-11-29 16:13:56 +0000 | [diff] [blame] | 2167 | AugmentedCodeCompleteConsumer *AugmentedConsumer |
| 2168 | = new AugmentedCodeCompleteConsumer(*this, Consumer, |
| 2169 | FrontendOpts.ShowMacrosInCodeCompletion, |
| 2170 | FrontendOpts.ShowCodePatternsInCodeCompletion, |
| 2171 | FrontendOpts.ShowGlobalSymbolsInCodeCompletion); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2172 | Clang->setCodeCompletionConsumer(AugmentedConsumer); |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2174 | // If we have a precompiled preamble, try to use it. We only allow |
| 2175 | // the use of the precompiled preamble if we're if the completion |
| 2176 | // point is within the main file, after the end of the precompiled |
| 2177 | // preamble. |
| 2178 | llvm::MemoryBuffer *OverrideMainBuffer = 0; |
| 2179 | if (!PreambleFile.empty()) { |
| 2180 | using llvm::sys::FileStatus; |
| 2181 | llvm::sys::PathWithStatus CompleteFilePath(File); |
| 2182 | llvm::sys::PathWithStatus MainPath(OriginalSourceFile); |
| 2183 | if (const FileStatus *CompleteFileStatus = CompleteFilePath.getFileStatus()) |
| 2184 | if (const FileStatus *MainStatus = MainPath.getFileStatus()) |
Argyrios Kyrtzidis | a3deaee | 2011-09-04 03:32:04 +0000 | [diff] [blame] | 2185 | if (CompleteFileStatus->getUniqueID() == MainStatus->getUniqueID() && |
| 2186 | Line > 1) |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2187 | OverrideMainBuffer |
Ted Kremenek | 5e14d39 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 2188 | = getMainBufferWithPrecompiledPreamble(*CCInvocation, false, |
Douglas Gregor | 8e817b6 | 2010-08-25 18:04:15 +0000 | [diff] [blame] | 2189 | Line - 1); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
| 2192 | // If the main file has been overridden due to the use of a preamble, |
| 2193 | // make that override happen and introduce the preamble. |
Douglas Gregor | 606c4ac | 2011-02-05 19:42:43 +0000 | [diff] [blame] | 2194 | PreprocessorOpts.DisableStatCache = true; |
Douglas Gregor | 7bb8af6 | 2010-10-12 00:50:20 +0000 | [diff] [blame] | 2195 | StoredDiagnostics.insert(StoredDiagnostics.end(), |
| 2196 | this->StoredDiagnostics.begin(), |
| 2197 | this->StoredDiagnostics.begin() + NumStoredDiagnosticsFromDriver); |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2198 | if (OverrideMainBuffer) { |
| 2199 | PreprocessorOpts.addRemappedFile(OriginalSourceFile, OverrideMainBuffer); |
| 2200 | PreprocessorOpts.PrecompiledPreambleBytes.first = Preamble.size(); |
| 2201 | PreprocessorOpts.PrecompiledPreambleBytes.second |
| 2202 | = PreambleEndsAtStartOfLine; |
| 2203 | PreprocessorOpts.ImplicitPCHInclude = PreambleFile; |
| 2204 | PreprocessorOpts.DisablePCHValidation = true; |
| 2205 | |
Douglas Gregor | b97b666 | 2010-08-20 00:59:43 +0000 | [diff] [blame] | 2206 | OwnedBuffers.push_back(OverrideMainBuffer); |
Douglas Gregor | 7b02b58 | 2010-08-20 00:02:33 +0000 | [diff] [blame] | 2207 | } else { |
| 2208 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
| 2209 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
Douglas Gregor | 028d3e4 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
Douglas Gregor | 998caea | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 2212 | // Disable the preprocessing record |
| 2213 | PreprocessorOpts.DetailedRecord = false; |
| 2214 | |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2215 | llvm::OwningPtr<SyntaxOnlyAction> Act; |
| 2216 | Act.reset(new SyntaxOnlyAction); |
Ted Kremenek | 84de4a1 | 2011-03-21 18:40:07 +0000 | [diff] [blame] | 2217 | if (Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0].second, |
| 2218 | Clang->getFrontendOpts().Inputs[0].first)) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2219 | if (OverrideMainBuffer) { |
Jonathan D. Turner | 2214acd | 2011-07-22 17:25:03 +0000 | [diff] [blame] | 2220 | std::string ModName = PreambleFile; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2221 | TranslateStoredDiagnostics(Clang->getModuleManager(), ModName, |
| 2222 | getSourceManager(), PreambleDiagnostics, |
| 2223 | StoredDiagnostics); |
| 2224 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2225 | Act->Execute(); |
| 2226 | Act->EndSourceFile(); |
| 2227 | } |
Douglas Gregor | 8e984da | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 2228 | } |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2230 | CXSaveError ASTUnit::Save(StringRef File) { |
Douglas Gregor | 8a60bbe | 2011-07-06 17:40:26 +0000 | [diff] [blame] | 2231 | if (getDiagnostics().hasUnrecoverableErrorOccurred()) |
Douglas Gregor | 30c80fa | 2011-07-06 16:43:36 +0000 | [diff] [blame] | 2232 | return CXSaveError_TranslationErrors; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2233 | |
| 2234 | // Write to a temporary file and later rename it to the actual file, to avoid |
| 2235 | // possible race conditions. |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2236 | llvm::SmallString<128> TempPath; |
| 2237 | TempPath = File; |
| 2238 | TempPath += "-%%%%%%%%"; |
| 2239 | int fd; |
| 2240 | if (llvm::sys::fs::unique_file(TempPath.str(), fd, TempPath, |
| 2241 | /*makeAbsolute=*/false)) |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2242 | return CXSaveError_Unknown; |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2244 | // FIXME: Can we somehow regenerate the stat cache here, or do we need to |
| 2245 | // unconditionally create a stat cache when we parse the file? |
Argyrios Kyrtzidis | 08a2bfd | 2011-07-28 00:45:10 +0000 | [diff] [blame] | 2246 | llvm::raw_fd_ostream Out(fd, /*shouldClose=*/true); |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2247 | |
| 2248 | serialize(Out); |
| 2249 | Out.close(); |
Argyrios Kyrtzidis | 55e7557 | 2011-07-21 18:44:49 +0000 | [diff] [blame] | 2250 | if (Out.has_error()) |
| 2251 | return CXSaveError_Unknown; |
| 2252 | |
| 2253 | if (llvm::error_code ec = llvm::sys::fs::rename(TempPath.str(), File)) { |
| 2254 | bool exists; |
| 2255 | llvm::sys::fs::remove(TempPath.str(), exists); |
| 2256 | return CXSaveError_Unknown; |
| 2257 | } |
| 2258 | |
| 2259 | return CXSaveError_None; |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2262 | bool ASTUnit::serialize(raw_ostream &OS) { |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2263 | if (getDiagnostics().hasErrorOccurred()) |
| 2264 | return true; |
| 2265 | |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2266 | std::vector<unsigned char> Buffer; |
| 2267 | llvm::BitstreamWriter Stream(Buffer); |
Sebastian Redl | 55c0ad5 | 2010-08-18 23:56:21 +0000 | [diff] [blame] | 2268 | ASTWriter Writer(Stream); |
Douglas Gregor | 4a69c2e | 2011-09-01 17:04:32 +0000 | [diff] [blame] | 2269 | // FIXME: Handle modules |
| 2270 | Writer.WriteAST(getSema(), 0, std::string(), /*IsModule=*/false, ""); |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2271 | |
| 2272 | // Write the generated bitstream to "Out". |
Douglas Gregor | 2dd19f1 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 2273 | if (!Buffer.empty()) |
Argyrios Kyrtzidis | 35dcda7 | 2011-03-09 17:21:42 +0000 | [diff] [blame] | 2274 | OS.write((char *)&Buffer.front(), Buffer.size()); |
| 2275 | |
| 2276 | return false; |
Douglas Gregor | e938668 | 2010-08-13 05:36:37 +0000 | [diff] [blame] | 2277 | } |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2278 | |
| 2279 | typedef ContinuousRangeMap<unsigned, int, 2> SLocRemap; |
| 2280 | |
| 2281 | static void TranslateSLoc(SourceLocation &L, SLocRemap &Remap) { |
| 2282 | unsigned Raw = L.getRawEncoding(); |
| 2283 | const unsigned MacroBit = 1U << 31; |
| 2284 | L = SourceLocation::getFromRawEncoding((Raw & MacroBit) | |
| 2285 | ((Raw & ~MacroBit) + Remap.find(Raw & ~MacroBit)->second)); |
| 2286 | } |
| 2287 | |
| 2288 | void ASTUnit::TranslateStoredDiagnostics( |
| 2289 | ASTReader *MMan, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2290 | StringRef ModName, |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2291 | SourceManager &SrcMgr, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2292 | const SmallVectorImpl<StoredDiagnostic> &Diags, |
| 2293 | SmallVectorImpl<StoredDiagnostic> &Out) { |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2294 | // The stored diagnostic has the old source manager in it; update |
| 2295 | // the locations to refer into the new source manager. We also need to remap |
| 2296 | // all the locations to the new view. This includes the diag location, any |
| 2297 | // associated source ranges, and the source ranges of associated fix-its. |
| 2298 | // FIXME: There should be a cleaner way to do this. |
| 2299 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2300 | SmallVector<StoredDiagnostic, 4> Result; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2301 | Result.reserve(Diags.size()); |
| 2302 | assert(MMan && "Don't have a module manager"); |
Jonathan D. Turner | b2b0823 | 2011-07-26 18:21:30 +0000 | [diff] [blame] | 2303 | serialization::Module *Mod = MMan->ModuleMgr.lookup(ModName); |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2304 | assert(Mod && "Don't have preamble module"); |
| 2305 | SLocRemap &Remap = Mod->SLocRemap; |
| 2306 | for (unsigned I = 0, N = Diags.size(); I != N; ++I) { |
| 2307 | // Rebuild the StoredDiagnostic. |
| 2308 | const StoredDiagnostic &SD = Diags[I]; |
| 2309 | SourceLocation L = SD.getLocation(); |
| 2310 | TranslateSLoc(L, Remap); |
| 2311 | FullSourceLoc Loc(L, SrcMgr); |
| 2312 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2313 | SmallVector<CharSourceRange, 4> Ranges; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2314 | Ranges.reserve(SD.range_size()); |
| 2315 | for (StoredDiagnostic::range_iterator I = SD.range_begin(), |
| 2316 | E = SD.range_end(); |
| 2317 | I != E; ++I) { |
| 2318 | SourceLocation BL = I->getBegin(); |
| 2319 | TranslateSLoc(BL, Remap); |
| 2320 | SourceLocation EL = I->getEnd(); |
| 2321 | TranslateSLoc(EL, Remap); |
| 2322 | Ranges.push_back(CharSourceRange(SourceRange(BL, EL), I->isTokenRange())); |
| 2323 | } |
| 2324 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2325 | SmallVector<FixItHint, 2> FixIts; |
Douglas Gregor | 925296b | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 2326 | FixIts.reserve(SD.fixit_size()); |
| 2327 | for (StoredDiagnostic::fixit_iterator I = SD.fixit_begin(), |
| 2328 | E = SD.fixit_end(); |
| 2329 | I != E; ++I) { |
| 2330 | FixIts.push_back(FixItHint()); |
| 2331 | FixItHint &FH = FixIts.back(); |
| 2332 | FH.CodeToInsert = I->CodeToInsert; |
| 2333 | SourceLocation BL = I->RemoveRange.getBegin(); |
| 2334 | TranslateSLoc(BL, Remap); |
| 2335 | SourceLocation EL = I->RemoveRange.getEnd(); |
| 2336 | TranslateSLoc(EL, Remap); |
| 2337 | FH.RemoveRange = CharSourceRange(SourceRange(BL, EL), |
| 2338 | I->RemoveRange.isTokenRange()); |
| 2339 | } |
| 2340 | |
| 2341 | Result.push_back(StoredDiagnostic(SD.getLevel(), SD.getID(), |
| 2342 | SD.getMessage(), Loc, Ranges, FixIts)); |
| 2343 | } |
| 2344 | Result.swap(Out); |
| 2345 | } |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2346 | |
| 2347 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2348 | unsigned Line, unsigned Col) const { |
| 2349 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2350 | SourceLocation Loc = SM.translateFileLineCol(File, Line, Col); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2351 | return SM.getMacroArgExpandedLocation(Loc); |
| 2352 | } |
| 2353 | |
| 2354 | SourceLocation ASTUnit::getLocation(const FileEntry *File, |
| 2355 | unsigned Offset) const { |
| 2356 | const SourceManager &SM = getSourceManager(); |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2357 | SourceLocation FileLoc = SM.translateFileLineCol(File, 1, 1); |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2358 | return SM.getMacroArgExpandedLocation(FileLoc.getLocWithOffset(Offset)); |
| 2359 | } |
| 2360 | |
Argyrios Kyrtzidis | 4cdfcae | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 2361 | /// \brief If \arg Loc is a loaded location from the preamble, returns |
| 2362 | /// the corresponding local location of the main file, otherwise it returns |
| 2363 | /// \arg Loc. |
| 2364 | SourceLocation ASTUnit::mapLocationFromPreamble(SourceLocation Loc) { |
| 2365 | FileID PreambleID; |
| 2366 | if (SourceMgr) |
| 2367 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2368 | |
| 2369 | if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) |
| 2370 | return Loc; |
| 2371 | |
| 2372 | unsigned Offs; |
| 2373 | if (SourceMgr->isInFileID(Loc, PreambleID, &Offs) && Offs < Preamble.size()) { |
| 2374 | SourceLocation FileLoc |
| 2375 | = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID()); |
| 2376 | return FileLoc.getLocWithOffset(Offs); |
| 2377 | } |
| 2378 | |
| 2379 | return Loc; |
| 2380 | } |
| 2381 | |
| 2382 | /// \brief If \arg Loc is a local location of the main file but inside the |
| 2383 | /// preamble chunk, returns the corresponding loaded location from the |
| 2384 | /// preamble, otherwise it returns \arg Loc. |
| 2385 | SourceLocation ASTUnit::mapLocationToPreamble(SourceLocation Loc) { |
| 2386 | FileID PreambleID; |
| 2387 | if (SourceMgr) |
| 2388 | PreambleID = SourceMgr->getPreambleFileID(); |
| 2389 | |
| 2390 | if (Loc.isInvalid() || Preamble.empty() || PreambleID.isInvalid()) |
| 2391 | return Loc; |
| 2392 | |
| 2393 | unsigned Offs; |
| 2394 | if (SourceMgr->isInFileID(Loc, SourceMgr->getMainFileID(), &Offs) && |
| 2395 | Offs < Preamble.size()) { |
| 2396 | SourceLocation FileLoc = SourceMgr->getLocForStartOfFile(PreambleID); |
| 2397 | return FileLoc.getLocWithOffset(Offs); |
| 2398 | } |
| 2399 | |
| 2400 | return Loc; |
| 2401 | } |
| 2402 | |
Argyrios Kyrtzidis | 7c06d86 | 2011-09-19 20:40:35 +0000 | [diff] [blame] | 2403 | void ASTUnit::PreambleData::countLines() const { |
| 2404 | NumLines = 0; |
| 2405 | if (empty()) |
| 2406 | return; |
| 2407 | |
| 2408 | for (std::vector<char>::const_iterator |
| 2409 | I = Buffer.begin(), E = Buffer.end(); I != E; ++I) { |
| 2410 | if (*I == '\n') |
| 2411 | ++NumLines; |
| 2412 | } |
| 2413 | if (Buffer.back() != '\n') |
| 2414 | ++NumLines; |
| 2415 | } |
Argyrios Kyrtzidis | ebf0136 | 2011-10-10 21:57:12 +0000 | [diff] [blame] | 2416 | |
| 2417 | #ifndef NDEBUG |
| 2418 | ASTUnit::ConcurrencyState::ConcurrencyState() { |
| 2419 | Mutex = new llvm::sys::MutexImpl(/*recursive=*/true); |
| 2420 | } |
| 2421 | |
| 2422 | ASTUnit::ConcurrencyState::~ConcurrencyState() { |
| 2423 | delete static_cast<llvm::sys::MutexImpl *>(Mutex); |
| 2424 | } |
| 2425 | |
| 2426 | void ASTUnit::ConcurrencyState::start() { |
| 2427 | bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire(); |
| 2428 | assert(acquired && "Concurrent access to ASTUnit!"); |
| 2429 | } |
| 2430 | |
| 2431 | void ASTUnit::ConcurrencyState::finish() { |
| 2432 | static_cast<llvm::sys::MutexImpl *>(Mutex)->release(); |
| 2433 | } |
| 2434 | |
| 2435 | #else // NDEBUG |
| 2436 | |
| 2437 | ASTUnit::ConcurrencyState::ConcurrencyState() {} |
| 2438 | ASTUnit::ConcurrencyState::~ConcurrencyState() {} |
| 2439 | void ASTUnit::ConcurrencyState::start() {} |
| 2440 | void ASTUnit::ConcurrencyState::finish() {} |
| 2441 | |
| 2442 | #endif |