[clangd] Patch PragmaMarks in preamble section of the file

Differential Revision: https://reviews.llvm.org/D146026
This commit is contained in:
Kadir Cetinkaya 2023-03-14 10:45:12 +01:00
parent 564ed0bd22
commit 9c888120e3
No known key found for this signature in database
GPG Key ID: E39E36B8D2057ED6
3 changed files with 23 additions and 4 deletions

View File

@ -15,7 +15,9 @@
#include "Protocol.h"
#include "SourceCode.h"
#include "clang-include-cleaner/Record.h"
#include "index/CanonicalIncludes.h"
#include "support/Logger.h"
#include "support/Path.h"
#include "support/ThreadsafeFS.h"
#include "support/Trace.h"
#include "clang/AST/DeclTemplate.h"
@ -27,6 +29,7 @@
#include "clang/Basic/TokenKinds.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/PrecompiledPreamble.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PPCallbacks.h"
@ -42,6 +45,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FormatVariadic.h"
@ -50,10 +54,12 @@
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cstddef>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <system_error>
#include <tuple>
#include <utility>
#include <vector>
@ -318,6 +324,7 @@ struct ScannedPreamble {
// Literal lines of the preamble contents.
std::vector<llvm::StringRef> Lines;
PreambleBounds Bounds = {0, false};
std::vector<PragmaMark> Marks;
};
/// Scans the preprocessor directives in the preamble section of the file by
@ -372,6 +379,8 @@ scanPreamble(llvm::StringRef Contents, const tooling::CompileCommand &Cmd) {
SP.Bounds = Bounds;
PP.addPPCallbacks(
std::make_unique<DirectiveCollector>(PP, SP.TextualDirectives));
PP.addPPCallbacks(
collectPragmaMarksCallback(PP.getSourceManager(), SP.Marks));
if (llvm::Error Err = Action.Execute())
return std::move(Err);
Action.EndSourceFile();
@ -849,6 +858,7 @@ PreamblePatch PreamblePatch::create(llvm::StringRef FileName,
}
PP.PatchedDiags = patchDiags(Baseline.Diags, *BaselineScan, *ModifiedScan);
PP.PatchedMarks = std::move(ModifiedScan->Marks);
dlog("Created preamble patch: {0}", Patch.str());
Patch.flush();
return PP;
@ -902,8 +912,7 @@ bool PreamblePatch::preserveDiagnostics() const {
llvm::ArrayRef<PragmaMark> PreamblePatch::marks() const {
if (PatchContents.empty())
return Baseline->Marks;
// FIXME: Patch pragma marks.
return {};
return PatchedMarks;
}
MainFileMacros PreamblePatch::mainFileMacros() const {

View File

@ -182,6 +182,7 @@ private:
std::vector<Diag> PatchedDiags;
PreambleBounds ModifiedBounds = {0, false};
const PreambleData *Baseline = nullptr;
std::vector<PragmaMark> PatchedMarks;
};
} // namespace clangd

View File

@ -829,6 +829,10 @@ x>)");
}
}
MATCHER_P2(Mark, Range, Text, "") {
return std::tie(arg.Rng, arg.Trivia) == std::tie(Range, Text);
}
TEST(PreamblePatch, MacroAndMarkHandling) {
Config Cfg;
Cfg.Diagnostics.AllowStalePreamble = true;
@ -847,13 +851,18 @@ TEST(PreamblePatch, MacroAndMarkHandling) {
#ifndef FOO
#define FOO
#define BAR
#pragma mark XX
#pragma $x[[mark XX
]]
#pragma $y[[mark YY
]]
#endif)cpp");
auto AST = createPatchedAST(Code.code(), NewCode.code());
// FIXME: Macros and marks have locations that need to be patched.
EXPECT_THAT(AST->getMacros().Names, IsEmpty());
EXPECT_THAT(AST->getMarks(), IsEmpty());
EXPECT_THAT(AST->getMarks(),
UnorderedElementsAre(Mark(NewCode.range("x"), " XX"),
Mark(NewCode.range("y"), " YY")));
}
}