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