Skip to content

Commit

Permalink
[clangd] Make all calls to format::getStyle() go through getFormatSty…
Browse files Browse the repository at this point in the history
…leForFile() (#82948)
  • Loading branch information
HighCommander4 committed Mar 6, 2024
1 parent 0f3628a commit 5549b01
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 27 deletions.
9 changes: 2 additions & 7 deletions clang-tools-extra/clangd/ClangdServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,10 @@ void ClangdServer::formatOnType(PathRef File, Position Pos,
auto Action = [File = File.str(), Code = std::move(*Code),
TriggerText = TriggerText.str(), CursorPos = *CursorPos,
CB = std::move(CB), this]() mutable {
auto Style = format::getStyle(format::DefaultFormatStyle, File,
format::DefaultFallbackStyle, Code,
TFS.view(/*CWD=*/std::nullopt).get());
if (!Style)
return CB(Style.takeError());

auto Style = getFormatStyleForFile(File, Code, TFS);
std::vector<TextEdit> Result;
for (const tooling::Replacement &R :
formatIncremental(Code, CursorPos, TriggerText, *Style))
formatIncremental(Code, CursorPos, TriggerText, Style))
Result.push_back(replacementToEdit(Code, R));
return CB(Result);
};
Expand Down
15 changes: 5 additions & 10 deletions clang-tools-extra/clangd/IncludeCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,15 @@ bool mayConsiderUnused(const Inclusion &Inc, ParsedAST &AST,

std::vector<Diag> generateMissingIncludeDiagnostics(
ParsedAST &AST, llvm::ArrayRef<MissingIncludeDiagInfo> MissingIncludes,
llvm::StringRef Code, HeaderFilter IgnoreHeaders) {
llvm::StringRef Code, HeaderFilter IgnoreHeaders, const ThreadsafeFS &TFS) {
std::vector<Diag> Result;
const SourceManager &SM = AST.getSourceManager();
const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID());

auto FileStyle = format::getStyle(
format::DefaultFormatStyle, AST.tuPath(), format::DefaultFallbackStyle,
Code, &SM.getFileManager().getVirtualFileSystem());
if (!FileStyle) {
elog("Couldn't infer style", FileStyle.takeError());
FileStyle = format::getLLVMStyle();
}
auto FileStyle = getFormatStyleForFile(AST.tuPath(), Code, TFS);

tooling::HeaderIncludes HeaderIncludes(AST.tuPath(), Code,
FileStyle->IncludeStyle);
FileStyle.IncludeStyle);
for (const auto &SymbolWithMissingInclude : MissingIncludes) {
llvm::StringRef ResolvedPath =
SymbolWithMissingInclude.Providers.front().resolvedPath();
Expand Down Expand Up @@ -459,14 +453,15 @@ bool isPreferredProvider(const Inclusion &Inc,
std::vector<Diag>
issueIncludeCleanerDiagnostics(ParsedAST &AST, llvm::StringRef Code,
const IncludeCleanerFindings &Findings,
const ThreadsafeFS &TFS,
HeaderFilter IgnoreHeaders) {
trace::Span Tracer("IncludeCleaner::issueIncludeCleanerDiagnostics");
std::vector<Diag> UnusedIncludes = generateUnusedIncludeDiagnostics(
AST.tuPath(), Findings.UnusedIncludes, Code, IgnoreHeaders);
std::optional<Fix> RemoveAllUnused = removeAllUnusedIncludes(UnusedIncludes);

std::vector<Diag> MissingIncludeDiags = generateMissingIncludeDiagnostics(
AST, Findings.MissingIncludes, Code, IgnoreHeaders);
AST, Findings.MissingIncludes, Code, IgnoreHeaders, TFS);
std::optional<Fix> AddAllMissing = addAllMissingIncludes(MissingIncludeDiags);

std::optional<Fix> FixAll;
Expand Down
1 change: 1 addition & 0 deletions clang-tools-extra/clangd/IncludeCleaner.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ using HeaderFilter = llvm::ArrayRef<std::function<bool(llvm::StringRef)>>;
std::vector<Diag>
issueIncludeCleanerDiagnostics(ParsedAST &AST, llvm::StringRef Code,
const IncludeCleanerFindings &Findings,
const ThreadsafeFS &TFS,
HeaderFilter IgnoreHeader = {});

/// Affects whether standard library includes should be considered for
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clangd/ParsedAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ void applyWarningOptions(llvm::ArrayRef<std::string> ExtraArgs,
}
}

std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code) {
std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code,
const ThreadsafeFS &TFS) {
auto &Cfg = Config::current();
if (Cfg.Diagnostics.SuppressAll)
return {};
Expand All @@ -377,7 +378,7 @@ std::vector<Diag> getIncludeCleanerDiags(ParsedAST &AST, llvm::StringRef Code) {
Findings.MissingIncludes.clear();
if (SuppressUnused)
Findings.UnusedIncludes.clear();
return issueIncludeCleanerDiagnostics(AST, Code, Findings,
return issueIncludeCleanerDiagnostics(AST, Code, Findings, TFS,
Cfg.Diagnostics.Includes.IgnoreHeader);
}

Expand Down Expand Up @@ -741,7 +742,7 @@ ParsedAST::build(llvm::StringRef Filename, const ParseInputs &Inputs,
std::move(Clang), std::move(Action), std::move(Tokens),
std::move(Macros), std::move(Marks), std::move(ParsedDecls),
std::move(Diags), std::move(Includes), std::move(PI));
llvm::move(getIncludeCleanerDiags(Result, Inputs.Contents),
llvm::move(getIncludeCleanerDiags(Result, Inputs.Contents, *Inputs.TFS),
std::back_inserter(Result.Diags));
return std::move(Result);
}
Expand Down
14 changes: 7 additions & 7 deletions clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ TEST(IncludeCleaner, GenerateMissingHeaderDiags) {
auto Findings = computeIncludeCleanerFindings(AST);
Findings.UnusedIncludes.clear();
std::vector<clangd::Diag> Diags = issueIncludeCleanerDiagnostics(
AST, TU.Code, Findings,
AST, TU.Code, Findings, MockFS(),
{[](llvm::StringRef Header) { return Header.ends_with("buzz.h"); }});
EXPECT_THAT(
Diags,
Expand Down Expand Up @@ -505,8 +505,8 @@ TEST(IncludeCleaner, BatchFix) {
)cpp";
auto AST = TU.build();
EXPECT_THAT(
issueIncludeCleanerDiagnostics(AST, TU.Code,
computeIncludeCleanerFindings(AST)),
issueIncludeCleanerDiagnostics(
AST, TU.Code, computeIncludeCleanerFindings(AST), MockFS()),
UnorderedElementsAre(withFix({FixMessage("#include \"foo.h\""),
FixMessage("fix all includes")}),
withFix({FixMessage("remove #include directive"),
Expand All @@ -520,8 +520,8 @@ TEST(IncludeCleaner, BatchFix) {
)cpp";
AST = TU.build();
EXPECT_THAT(
issueIncludeCleanerDiagnostics(AST, TU.Code,
computeIncludeCleanerFindings(AST)),
issueIncludeCleanerDiagnostics(
AST, TU.Code, computeIncludeCleanerFindings(AST), MockFS()),
UnorderedElementsAre(withFix({FixMessage("#include \"foo.h\""),
FixMessage("fix all includes")}),
withFix({FixMessage("remove #include directive"),
Expand All @@ -539,8 +539,8 @@ TEST(IncludeCleaner, BatchFix) {
)cpp";
AST = TU.build();
EXPECT_THAT(
issueIncludeCleanerDiagnostics(AST, TU.Code,
computeIncludeCleanerFindings(AST)),
issueIncludeCleanerDiagnostics(
AST, TU.Code, computeIncludeCleanerFindings(AST), MockFS()),
UnorderedElementsAre(withFix({FixMessage("#include \"foo.h\""),
FixMessage("add all missing includes"),
FixMessage("fix all includes")}),
Expand Down

0 comments on commit 5549b01

Please sign in to comment.