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