[clang-tools-extra] f3a815a - [clangd] Map references from include'd files to directives
Kadir Cetinkaya via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 30 03:07:44 PDT 2023
Author: Kadir Cetinkaya
Date: 2023-03-30T12:06:22+02:00
New Revision: f3a815aa776f42c4f4b80fc8d19a69ff1b359906
URL: https://github.com/llvm/llvm-project/commit/f3a815aa776f42c4f4b80fc8d19a69ff1b359906
DIFF: https://github.com/llvm/llvm-project/commit/f3a815aa776f42c4f4b80fc8d19a69ff1b359906.diff
LOG: [clangd] Map references from include'd files to directives
Differential Revision: https://reviews.llvm.org/D147139
Added:
Modified:
clang-tools-extra/clangd/IncludeCleaner.cpp
clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/IncludeCleaner.cpp b/clang-tools-extra/clangd/IncludeCleaner.cpp
index fb245593b2f94..ee645efa6e6e1 100644
--- a/clang-tools-extra/clangd/IncludeCleaner.cpp
+++ b/clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -51,9 +51,11 @@
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
+#include <cassert>
#include <iterator>
#include <optional>
#include <string>
+#include <utility>
#include <vector>
namespace clang {
@@ -266,7 +268,6 @@ std::vector<Diag> generateUnusedIncludeDiagnostics(
}
} // namespace
-
std::vector<include_cleaner::SymbolReference>
collectMacroReferences(ParsedAST &AST) {
const auto &SM = AST.getSourceManager();
@@ -398,6 +399,10 @@ IncludeCleanerFindings computeIncludeCleanerFindings(ParsedAST &AST) {
// FIXME: Use presumed locations to map such usages back to patched
// locations safely.
auto Loc = SM.getFileLoc(Ref.RefLocation);
+ // File locations can be outside of the main file if macro is expanded
+ // through an #include.
+ while (SM.getFileID(Loc) != SM.getMainFileID())
+ Loc = SM.getIncludeLoc(SM.getFileID(Loc));
const auto *Token = AST.getTokens().spelledTokenAt(Loc);
MissingIncludeDiagInfo DiagInfo{Ref.Target, Token->range(SM),
Providers};
diff --git a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
index de45da02bfe87..c0c15cd0db8e5 100644
--- a/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/IncludeCleanerTests.cpp
@@ -384,6 +384,35 @@ TEST(IncludeCleaner, UmbrellaUsesPrivate) {
EXPECT_THAT(Findings.UnusedIncludes, IsEmpty());
}
+TEST(IncludeCleaner, MacroExpandedThroughIncludes) {
+ Annotations MainFile(R"cpp(
+ #include "all.h"
+ #define FOO(X) const Foo *X
+ void foo() {
+ #include [["expander.inc"]]
+ }
+)cpp");
+
+ TestTU TU;
+ TU.AdditionalFiles["expander.inc"] = guard("FOO(f1);FOO(f2);");
+ TU.AdditionalFiles["foo.h"] = guard("struct Foo {};");
+ TU.AdditionalFiles["all.h"] = guard("#include \"foo.h\"");
+
+ TU.Code = MainFile.code();
+ ParsedAST AST = TU.build();
+
+ auto Findings = computeIncludeCleanerFindings(AST).MissingIncludes;
+ // FIXME: Deduplicate references resulting from expansion of the same macro in
+ // multiple places.
+ EXPECT_THAT(Findings, testing::SizeIs(2));
+ auto RefRange = Findings.front().SymRefRange;
+ auto &SM = AST.getSourceManager();
+ EXPECT_EQ(RefRange.file(), SM.getMainFileID());
+ // FIXME: Point at the spelling location, rather than the include.
+ EXPECT_EQ(halfOpenToRange(SM, RefRange.toCharRange(SM)), MainFile.range());
+ EXPECT_EQ(RefRange, Findings[1].SymRefRange);
+}
+
} // namespace
} // namespace clangd
} // namespace clang
More information about the cfe-commits
mailing list