[clang] [libclang] Compute the right spelling location (PR #72400)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 19 09:51:10 PDT 2024


================
@@ -1292,6 +1292,31 @@ 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);
+
+  Traverse([](CXCursor cursor, CXCursor parent) -> CXChildVisitResult {
+    if (cursor.kind == CXCursor_VarDecl) {
+      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;
+  });
+}
----------------
jansvoboda11 wrote:

I think we should adopt the pattern used in other unit tests in this file where we initialize a counter to 0 before calling `Traverse()` and after it returns check we actually visited the expected number of nodes. Otherwise this test might pass trivially without performing any checks if the inner block is not executed.

https://github.com/llvm/llvm-project/pull/72400


More information about the cfe-commits mailing list