aboutsummaryrefslogtreecommitdiff
path: root/clang/unittests/libclang/LibclangTest.cpp
diff options
context:
space:
mode:
authorSebastian Poeplau <poeplau@adacore.com>2024-04-24 20:01:19 +0200
committerGitHub <noreply@github.com>2024-04-24 11:01:19 -0700
commit2e770edd8ce13f48402f1d93e5fb982d8a2ebe64 (patch)
tree2983db9d3101d8f692c57ad746b7b9ac8715ae41 /clang/unittests/libclang/LibclangTest.cpp
parentb8f3024a315074e0f880542c33cb89681eebc5a3 (diff)
downloadllvm-2e770edd8ce13f48402f1d93e5fb982d8a2ebe64.zip
llvm-2e770edd8ce13f48402f1d93e5fb982d8a2ebe64.tar.gz
llvm-2e770edd8ce13f48402f1d93e5fb982d8a2ebe64.tar.bz2
[libclang] Compute the right spelling location (#72400)
Locations inside macro expansions have different spelling/expansion locations. Apply a FIXME to make the libclang function clang_getSpellingLocation return the right spelling location, and adapt the testsuite driver code to use the file location rather than the spelling location to compute source ranges. Co-authored-by: Matthieu Eyraud <eyraud@adacore.com>
Diffstat (limited to 'clang/unittests/libclang/LibclangTest.cpp')
-rw-r--r--clang/unittests/libclang/LibclangTest.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/unittests/libclang/LibclangTest.cpp b/clang/unittests/libclang/LibclangTest.cpp
index 87075a4..6de4d02 100644
--- a/clang/unittests/libclang/LibclangTest.cpp
+++ b/clang/unittests/libclang/LibclangTest.cpp
@@ -1292,6 +1292,37 @@ void func() {}
EXPECT_EQ(attrCount, 1);
}
+TEST_F(LibclangParseTest, clang_getSpellingLocation) {
+ std::string fileName = "main.c";
+ WriteFile(fileName, "#define X(value) int x = value;\nX(42)\n");
+
+ ClangTU = clang_parseTranslationUnit(Index, fileName.c_str(), nullptr, 0,
+ nullptr, 0, TUFlags);
+
+ int declarationCount = 0;
+ Traverse([&declarationCount](CXCursor cursor,
+ CXCursor parent) -> CXChildVisitResult {
+ if (cursor.kind == CXCursor_VarDecl) {
+ declarationCount++;
+
+ CXSourceLocation cxl = clang_getCursorLocation(cursor);
+ unsigned line;
+
+ // We expect clang_getFileLocation to return the expansion location,
+ // whereas clang_getSpellingLocation should resolve the macro expansion
+ // and return the location of the macro definition.
+ clang_getFileLocation(cxl, nullptr, &line, nullptr, nullptr);
+ EXPECT_EQ(line, 2U);
+ clang_getSpellingLocation(cxl, nullptr, &line, nullptr, nullptr);
+ EXPECT_EQ(line, 1U);
+ }
+
+ return CXChildVisit_Recurse;
+ });
+
+ EXPECT_EQ(declarationCount, 1);
+}
+
class LibclangRewriteTest : public LibclangParseTest {
public:
CXRewriter Rew = nullptr;