Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 1 | //===--- Preamble.cpp - Reusing expensive parts of the AST ----------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Preamble.h" |
Kadir Cetinkaya | ecd3e67 | 2020-03-11 16:34:01 +0100 | [diff] [blame] | 10 | #include "Compiler.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 11 | #include "Headers.h" |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 12 | #include "SourceCode.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 13 | #include "support/Logger.h" |
Kadir Cetinkaya | 0628705 | 2020-06-17 11:53:32 +0200 | [diff] [blame] | 14 | #include "support/ThreadsafeFS.h" |
Sam McCall | ad97ccf | 2020-04-28 17:49:17 +0200 | [diff] [blame] | 15 | #include "support/Trace.h" |
Sam McCall | 4160f4c | 2020-06-09 15:46:35 +0200 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
| 18 | #include "clang/Basic/LangOptions.h" |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 19 | #include "clang/Basic/SourceLocation.h" |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 21 | #include "clang/Basic/TokenKinds.h" |
| 22 | #include "clang/Frontend/CompilerInvocation.h" |
| 23 | #include "clang/Frontend/FrontendActions.h" |
| 24 | #include "clang/Lex/Lexer.h" |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 25 | #include "clang/Lex/PPCallbacks.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 27 | #include "clang/Lex/PreprocessorOptions.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 28 | #include "clang/Tooling/CompilationDatabase.h" |
| 29 | #include "llvm/ADT/ArrayRef.h" |
Kadir Cetinkaya | b742eaa | 2020-04-02 10:53:45 +0200 | [diff] [blame] | 30 | #include "llvm/ADT/DenseMap.h" |
| 31 | #include "llvm/ADT/DenseSet.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 32 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Kadir Cetinkaya | 2dc2e47 | 2020-06-16 12:16:24 +0200 | [diff] [blame] | 33 | #include "llvm/ADT/None.h" |
| 34 | #include "llvm/ADT/Optional.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
| 36 | #include "llvm/ADT/SmallString.h" |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 37 | #include "llvm/ADT/StringExtras.h" |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 38 | #include "llvm/ADT/StringRef.h" |
| 39 | #include "llvm/ADT/StringSet.h" |
| 40 | #include "llvm/Support/Error.h" |
| 41 | #include "llvm/Support/ErrorHandling.h" |
| 42 | #include "llvm/Support/FormatVariadic.h" |
| 43 | #include "llvm/Support/MemoryBuffer.h" |
| 44 | #include "llvm/Support/Path.h" |
| 45 | #include "llvm/Support/VirtualFileSystem.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
| 47 | #include <iterator> |
| 48 | #include <memory> |
| 49 | #include <string> |
| 50 | #include <system_error> |
| 51 | #include <utility> |
| 52 | #include <vector> |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 53 | |
| 54 | namespace clang { |
| 55 | namespace clangd { |
| 56 | namespace { |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 57 | constexpr llvm::StringLiteral PreamblePatchHeaderName = "__preamble_patch__.h"; |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 58 | |
| 59 | bool compileCommandsAreEqual(const tooling::CompileCommand &LHS, |
| 60 | const tooling::CompileCommand &RHS) { |
| 61 | // We don't check for Output, it should not matter to clangd. |
| 62 | return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename && |
| 63 | llvm::makeArrayRef(LHS.CommandLine).equals(RHS.CommandLine); |
| 64 | } |
| 65 | |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 66 | class CppFilePreambleCallbacks : public PreambleCallbacks { |
| 67 | public: |
| 68 | CppFilePreambleCallbacks(PathRef File, PreambleParsedCallback ParsedCallback) |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 69 | : File(File), ParsedCallback(ParsedCallback) {} |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 70 | |
| 71 | IncludeStructure takeIncludes() { return std::move(Includes); } |
| 72 | |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 73 | MainFileMacros takeMacros() { return std::move(Macros); } |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 74 | |
| 75 | CanonicalIncludes takeCanonicalIncludes() { return std::move(CanonIncludes); } |
| 76 | |
| 77 | void AfterExecute(CompilerInstance &CI) override { |
| 78 | if (!ParsedCallback) |
| 79 | return; |
| 80 | trace::Span Tracer("Running PreambleCallback"); |
| 81 | ParsedCallback(CI.getASTContext(), CI.getPreprocessorPtr(), CanonIncludes); |
| 82 | } |
| 83 | |
| 84 | void BeforeExecute(CompilerInstance &CI) override { |
Ilya Biryukov | 8b76709 | 2019-09-09 15:32:51 +0000 | [diff] [blame] | 85 | CanonIncludes.addSystemHeadersMapping(CI.getLangOpts()); |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 86 | LangOpts = &CI.getLangOpts(); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 87 | SourceMgr = &CI.getSourceManager(); |
| 88 | } |
| 89 | |
| 90 | std::unique_ptr<PPCallbacks> createPPCallbacks() override { |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 91 | assert(SourceMgr && LangOpts && |
| 92 | "SourceMgr and LangOpts must be set at this point"); |
| 93 | |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 94 | return std::make_unique<PPChainedCallbacks>( |
| 95 | collectIncludeStructureCallback(*SourceMgr, &Includes), |
Kadir Cetinkaya | 3755039 | 2020-03-01 16:05:12 +0100 | [diff] [blame] | 96 | std::make_unique<CollectMainFileMacros>(*SourceMgr, Macros)); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | CommentHandler *getCommentHandler() override { |
| 100 | IWYUHandler = collectIWYUHeaderMaps(&CanonIncludes); |
| 101 | return IWYUHandler.get(); |
| 102 | } |
| 103 | |
Sam McCall | 4160f4c | 2020-06-09 15:46:35 +0200 | [diff] [blame] | 104 | bool shouldSkipFunctionBody(Decl *D) override { |
| 105 | // Generally we skip function bodies in preambles for speed. |
| 106 | // We can make exceptions for functions that are cheap to parse and |
| 107 | // instantiate, widely used, and valuable (e.g. commonly produce errors). |
| 108 | if (const auto *FT = llvm::dyn_cast<clang::FunctionTemplateDecl>(D)) { |
| 109 | if (const auto *II = FT->getDeclName().getAsIdentifierInfo()) |
| 110 | // std::make_unique is trivial, and we diagnose bad constructor calls. |
| 111 | if (II->isStr("make_unique") && FT->isInStdNamespace()) |
| 112 | return false; |
| 113 | } |
| 114 | return true; |
| 115 | } |
| 116 | |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 117 | private: |
| 118 | PathRef File; |
| 119 | PreambleParsedCallback ParsedCallback; |
| 120 | IncludeStructure Includes; |
| 121 | CanonicalIncludes CanonIncludes; |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 122 | MainFileMacros Macros; |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 123 | std::unique_ptr<CommentHandler> IWYUHandler = nullptr; |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 124 | const clang::LangOptions *LangOpts = nullptr; |
| 125 | const SourceManager *SourceMgr = nullptr; |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 128 | // Represents directives other than includes, where basic textual information is |
| 129 | // enough. |
| 130 | struct TextualPPDirective { |
| 131 | unsigned DirectiveLine; |
| 132 | // Full text that's representing the directive, including the `#`. |
| 133 | std::string Text; |
| 134 | |
| 135 | bool operator==(const TextualPPDirective &RHS) const { |
| 136 | return std::tie(DirectiveLine, Text) == |
| 137 | std::tie(RHS.DirectiveLine, RHS.Text); |
| 138 | } |
| 139 | }; |
| 140 | |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 141 | // Formats a PP directive consisting of Prefix (e.g. "#define ") and Body ("X |
| 142 | // 10"). The formatting is copied so that the tokens in Body have PresumedLocs |
| 143 | // with correct columns and lines. |
| 144 | std::string spellDirective(llvm::StringRef Prefix, |
| 145 | CharSourceRange DirectiveRange, |
| 146 | const LangOptions &LangOpts, const SourceManager &SM, |
| 147 | unsigned &DirectiveLine) { |
| 148 | std::string SpelledDirective; |
| 149 | llvm::raw_string_ostream OS(SpelledDirective); |
| 150 | OS << Prefix; |
| 151 | |
| 152 | // Make sure DirectiveRange is a char range and doesn't contain macro ids. |
| 153 | DirectiveRange = SM.getExpansionRange(DirectiveRange); |
| 154 | if (DirectiveRange.isTokenRange()) { |
| 155 | DirectiveRange.setEnd( |
| 156 | Lexer::getLocForEndOfToken(DirectiveRange.getEnd(), 0, SM, LangOpts)); |
| 157 | } |
| 158 | |
| 159 | auto DecompLoc = SM.getDecomposedLoc(DirectiveRange.getBegin()); |
| 160 | DirectiveLine = SM.getLineNumber(DecompLoc.first, DecompLoc.second); |
| 161 | auto TargetColumn = SM.getColumnNumber(DecompLoc.first, DecompLoc.second) - 1; |
| 162 | |
| 163 | // Pad with spaces before DirectiveRange to make sure it will be on right |
| 164 | // column when patched. |
| 165 | if (Prefix.size() <= TargetColumn) { |
| 166 | // There is enough space for Prefix and space before directive, use it. |
| 167 | // We try to squeeze the Prefix into the same line whenever we can, as |
| 168 | // putting onto a separate line won't work at the beginning of the file. |
| 169 | OS << std::string(TargetColumn - Prefix.size(), ' '); |
| 170 | } else { |
| 171 | // Prefix was longer than the space we had. We produce e.g.: |
| 172 | // #line N-1 |
| 173 | // #define \ |
| 174 | // X 10 |
| 175 | OS << "\\\n" << std::string(TargetColumn, ' '); |
| 176 | // Decrement because we put an additional line break before |
| 177 | // DirectiveRange.begin(). |
| 178 | --DirectiveLine; |
| 179 | } |
| 180 | OS << toSourceCode(SM, DirectiveRange.getAsRange()); |
| 181 | return OS.str(); |
| 182 | } |
| 183 | |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 184 | // Collects #define directives inside the main file. |
| 185 | struct DirectiveCollector : public PPCallbacks { |
| 186 | DirectiveCollector(const Preprocessor &PP, |
| 187 | std::vector<TextualPPDirective> &TextualDirectives) |
| 188 | : LangOpts(PP.getLangOpts()), SM(PP.getSourceManager()), |
| 189 | TextualDirectives(TextualDirectives) {} |
| 190 | |
| 191 | void FileChanged(SourceLocation Loc, FileChangeReason Reason, |
| 192 | SrcMgr::CharacteristicKind FileType, |
| 193 | FileID PrevFID) override { |
| 194 | InMainFile = SM.isWrittenInMainFile(Loc); |
| 195 | } |
| 196 | |
| 197 | void MacroDefined(const Token &MacroNameTok, |
| 198 | const MacroDirective *MD) override { |
| 199 | if (!InMainFile) |
| 200 | return; |
| 201 | TextualDirectives.emplace_back(); |
| 202 | TextualPPDirective &TD = TextualDirectives.back(); |
| 203 | |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 204 | const auto *MI = MD->getMacroInfo(); |
| 205 | TD.Text = |
| 206 | spellDirective("#define ", |
| 207 | CharSourceRange::getTokenRange( |
| 208 | MI->getDefinitionLoc(), MI->getDefinitionEndLoc()), |
| 209 | LangOpts, SM, TD.DirectiveLine); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | private: |
| 213 | bool InMainFile = true; |
| 214 | const LangOptions &LangOpts; |
| 215 | const SourceManager &SM; |
| 216 | std::vector<TextualPPDirective> &TextualDirectives; |
| 217 | }; |
| 218 | |
| 219 | struct ScannedPreamble { |
| 220 | std::vector<Inclusion> Includes; |
| 221 | std::vector<TextualPPDirective> TextualDirectives; |
Kadir Cetinkaya | 4317ee2 | 2020-06-16 21:21:45 +0200 | [diff] [blame] | 222 | PreambleBounds Bounds = {0, false}; |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | /// Scans the preprocessor directives in the preamble section of the file by |
| 226 | /// running preprocessor over \p Contents. Returned includes do not contain |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 227 | /// resolved paths. \p Cmd is used to build the compiler invocation, which might |
| 228 | /// stat/read files. |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 229 | llvm::Expected<ScannedPreamble> |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 230 | scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) { |
| 231 | class EmptyFS : public ThreadsafeFS { |
| 232 | public: |
| 233 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> |
| 234 | view(llvm::NoneType) const override { |
| 235 | return new llvm::vfs::InMemoryFileSystem; |
| 236 | } |
Kadir Cetinkaya | f693ce4 | 2020-06-04 18:26:52 +0200 | [diff] [blame] | 237 | }; |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 238 | EmptyFS FS; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 239 | // Build and run Preprocessor over the preamble. |
| 240 | ParseInputs PI; |
| 241 | PI.Contents = Contents.str(); |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 242 | PI.TFS = &FS; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 243 | PI.CompileCommand = Cmd; |
| 244 | IgnoringDiagConsumer IgnoreDiags; |
| 245 | auto CI = buildCompilerInvocation(PI, IgnoreDiags); |
| 246 | if (!CI) |
| 247 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 248 | "failed to create compiler invocation"); |
| 249 | CI->getDiagnosticOpts().IgnoreWarnings = true; |
| 250 | auto ContentsBuffer = llvm::MemoryBuffer::getMemBuffer(Contents); |
Kadir Cetinkaya | 34e39eb | 2020-05-05 17:55:11 +0200 | [diff] [blame] | 251 | // This means we're scanning (though not preprocessing) the preamble section |
| 252 | // twice. However, it's important to precisely follow the preamble bounds used |
| 253 | // elsewhere. |
| 254 | auto Bounds = |
| 255 | ComputePreambleBounds(*CI->getLangOpts(), ContentsBuffer.get(), 0); |
| 256 | auto PreambleContents = |
| 257 | llvm::MemoryBuffer::getMemBufferCopy(Contents.substr(0, Bounds.Size)); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 258 | auto Clang = prepareCompilerInstance( |
Kadir Cetinkaya | 34e39eb | 2020-05-05 17:55:11 +0200 | [diff] [blame] | 259 | std::move(CI), nullptr, std::move(PreambleContents), |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 260 | // Provide an empty FS to prevent preprocessor from performing IO. This |
| 261 | // also implies missing resolved paths for includes. |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 262 | FS.view(llvm::None), IgnoreDiags); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 263 | if (Clang->getFrontendOpts().Inputs.empty()) |
| 264 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 265 | "compiler instance had no inputs"); |
| 266 | // We are only interested in main file includes. |
| 267 | Clang->getPreprocessorOpts().SingleFileParseMode = true; |
Kadir Cetinkaya | 34e39eb | 2020-05-05 17:55:11 +0200 | [diff] [blame] | 268 | PreprocessOnlyAction Action; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 269 | if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0])) |
| 270 | return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 271 | "failed BeginSourceFile"); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 272 | const auto &SM = Clang->getSourceManager(); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 273 | Preprocessor &PP = Clang->getPreprocessor(); |
| 274 | IncludeStructure Includes; |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 275 | PP.addPPCallbacks(collectIncludeStructureCallback(SM, &Includes)); |
| 276 | ScannedPreamble SP; |
Kadir Cetinkaya | 4317ee2 | 2020-06-16 21:21:45 +0200 | [diff] [blame] | 277 | SP.Bounds = Bounds; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 278 | PP.addPPCallbacks( |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 279 | std::make_unique<DirectiveCollector>(PP, SP.TextualDirectives)); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 280 | if (llvm::Error Err = Action.Execute()) |
| 281 | return std::move(Err); |
| 282 | Action.EndSourceFile(); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 283 | SP.Includes = std::move(Includes.MainFileIncludes); |
| 284 | return SP; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | const char *spellingForIncDirective(tok::PPKeywordKind IncludeDirective) { |
| 288 | switch (IncludeDirective) { |
| 289 | case tok::pp_include: |
| 290 | return "include"; |
| 291 | case tok::pp_import: |
| 292 | return "import"; |
| 293 | case tok::pp_include_next: |
| 294 | return "include_next"; |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | llvm_unreachable("not an include directive"); |
| 299 | } |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 300 | |
| 301 | // Checks whether \p FileName is a valid spelling of main file. |
| 302 | bool isMainFile(llvm::StringRef FileName, const SourceManager &SM) { |
| 303 | auto FE = SM.getFileManager().getFile(FileName); |
| 304 | return FE && *FE == SM.getFileEntryForID(SM.getMainFileID()); |
| 305 | } |
| 306 | |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 307 | } // namespace |
| 308 | |
Kadir Cetinkaya | ecd3e67 | 2020-03-11 16:34:01 +0100 | [diff] [blame] | 309 | PreambleData::PreambleData(const ParseInputs &Inputs, |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 310 | PrecompiledPreamble Preamble, |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 311 | std::vector<Diag> Diags, IncludeStructure Includes, |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 312 | MainFileMacros Macros, |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 313 | std::unique_ptr<PreambleFileStatusCache> StatCache, |
| 314 | CanonicalIncludes CanonIncludes) |
Kadir Cetinkaya | ecd3e67 | 2020-03-11 16:34:01 +0100 | [diff] [blame] | 315 | : Version(Inputs.Version), CompileCommand(Inputs.CompileCommand), |
| 316 | Preamble(std::move(Preamble)), Diags(std::move(Diags)), |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 317 | Includes(std::move(Includes)), Macros(std::move(Macros)), |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 318 | StatCache(std::move(StatCache)), CanonIncludes(std::move(CanonIncludes)) { |
| 319 | } |
| 320 | |
| 321 | std::shared_ptr<const PreambleData> |
Kadir Cetinkaya | 276a95b | 2020-03-13 11:52:19 +0100 | [diff] [blame] | 322 | buildPreamble(PathRef FileName, CompilerInvocation CI, |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 323 | const ParseInputs &Inputs, bool StoreInMemory, |
| 324 | PreambleParsedCallback PreambleCallback) { |
| 325 | // Note that we don't need to copy the input contents, preamble can live |
| 326 | // without those. |
| 327 | auto ContentsBuffer = |
| 328 | llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName); |
| 329 | auto Bounds = |
| 330 | ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0); |
| 331 | |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 332 | trace::Span Tracer("BuildPreamble"); |
| 333 | SPAN_ATTACH(Tracer, "File", FileName); |
| 334 | StoreDiags PreambleDiagnostics; |
| 335 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> PreambleDiagsEngine = |
| 336 | CompilerInstance::createDiagnostics(&CI.getDiagnosticOpts(), |
| 337 | &PreambleDiagnostics, false); |
| 338 | |
| 339 | // Skip function bodies when building the preamble to speed up building |
| 340 | // the preamble and make it smaller. |
| 341 | assert(!CI.getFrontendOpts().SkipFunctionBodies); |
| 342 | CI.getFrontendOpts().SkipFunctionBodies = true; |
| 343 | // We don't want to write comment locations into PCH. They are racy and slow |
| 344 | // to read back. We rely on dynamic index for the comments instead. |
| 345 | CI.getPreprocessorOpts().WriteCommentListToPCH = false; |
| 346 | |
| 347 | CppFilePreambleCallbacks SerializedDeclsCollector(FileName, PreambleCallback); |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 348 | auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 349 | llvm::SmallString<32> AbsFileName(FileName); |
Kadir Cetinkaya | f693ce4 | 2020-06-04 18:26:52 +0200 | [diff] [blame] | 350 | VFS->makeAbsolute(AbsFileName); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 351 | auto StatCache = std::make_unique<PreambleFileStatusCache>(AbsFileName); |
| 352 | auto BuiltPreamble = PrecompiledPreamble::Build( |
| 353 | CI, ContentsBuffer.get(), Bounds, *PreambleDiagsEngine, |
Kadir Cetinkaya | f693ce4 | 2020-06-04 18:26:52 +0200 | [diff] [blame] | 354 | StatCache->getProducingFS(VFS), |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 355 | std::make_shared<PCHContainerOperations>(), StoreInMemory, |
| 356 | SerializedDeclsCollector); |
| 357 | |
| 358 | // When building the AST for the main file, we do want the function |
| 359 | // bodies. |
| 360 | CI.getFrontendOpts().SkipFunctionBodies = false; |
| 361 | |
| 362 | if (BuiltPreamble) { |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 363 | vlog("Built preamble of size {0} for file {1} version {2}", |
| 364 | BuiltPreamble->getSize(), FileName, Inputs.Version); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 365 | std::vector<Diag> Diags = PreambleDiagnostics.take(); |
| 366 | return std::make_shared<PreambleData>( |
Kadir Cetinkaya | ecd3e67 | 2020-03-11 16:34:01 +0100 | [diff] [blame] | 367 | Inputs, std::move(*BuiltPreamble), std::move(Diags), |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 368 | SerializedDeclsCollector.takeIncludes(), |
Haojian Wu | 7e3c74b | 2019-09-24 11:14:06 +0000 | [diff] [blame] | 369 | SerializedDeclsCollector.takeMacros(), std::move(StatCache), |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 370 | SerializedDeclsCollector.takeCanonicalIncludes()); |
| 371 | } else { |
Adam Czachorowski | 55b92dc | 2020-03-19 15:09:28 +0100 | [diff] [blame] | 372 | elog("Could not build a preamble for file {0} version {1}", FileName, |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 373 | Inputs.Version); |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 374 | return nullptr; |
| 375 | } |
| 376 | } |
| 377 | |
Kadir Cetinkaya | c31367e | 2020-03-15 21:43:00 +0100 | [diff] [blame] | 378 | bool isPreambleCompatible(const PreambleData &Preamble, |
| 379 | const ParseInputs &Inputs, PathRef FileName, |
| 380 | const CompilerInvocation &CI) { |
| 381 | auto ContentsBuffer = |
| 382 | llvm::MemoryBuffer::getMemBuffer(Inputs.Contents, FileName); |
| 383 | auto Bounds = |
| 384 | ComputePreambleBounds(*CI.getLangOpts(), ContentsBuffer.get(), 0); |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 385 | auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory); |
Kadir Cetinkaya | c31367e | 2020-03-15 21:43:00 +0100 | [diff] [blame] | 386 | return compileCommandsAreEqual(Inputs.CompileCommand, |
| 387 | Preamble.CompileCommand) && |
| 388 | Preamble.Preamble.CanReuse(CI, ContentsBuffer.get(), Bounds, |
Kadir Cetinkaya | 3725142 | 2020-06-16 11:02:08 +0200 | [diff] [blame] | 389 | VFS.get()); |
Kadir Cetinkaya | c31367e | 2020-03-15 21:43:00 +0100 | [diff] [blame] | 390 | } |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 391 | |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 392 | void escapeBackslashAndQuotes(llvm::StringRef Text, llvm::raw_ostream &OS) { |
| 393 | for (char C : Text) { |
| 394 | switch (C) { |
| 395 | case '\\': |
| 396 | case '"': |
| 397 | OS << '\\'; |
| 398 | break; |
| 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | OS << C; |
| 403 | } |
| 404 | } |
| 405 | |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 406 | PreamblePatch PreamblePatch::create(llvm::StringRef FileName, |
| 407 | const ParseInputs &Modified, |
| 408 | const PreambleData &Baseline) { |
Kadir Cetinkaya | 20b2af3 | 2020-05-29 12:31:35 +0200 | [diff] [blame] | 409 | trace::Span Tracer("CreatePreamblePatch"); |
| 410 | SPAN_ATTACH(Tracer, "File", FileName); |
Kadir Cetinkaya | b742eaa | 2020-04-02 10:53:45 +0200 | [diff] [blame] | 411 | assert(llvm::sys::path::is_absolute(FileName) && "relative FileName!"); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 412 | // First scan preprocessor directives in Baseline and Modified. These will be |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 413 | // used to figure out newly added directives in Modified. Scanning can fail, |
| 414 | // the code just bails out and creates an empty patch in such cases, as: |
| 415 | // - If scanning for Baseline fails, no knowledge of existing includes hence |
| 416 | // patch will contain all the includes in Modified. Leading to rebuild of |
| 417 | // whole preamble, which is terribly slow. |
| 418 | // - If scanning for Modified fails, cannot figure out newly added ones so |
| 419 | // there's nothing to do but generate an empty patch. |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 420 | auto BaselineScan = scanPreamble( |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 421 | // Contents needs to be null-terminated. |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 422 | Baseline.Preamble.getContents().str(), Modified.CompileCommand); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 423 | if (!BaselineScan) { |
| 424 | elog("Failed to scan baseline of {0}: {1}", FileName, |
| 425 | BaselineScan.takeError()); |
| 426 | return PreamblePatch::unmodified(Baseline); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 427 | } |
Kadir Cetinkaya | d2fcc58 | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 428 | auto ModifiedScan = scanPreamble(Modified.Contents, Modified.CompileCommand); |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 429 | if (!ModifiedScan) { |
| 430 | elog("Failed to scan modified contents of {0}: {1}", FileName, |
| 431 | ModifiedScan.takeError()); |
| 432 | return PreamblePatch::unmodified(Baseline); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 433 | } |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 434 | |
| 435 | bool IncludesChanged = BaselineScan->Includes != ModifiedScan->Includes; |
| 436 | bool DirectivesChanged = |
| 437 | BaselineScan->TextualDirectives != ModifiedScan->TextualDirectives; |
| 438 | if (!IncludesChanged && !DirectivesChanged) |
Kadir Cetinkaya | b742eaa | 2020-04-02 10:53:45 +0200 | [diff] [blame] | 439 | return PreamblePatch::unmodified(Baseline); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 440 | |
| 441 | PreamblePatch PP; |
| 442 | // This shouldn't coincide with any real file name. |
| 443 | llvm::SmallString<128> PatchName; |
| 444 | llvm::sys::path::append(PatchName, llvm::sys::path::parent_path(FileName), |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 445 | PreamblePatchHeaderName); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 446 | PP.PatchFileName = PatchName.str().str(); |
Kadir Cetinkaya | 4317ee2 | 2020-06-16 21:21:45 +0200 | [diff] [blame] | 447 | PP.ModifiedBounds = ModifiedScan->Bounds; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 448 | |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 449 | llvm::raw_string_ostream Patch(PP.PatchContents); |
Kadir Cetinkaya | 717bef6 | 2020-04-23 17:44:51 +0200 | [diff] [blame] | 450 | // Set default filename for subsequent #line directives |
| 451 | Patch << "#line 0 \""; |
| 452 | // FileName part of a line directive is subject to backslash escaping, which |
| 453 | // might lead to problems on windows especially. |
| 454 | escapeBackslashAndQuotes(FileName, Patch); |
| 455 | Patch << "\"\n"; |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 456 | |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 457 | if (IncludesChanged) { |
| 458 | // We are only interested in newly added includes, record the ones in |
| 459 | // Baseline for exclusion. |
| 460 | llvm::DenseMap<std::pair<tok::PPKeywordKind, llvm::StringRef>, |
| 461 | /*Resolved=*/llvm::StringRef> |
| 462 | ExistingIncludes; |
| 463 | for (const auto &Inc : Baseline.Includes.MainFileIncludes) |
| 464 | ExistingIncludes[{Inc.Directive, Inc.Written}] = Inc.Resolved; |
| 465 | // There might be includes coming from disabled regions, record these for |
| 466 | // exclusion too. note that we don't have resolved paths for those. |
| 467 | for (const auto &Inc : BaselineScan->Includes) |
| 468 | ExistingIncludes.try_emplace({Inc.Directive, Inc.Written}); |
| 469 | // Calculate extra includes that needs to be inserted. |
| 470 | for (auto &Inc : ModifiedScan->Includes) { |
| 471 | auto It = ExistingIncludes.find({Inc.Directive, Inc.Written}); |
| 472 | // Include already present in the baseline preamble. Set resolved path and |
| 473 | // put into preamble includes. |
| 474 | if (It != ExistingIncludes.end()) { |
| 475 | Inc.Resolved = It->second.str(); |
| 476 | PP.PreambleIncludes.push_back(Inc); |
| 477 | continue; |
| 478 | } |
| 479 | // Include is new in the modified preamble. Inject it into the patch and |
| 480 | // use #line to set the presumed location to where it is spelled. |
| 481 | auto LineCol = offsetToClangLineColumn(Modified.Contents, Inc.HashOffset); |
| 482 | Patch << llvm::formatv("#line {0}\n", LineCol.first); |
| 483 | Patch << llvm::formatv( |
| 484 | "#{0} {1}\n", spellingForIncDirective(Inc.Directive), Inc.Written); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | if (DirectivesChanged) { |
| 489 | // We need to patch all the directives, since they are order dependent. e.g: |
| 490 | // #define BAR(X) NEW(X) // Newly introduced in Modified |
| 491 | // #define BAR(X) OLD(X) // Exists in the Baseline |
| 492 | // |
| 493 | // If we've patched only the first directive, the macro definition would've |
| 494 | // been wrong for the rest of the file, since patch is applied after the |
| 495 | // baseline preamble. |
| 496 | // |
| 497 | // Note that we deliberately ignore conditional directives and undefs to |
| 498 | // reduce complexity. The former might cause problems because scanning is |
| 499 | // imprecise and might pick directives from disabled regions. |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 500 | for (const auto &TD : ModifiedScan->TextualDirectives) { |
| 501 | Patch << "#line " << TD.DirectiveLine << '\n'; |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 502 | Patch << TD.Text << '\n'; |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 503 | } |
Kadir Cetinkaya | fcde3d5 | 2020-05-14 12:20:33 +0200 | [diff] [blame] | 504 | } |
| 505 | dlog("Created preamble patch: {0}", Patch.str()); |
| 506 | Patch.flush(); |
Kadir Cetinkaya | 2214b90 | 2020-04-02 10:53:23 +0200 | [diff] [blame] | 507 | return PP; |
| 508 | } |
| 509 | |
| 510 | void PreamblePatch::apply(CompilerInvocation &CI) const { |
| 511 | // No need to map an empty file. |
| 512 | if (PatchContents.empty()) |
| 513 | return; |
| 514 | auto &PPOpts = CI.getPreprocessorOpts(); |
| 515 | auto PatchBuffer = |
| 516 | // we copy here to ensure contents are still valid if CI outlives the |
| 517 | // PreamblePatch. |
| 518 | llvm::MemoryBuffer::getMemBufferCopy(PatchContents, PatchFileName); |
| 519 | // CI will take care of the lifetime of the buffer. |
| 520 | PPOpts.addRemappedFile(PatchFileName, PatchBuffer.release()); |
| 521 | // The patch will be parsed after loading the preamble ast and before parsing |
| 522 | // the main file. |
| 523 | PPOpts.Includes.push_back(PatchFileName); |
| 524 | } |
| 525 | |
Kadir Cetinkaya | b742eaa | 2020-04-02 10:53:45 +0200 | [diff] [blame] | 526 | std::vector<Inclusion> PreamblePatch::preambleIncludes() const { |
| 527 | return PreambleIncludes; |
| 528 | } |
| 529 | |
| 530 | PreamblePatch PreamblePatch::unmodified(const PreambleData &Preamble) { |
| 531 | PreamblePatch PP; |
| 532 | PP.PreambleIncludes = Preamble.Includes.MainFileIncludes; |
Kadir Cetinkaya | 4317ee2 | 2020-06-16 21:21:45 +0200 | [diff] [blame] | 533 | PP.ModifiedBounds = Preamble.Preamble.getBounds(); |
Kadir Cetinkaya | b742eaa | 2020-04-02 10:53:45 +0200 | [diff] [blame] | 534 | return PP; |
| 535 | } |
| 536 | |
Kadir Cetinkaya | 538c275 | 2020-05-14 12:26:47 +0200 | [diff] [blame] | 537 | SourceLocation translatePreamblePatchLocation(SourceLocation Loc, |
| 538 | const SourceManager &SM) { |
| 539 | auto DefFile = SM.getFileID(Loc); |
| 540 | if (auto *FE = SM.getFileEntryForID(DefFile)) { |
| 541 | auto IncludeLoc = SM.getIncludeLoc(DefFile); |
| 542 | // Preamble patch is included inside the builtin file. |
| 543 | if (IncludeLoc.isValid() && SM.isWrittenInBuiltinFile(IncludeLoc) && |
| 544 | FE->getName().endswith(PreamblePatchHeaderName)) { |
| 545 | auto Presumed = SM.getPresumedLoc(Loc); |
| 546 | // Check that line directive is pointing at main file. |
| 547 | if (Presumed.isValid() && Presumed.getFileID().isInvalid() && |
| 548 | isMainFile(Presumed.getFilename(), SM)) { |
| 549 | Loc = SM.translateLineCol(SM.getMainFileID(), Presumed.getLine(), |
| 550 | Presumed.getColumn()); |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | return Loc; |
| 555 | } |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 556 | } // namespace clangd |
| 557 | } // namespace clang |