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

2023-11-15 Thread Sebastian Poeplau via cfe-commits

https://github.com/sebastianpoeplau created 
https://github.com/llvm/llvm-project/pull/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.

>From 88b94c936257100e5400ad37273bb9d509ecfe3c Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Mon, 11 Apr 2022 16:53:24 +0200
Subject: [PATCH] [libclang] Compute the right spelling location

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.
---
 clang/tools/c-index-test/c-index-test.c   | 55 +++
 clang/tools/libclang/CXSourceLocation.cpp |  3 +-
 2 files changed, 28 insertions(+), 30 deletions(-)

diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 9d66a22f3b43b55..e73c73ce59e0f72 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -451,10 +451,10 @@ static void PrintRange(CXSourceRange R, const char *str) {
   CXFile begin_file, end_file;
   unsigned begin_line, begin_column, end_line, end_column;
 
-  clang_getSpellingLocation(clang_getRangeStart(R),
-&begin_file, &begin_line, &begin_column, 0);
-  clang_getSpellingLocation(clang_getRangeEnd(R),
-&end_file, &end_line, &end_column, 0);
+  clang_getFileLocation(clang_getRangeStart(R),
+&begin_file, &begin_line, &begin_column, 0);
+  clang_getFileLocation(clang_getRangeEnd(R),
+&end_file, &end_line, &end_column, 0);
   if (!begin_file || !end_file)
 return;
 
@@ -836,13 +836,13 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 printf(", ");
   
   Loc = clang_getCursorLocation(Ovl);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf("%d:%d", line, column);  
 }
 printf("]");
   } else {
 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf(":%d:%d", line, column);
   }
 
@@ -1034,7 +1034,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
   CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
   CXString Name = clang_getCursorSpelling(SpecializationOf);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf(" [Specialization of %s:%d:%d]",
  clang_getCString(Name), line, column);
   clang_disposeString(Name);
@@ -1081,7 +1081,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
   printf(" [Overrides ");
   for (I = 0; I != num_overridden; ++I) {
 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 lineCols[I].line = line;
 lineCols[I].col = column;
   }
@@ -1244,8 +1244,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
   fprintf(stderr, "%s\n", clang_getCString(Msg));
   clang_disposeString(Msg);
 
-  clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
-&file, 0, 0, 0);
+  clang_getFileLocation(clang_getDiagnosticLocation(Diagnostic),
+&file, 0, 0, 0);
   if (!file)
 return;
 
@@ -1258,9 +1258,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
 CXSourceLocation end = clang_getRangeEnd(range);
 unsigned start_line, start_column, end_line, end_column;
 CXFile start_file, end_file;
-clang_getSpellingLocation(start, &start_file, &start_line,
-  &start_column, 0);
-clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
+clang_getFileLocation(start, &start_file, &start_line, &start_column, 0);
+clang_getFileLocation(end, &end_file, &end_line, &end_column, 0);
 if (clang_equalLocations(start, end)) {
   /* Insertion. */
   if (start_file == file)
@@ -1343,7 +1342,7 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor 
Cursor,
   if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
 unsigned line, colu

[clang] [clang] Add missing LinkageSpec case to getCursorKindForDecl (PR #72401)

2023-11-15 Thread Sebastian Poeplau via cfe-commits

https://github.com/sebastianpoeplau created 
https://github.com/llvm/llvm-project/pull/72401

The LinkageSpec case was omitted, and there is a declared CXCursor_Kind for it. 
Adapt the testsuite drivers to print mangled names for declarations with extern 
linkage. Also update the test baseline for the recursive-cxx-member-calls.cpp 
test.

>From 660aade6c8f1aea4b86da2dc3bee98e8b44f2ae2 Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Wed, 17 Aug 2022 19:22:53 +0200
Subject: [PATCH] [clang] Add missing LinkageSpec case to getCursorKindForDecl

The LinkageSpec case was omitted, and there is a declared CXCursor_Kind
for it. Adapt the testsuite drivers to print mangled names for
declarations with extern linkage. Also update the test baseline for the
recursive-cxx-member-calls.cpp test.
---
 clang/lib/Sema/SemaCodeComplete.cpp |  3 +++
 clang/test/Index/recursive-cxx-member-calls.cpp | 10 +-
 clang/tools/c-index-test/c-index-test.c |  4 
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Sema/SemaCodeComplete.cpp 
b/clang/lib/Sema/SemaCodeComplete.cpp
index 3355336d8c2c816..6169144ef1c2d48 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -4165,6 +4165,9 @@ CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
   case Decl::Concept:
 return CXCursor_ConceptDecl;
 
+  case Decl::LinkageSpec:
+return CXCursor_LinkageSpec;
+
   default:
 if (const auto *TD = dyn_cast(D)) {
   switch (TD->getTagKind()) {
diff --git a/clang/test/Index/recursive-cxx-member-calls.cpp 
b/clang/test/Index/recursive-cxx-member-calls.cpp
index 09f3f414194c565..be908c506e74769 100644
--- a/clang/test/Index/recursive-cxx-member-calls.cpp
+++ b/clang/test/Index/recursive-cxx-member-calls.cpp
@@ -216,9 +216,9 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK-tokens: Punctuation: "}" [4:63 - 4:64] ClassTemplate=pair:4:44 
(Definition)
 // CHECK-tokens: Punctuation: ";" [4:64 - 4:65] Namespace=std:3:11 (Definition)
 // CHECK-tokens: Punctuation: "}" [5:1 - 5:2] Namespace=std:3:11 (Definition)
-// CHECK-tokens: Keyword: "extern" [6:1 - 6:7]
-// CHECK-tokens: Literal: ""C"" [6:8 - 6:11] UnexposedDecl=:6:8 (Definition)
-// CHECK-tokens: Punctuation: "{" [6:12 - 6:13] UnexposedDecl=:6:8 (Definition)
+// CHECK-tokens: Keyword: "extern" [6:1 - 6:7] LinkageSpec=:6:8 (Definition)
+// CHECK-tokens: Literal: ""C"" [6:8 - 6:11] LinkageSpec=:6:8 (Definition)
+// CHECK-tokens: Punctuation: "{" [6:12 - 6:13] LinkageSpec=:6:8 (Definition)
 // CHECK-tokens: Keyword: "int" [7:3 - 7:6] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Identifier: "memcmp" [7:7 - 7:13] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Punctuation: "(" [7:13 - 7:14] FunctionDecl=memcmp:7:7
@@ -232,7 +232,7 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK-tokens: Punctuation: "," [7:40 - 7:41] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Identifier: "size_t" [7:42 - 7:48] TypeRef=size_t:2:25
 // CHECK-tokens: Punctuation: ")" [7:48 - 7:49] FunctionDecl=memcmp:7:7
-// CHECK-tokens: Punctuation: ";" [7:49 - 7:50] UnexposedDecl=:6:8 (Definition)
+// CHECK-tokens: Punctuation: ";" [7:49 - 7:50] LinkageSpec=:6:8 (Definition)
 // CHECK-tokens: Identifier: "size_t" [8:3 - 8:9] TypeRef=size_t:2:25
 // CHECK-tokens: Identifier: "strlen" [8:10 - 8:16] FunctionDecl=strlen:8:10
 // CHECK-tokens: Punctuation: "(" [8:16 - 8:17] FunctionDecl=strlen:8:10
@@ -1532,7 +1532,7 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK: 4:20: TemplateTypeParameter=_T1:4:20 (Definition) Extent=[4:14 - 
4:23]
 // CHECK: 4:31: TemplateTypeParameter=_T2:4:31 (Definition) Extent=[4:25 - 
4:34]
 // CHECK: 4:55: FieldDecl=second:4:55 (Definition) Extent=[4:51 - 4:61]
-// CHECK: 6:8: UnexposedDecl=:6:8 (Definition) Extent=[6:1 - 9:2]
+// CHECK: 6:8: LinkageSpec=:6:8 (Definition) Extent=[6:1 - 9:2]
 // CHECK: 7:7: FunctionDecl=memcmp:7:7 Extent=[7:3 - 7:49]
 // CHECK: 7:26: ParmDecl=:7:26 (Definition) Extent=[7:14 - 7:26]
 // CHECK: 7:40: ParmDecl=:7:40 (Definition) Extent=[7:28 - 7:40]
diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 9d66a22f3b43b55..2c0c9cb8eb5e42f 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -1838,6 +1838,8 @@ static enum CXChildVisitResult PrintMangledName(CXCursor 
cursor, CXCursor p,
   CXString MangledName;
   if (clang_isUnexposed(clang_getCursorKind(cursor)))
 return CXChildVisit_Recurse;
+  if (clang_getCursorKind(cursor) == CXCursor_LinkageSpec)
+return CXChildVisit_Recurse;
   PrintCursor(cursor, NULL);
   MangledName = clang_Cursor_getMangling(cursor);
   printf(" [mangled=%s]\n", clang_getCString(MangledName));
@@ -1853,6 +1855,8 @@ static enum CXChildVisitResult PrintManglings(CXCursor 
cursor, CXCursor p,
 return CXChildVisit_Recurse;
   if (!clang_i

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

2023-11-15 Thread Sebastian Poeplau via cfe-commits

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

>From db1f8becea210494ae42619cba3ad697cddc07bf Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Mon, 11 Apr 2022 16:53:24 +0200
Subject: [PATCH] [libclang] Compute the right spelling location

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.
---
 clang/tools/c-index-test/c-index-test.c   | 54 +++
 clang/tools/libclang/CXSourceLocation.cpp |  3 +-
 2 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 9d66a22f3b43b55..ac1a11315230025 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -451,10 +451,10 @@ static void PrintRange(CXSourceRange R, const char *str) {
   CXFile begin_file, end_file;
   unsigned begin_line, begin_column, end_line, end_column;
 
-  clang_getSpellingLocation(clang_getRangeStart(R),
-&begin_file, &begin_line, &begin_column, 0);
-  clang_getSpellingLocation(clang_getRangeEnd(R),
-&end_file, &end_line, &end_column, 0);
+  clang_getFileLocation(clang_getRangeStart(R), &begin_file, &begin_line,
+&begin_column, 0);
+  clang_getFileLocation(clang_getRangeEnd(R), &end_file, &end_line, 
&end_column,
+0);
   if (!begin_file || !end_file)
 return;
 
@@ -836,13 +836,13 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 printf(", ");
   
   Loc = clang_getCursorLocation(Ovl);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf("%d:%d", line, column);  
 }
 printf("]");
   } else {
 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf(":%d:%d", line, column);
   }
 
@@ -1034,7 +1034,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
   CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
   CXString Name = clang_getCursorSpelling(SpecializationOf);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf(" [Specialization of %s:%d:%d]",
  clang_getCString(Name), line, column);
   clang_disposeString(Name);
@@ -1081,7 +1081,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
   printf(" [Overrides ");
   for (I = 0; I != num_overridden; ++I) {
 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 lineCols[I].line = line;
 lineCols[I].col = column;
   }
@@ -1244,8 +1244,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
   fprintf(stderr, "%s\n", clang_getCString(Msg));
   clang_disposeString(Msg);
 
-  clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
-&file, 0, 0, 0);
+  clang_getFileLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0,
+0);
   if (!file)
 return;
 
@@ -1258,9 +1258,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
 CXSourceLocation end = clang_getRangeEnd(range);
 unsigned start_line, start_column, end_line, end_column;
 CXFile start_file, end_file;
-clang_getSpellingLocation(start, &start_file, &start_line,
-  &start_column, 0);
-clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
+clang_getFileLocation(start, &start_file, &start_line, &start_column, 0);
+clang_getFileLocation(end, &end_file, &end_line, &end_column, 0);
 if (clang_equalLocations(start, end)) {
   /* Insertion. */
   if (start_file == file)
@@ -1343,7 +1342,7 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor 
Cursor,
   if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
 unsigned line, column;
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
GetCursorSource(Cursor), line, column);
 PrintCursor(Cursor, Data->CommentSchemaFile);
@@ -1404,7 +1403,7 @@ static enum CXCh

[clang] [clang] Add missing LinkageSpec case to getCursorKindForDecl (PR #72401)

2023-11-23 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

It should, yes.

https://github.com/llvm/llvm-project/pull/72401
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add missing LinkageSpec case to getCursorKindForDecl (PR #72401)

2023-11-24 Thread Sebastian Poeplau via cfe-commits

https://github.com/sebastianpoeplau updated 
https://github.com/llvm/llvm-project/pull/72401

>From df8bfc2cdb1488d3ad037ea26bad48e658e5d3c8 Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Wed, 17 Aug 2022 19:22:53 +0200
Subject: [PATCH] [clang] Add missing LinkageSpec case to getCursorKindForDecl

The LinkageSpec case was omitted, and there is a declared CXCursor_Kind
for it. Adapt the testsuite drivers to print mangled names for
declarations with extern linkage. Also update the test baseline for the
recursive-cxx-member-calls.cpp test.
---
 clang/docs/ReleaseNotes.rst |  2 ++
 clang/lib/Sema/SemaCodeComplete.cpp |  3 +++
 clang/test/Index/recursive-cxx-member-calls.cpp | 10 +-
 clang/tools/c-index-test/c-index-test.c |  4 
 4 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ed1a978b5382d71..d321ec694c59914 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -861,6 +861,8 @@ libclang
 
 
 - Exposed arguments of ``clang::annotate``.
+- ``clang::getCursorKindForDecl`` now recognizes linkage specifications such as
+  ``extern "C"`` and reports them as ``CXCursor_LinkageSpec``.
 
 Static Analyzer
 ---
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp 
b/clang/lib/Sema/SemaCodeComplete.cpp
index 3355336d8c2c816..6169144ef1c2d48 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -4165,6 +4165,9 @@ CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
   case Decl::Concept:
 return CXCursor_ConceptDecl;
 
+  case Decl::LinkageSpec:
+return CXCursor_LinkageSpec;
+
   default:
 if (const auto *TD = dyn_cast(D)) {
   switch (TD->getTagKind()) {
diff --git a/clang/test/Index/recursive-cxx-member-calls.cpp 
b/clang/test/Index/recursive-cxx-member-calls.cpp
index 09f3f414194c565..be908c506e74769 100644
--- a/clang/test/Index/recursive-cxx-member-calls.cpp
+++ b/clang/test/Index/recursive-cxx-member-calls.cpp
@@ -216,9 +216,9 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK-tokens: Punctuation: "}" [4:63 - 4:64] ClassTemplate=pair:4:44 
(Definition)
 // CHECK-tokens: Punctuation: ";" [4:64 - 4:65] Namespace=std:3:11 (Definition)
 // CHECK-tokens: Punctuation: "}" [5:1 - 5:2] Namespace=std:3:11 (Definition)
-// CHECK-tokens: Keyword: "extern" [6:1 - 6:7]
-// CHECK-tokens: Literal: ""C"" [6:8 - 6:11] UnexposedDecl=:6:8 (Definition)
-// CHECK-tokens: Punctuation: "{" [6:12 - 6:13] UnexposedDecl=:6:8 (Definition)
+// CHECK-tokens: Keyword: "extern" [6:1 - 6:7] LinkageSpec=:6:8 (Definition)
+// CHECK-tokens: Literal: ""C"" [6:8 - 6:11] LinkageSpec=:6:8 (Definition)
+// CHECK-tokens: Punctuation: "{" [6:12 - 6:13] LinkageSpec=:6:8 (Definition)
 // CHECK-tokens: Keyword: "int" [7:3 - 7:6] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Identifier: "memcmp" [7:7 - 7:13] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Punctuation: "(" [7:13 - 7:14] FunctionDecl=memcmp:7:7
@@ -232,7 +232,7 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK-tokens: Punctuation: "," [7:40 - 7:41] FunctionDecl=memcmp:7:7
 // CHECK-tokens: Identifier: "size_t" [7:42 - 7:48] TypeRef=size_t:2:25
 // CHECK-tokens: Punctuation: ")" [7:48 - 7:49] FunctionDecl=memcmp:7:7
-// CHECK-tokens: Punctuation: ";" [7:49 - 7:50] UnexposedDecl=:6:8 (Definition)
+// CHECK-tokens: Punctuation: ";" [7:49 - 7:50] LinkageSpec=:6:8 (Definition)
 // CHECK-tokens: Identifier: "size_t" [8:3 - 8:9] TypeRef=size_t:2:25
 // CHECK-tokens: Identifier: "strlen" [8:10 - 8:16] FunctionDecl=strlen:8:10
 // CHECK-tokens: Punctuation: "(" [8:16 - 8:17] FunctionDecl=strlen:8:10
@@ -1532,7 +1532,7 @@ AttributeList::Kind AttributeList::getKind(const 
IdentifierInfo * Name) {
 // CHECK: 4:20: TemplateTypeParameter=_T1:4:20 (Definition) Extent=[4:14 - 
4:23]
 // CHECK: 4:31: TemplateTypeParameter=_T2:4:31 (Definition) Extent=[4:25 - 
4:34]
 // CHECK: 4:55: FieldDecl=second:4:55 (Definition) Extent=[4:51 - 4:61]
-// CHECK: 6:8: UnexposedDecl=:6:8 (Definition) Extent=[6:1 - 9:2]
+// CHECK: 6:8: LinkageSpec=:6:8 (Definition) Extent=[6:1 - 9:2]
 // CHECK: 7:7: FunctionDecl=memcmp:7:7 Extent=[7:3 - 7:49]
 // CHECK: 7:26: ParmDecl=:7:26 (Definition) Extent=[7:14 - 7:26]
 // CHECK: 7:40: ParmDecl=:7:40 (Definition) Extent=[7:28 - 7:40]
diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 9d66a22f3b43b55..2c0c9cb8eb5e42f 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -1838,6 +1838,8 @@ static enum CXChildVisitResult PrintMangledName(CXCursor 
cursor, CXCursor p,
   CXString MangledName;
   if (clang_isUnexposed(clang_getCursorKind(cursor)))
 return CXChildVisit_Recurse;
+  if (clang_getCursorKind(cursor) == CXCursor_LinkageSpec)
+return CXChildVisit_Recurse;
   PrintCursor(cursor, NULL)

[clang] [clang] Add missing LinkageSpec case to getCursorKindForDecl (PR #72401)

2023-11-24 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

I've added an entry to the release notes.

https://github.com/llvm/llvm-project/pull/72401
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Add missing LinkageSpec case to getCursorKindForDecl (PR #72401)

2023-11-24 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

By the way, I don't have permission to merge, so feel free to do it when you 
think it's ready.

https://github.com/llvm/llvm-project/pull/72401
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2023-11-28 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

I just discovered #28205 which should be fixed by this change.

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Rewrite] Fix offset computation in RemoveText (PR #73827)

2023-11-29 Thread Sebastian Poeplau via cfe-commits

https://github.com/sebastianpoeplau created 
https://github.com/llvm/llvm-project/pull/73827

When removing a source range from the RewriterBuffer, Clang needs to compute 
the size of the actual range in the RewriterBuffer (as new elements may have 
been inserted at any position of the original range). Once this is done, it 
then maps the original source location offset to real offsets into the 
RewriteBuffer, supposedly accounting for modifications of the RewriteBuffer 
(insertion/deletion of text). However, it actually does not account for 
modifications at the beginning of the range, which is inconsistent behavior as 
they are taken into account when computing the size of the range. This commit 
fixes this slightly-off behavior.

>From dd764d6773529fb79e0a2a881c34b49cb6ddda24 Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Wed, 16 Aug 2023 13:34:10 +
Subject: [PATCH] [Rewrite] Fix offset computation in RemoveText

When removing a source range from the RewriterBuffer, Clang needs to
compute the size of the actual range in the RewriterBuffer (as new
elements may have been inserted at any position of the original range).
Once this is done, it then maps the original source location offset to
real offsets into the RewriteBuffer, supposedly accounting for
modifications of the RewriteBuffer (insertion/deletion of text).
However, it actually does not account for modifications at the beginning
of the range, which is inconsistent behavior as they are taken into
account when computing the size of the range. This commit fixes this
slightly-off behavior.
---
 .../clang/Rewrite/Core/RewriteBuffer.h|  2 +-
 clang/include/clang/Rewrite/Core/Rewriter.h   |  9 --
 clang/lib/Rewrite/Rewriter.cpp| 28 +++
 clang/unittests/Rewrite/RewriterTest.cpp  | 25 +
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Rewrite/Core/RewriteBuffer.h 
b/clang/include/clang/Rewrite/Core/RewriteBuffer.h
index b8f34174b715664..295910aa715fc59 100644
--- a/clang/include/clang/Rewrite/Core/RewriteBuffer.h
+++ b/clang/include/clang/Rewrite/Core/RewriteBuffer.h
@@ -58,7 +58,7 @@ class RewriteBuffer {
 
   /// RemoveText - Remove the specified text.
   void RemoveText(unsigned OrigOffset, unsigned Size,
-  bool removeLineIfEmpty = false);
+  bool removeLineIfEmpty = false, bool AfterInserts = true);
 
   /// InsertText - Insert some text at the specified point, where the offset in
   /// the buffer is specified relative to the original SourceBuffer.  The
diff --git a/clang/include/clang/Rewrite/Core/Rewriter.h 
b/clang/include/clang/Rewrite/Core/Rewriter.h
index c89015e4055820c..4cf846d777bd4db 100644
--- a/clang/include/clang/Rewrite/Core/Rewriter.h
+++ b/clang/include/clang/Rewrite/Core/Rewriter.h
@@ -139,17 +139,20 @@ class Rewriter {
 
   /// RemoveText - Remove the specified text region.
   bool RemoveText(SourceLocation Start, unsigned Length,
-  RewriteOptions opts = RewriteOptions());
+  RewriteOptions opts = RewriteOptions(),
+  bool AfterInserts = true);
 
   /// Remove the specified text region.
   bool RemoveText(CharSourceRange range,
   RewriteOptions opts = RewriteOptions()) {
-return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
+return RemoveText(range.getBegin(), getRangeSize(range, opts), opts,
+  !opts.IncludeInsertsAtBeginOfRange);
   }
 
   /// Remove the specified text region.
   bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
-return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
+return RemoveText(range.getBegin(), getRangeSize(range, opts), opts,
+  !opts.IncludeInsertsAtBeginOfRange);
   }
 
   /// ReplaceText - This method replaces a range of characters in the input
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index ef2858990dd954e..901e11e8b439e92 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -55,19 +55,34 @@ static inline bool isWhitespaceExceptNL(unsigned char c) {
 }
 
 void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
-   bool removeLineIfEmpty) {
+   bool removeLineIfEmpty, bool AfterInserts) {
   // Nothing to remove, exit early.
   if (Size == 0) return;
 
-  unsigned RealOffset = getMappedOffset(OrigOffset, true);
+  unsigned RealOffset = getMappedOffset(OrigOffset, AfterInserts);
   assert(RealOffset+Size <= Buffer.size() && "Invalid location");
 
   // Remove the dead characters.
   Buffer.erase(RealOffset, Size);
 
   // Add a delta so that future changes are offset correctly.
-  AddReplaceDelta(OrigOffset, -Size);
-
+  if (!AfterInserts) {
+// In this case, we must adjust two deltas:
+//   * The one that corresponds to text inserted at the Rea

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

2024-01-19 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

Ping

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Rewrite] Fix offset computation in RemoveText (PR #73827)

2024-01-19 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

Ping

https://github.com/llvm/llvm-project/pull/73827
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-04-18 Thread Sebastian Poeplau via cfe-commits

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

>From f260093f23fe599f2b91bbe0bf06304387549a22 Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Mon, 11 Apr 2022 16:53:24 +0200
Subject: [PATCH] [libclang] Compute the right spelling location

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.
---
 clang/tools/c-index-test/c-index-test.c   | 54 +++
 clang/tools/libclang/CXSourceLocation.cpp |  3 +-
 2 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 21619888cfa5f3..e078e9bdce027a 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -464,10 +464,10 @@ static void PrintRange(CXSourceRange R, const char *str) {
   CXFile begin_file, end_file;
   unsigned begin_line, begin_column, end_line, end_column;
 
-  clang_getSpellingLocation(clang_getRangeStart(R),
-&begin_file, &begin_line, &begin_column, 0);
-  clang_getSpellingLocation(clang_getRangeEnd(R),
-&end_file, &end_line, &end_column, 0);
+  clang_getFileLocation(clang_getRangeStart(R), &begin_file, &begin_line,
+&begin_column, 0);
+  clang_getFileLocation(clang_getRangeEnd(R), &end_file, &end_line, 
&end_column,
+0);
   if (!begin_file || !end_file)
 return;
 
@@ -849,13 +849,13 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 printf(", ");
   
   Loc = clang_getCursorLocation(Ovl);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf("%d:%d", line, column);  
 }
 printf("]");
   } else {
 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf(":%d:%d", line, column);
   }
 
@@ -1047,7 +1047,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
   CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
   CXString Name = clang_getCursorSpelling(SpecializationOf);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf(" [Specialization of %s:%d:%d]",
  clang_getCString(Name), line, column);
   clang_disposeString(Name);
@@ -1094,7 +1094,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
   printf(" [Overrides ");
   for (I = 0; I != num_overridden; ++I) {
 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 lineCols[I].line = line;
 lineCols[I].col = column;
   }
@@ -1257,8 +1257,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
   fprintf(stderr, "%s\n", clang_getCString(Msg));
   clang_disposeString(Msg);
 
-  clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
-&file, 0, 0, 0);
+  clang_getFileLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0,
+0);
   if (!file)
 return;
 
@@ -1271,9 +1271,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
 CXSourceLocation end = clang_getRangeEnd(range);
 unsigned start_line, start_column, end_line, end_column;
 CXFile start_file, end_file;
-clang_getSpellingLocation(start, &start_file, &start_line,
-  &start_column, 0);
-clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
+clang_getFileLocation(start, &start_file, &start_line, &start_column, 0);
+clang_getFileLocation(end, &end_file, &end_line, &end_column, 0);
 if (clang_equalLocations(start, end)) {
   /* Insertion. */
   if (start_file == file)
@@ -1356,7 +1355,7 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor 
Cursor,
   if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
 unsigned line, column;
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
GetCursorSource(Cursor), line, column);
 PrintCursor(Cursor, Data->CommentSchemaFile);
@@ -1417,7 +1416,7 @@ static enum CXChil

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

2024-04-18 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

@jansvoboda11 since you've most recently touched this part of the code, would 
you mind having a look at the change?

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Rewrite] Fix offset computation in RemoveText (PR #73827)

2024-04-18 Thread Sebastian Poeplau via cfe-commits

https://github.com/sebastianpoeplau updated 
https://github.com/llvm/llvm-project/pull/73827

>From f31443bb4de3cec4e7d98ac3c147f09a7ca297fe Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Wed, 16 Aug 2023 13:34:10 +
Subject: [PATCH] [Rewrite] Fix offset computation in RemoveText

When removing a source range from the RewriterBuffer, Clang needs to
compute the size of the actual range in the RewriterBuffer (as new
elements may have been inserted at any position of the original range).
Once this is done, it then maps the original source location offset to
real offsets into the RewriteBuffer, supposedly accounting for
modifications of the RewriteBuffer (insertion/deletion of text).
However, it actually does not account for modifications at the beginning
of the range, which is inconsistent behavior as they are taken into
account when computing the size of the range. This commit fixes this
slightly-off behavior.
---
 .../clang/Rewrite/Core/RewriteBuffer.h|  2 +-
 clang/include/clang/Rewrite/Core/Rewriter.h   |  9 --
 clang/lib/Rewrite/Rewriter.cpp| 28 +++
 clang/unittests/Rewrite/RewriterTest.cpp  | 25 +
 4 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/clang/include/clang/Rewrite/Core/RewriteBuffer.h 
b/clang/include/clang/Rewrite/Core/RewriteBuffer.h
index b8f34174b71566..295910aa715fc5 100644
--- a/clang/include/clang/Rewrite/Core/RewriteBuffer.h
+++ b/clang/include/clang/Rewrite/Core/RewriteBuffer.h
@@ -58,7 +58,7 @@ class RewriteBuffer {
 
   /// RemoveText - Remove the specified text.
   void RemoveText(unsigned OrigOffset, unsigned Size,
-  bool removeLineIfEmpty = false);
+  bool removeLineIfEmpty = false, bool AfterInserts = true);
 
   /// InsertText - Insert some text at the specified point, where the offset in
   /// the buffer is specified relative to the original SourceBuffer.  The
diff --git a/clang/include/clang/Rewrite/Core/Rewriter.h 
b/clang/include/clang/Rewrite/Core/Rewriter.h
index c89015e4055820..4cf846d777bd4d 100644
--- a/clang/include/clang/Rewrite/Core/Rewriter.h
+++ b/clang/include/clang/Rewrite/Core/Rewriter.h
@@ -139,17 +139,20 @@ class Rewriter {
 
   /// RemoveText - Remove the specified text region.
   bool RemoveText(SourceLocation Start, unsigned Length,
-  RewriteOptions opts = RewriteOptions());
+  RewriteOptions opts = RewriteOptions(),
+  bool AfterInserts = true);
 
   /// Remove the specified text region.
   bool RemoveText(CharSourceRange range,
   RewriteOptions opts = RewriteOptions()) {
-return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
+return RemoveText(range.getBegin(), getRangeSize(range, opts), opts,
+  !opts.IncludeInsertsAtBeginOfRange);
   }
 
   /// Remove the specified text region.
   bool RemoveText(SourceRange range, RewriteOptions opts = RewriteOptions()) {
-return RemoveText(range.getBegin(), getRangeSize(range, opts), opts);
+return RemoveText(range.getBegin(), getRangeSize(range, opts), opts,
+  !opts.IncludeInsertsAtBeginOfRange);
   }
 
   /// ReplaceText - This method replaces a range of characters in the input
diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp
index 0e6ae365064463..9f68cf20f49107 100644
--- a/clang/lib/Rewrite/Rewriter.cpp
+++ b/clang/lib/Rewrite/Rewriter.cpp
@@ -55,19 +55,34 @@ static inline bool isWhitespaceExceptNL(unsigned char c) {
 }
 
 void RewriteBuffer::RemoveText(unsigned OrigOffset, unsigned Size,
-   bool removeLineIfEmpty) {
+   bool removeLineIfEmpty, bool AfterInserts) {
   // Nothing to remove, exit early.
   if (Size == 0) return;
 
-  unsigned RealOffset = getMappedOffset(OrigOffset, true);
+  unsigned RealOffset = getMappedOffset(OrigOffset, AfterInserts);
   assert(RealOffset+Size <= Buffer.size() && "Invalid location");
 
   // Remove the dead characters.
   Buffer.erase(RealOffset, Size);
 
   // Add a delta so that future changes are offset correctly.
-  AddReplaceDelta(OrigOffset, -Size);
-
+  if (!AfterInserts) {
+// In this case, we must adjust two deltas:
+//   * The one that corresponds to text inserted at the RealOffset
+// location. We must thus compute the size of the text inserted there.
+//
+//   * The one that corresponds to the rest of the range, excluding the
+// text inserted at the RealOffset location.
+unsigned OffsetBeforeInserts = RealOffset;
+unsigned OffsetAfterInserts = getMappedOffset(OrigOffset, true);
+unsigned InsertedTextSize = OffsetAfterInserts - OffsetBeforeInserts;
+if (InsertedTextSize != 0)
+  AddInsertDelta(OrigOffset, -InsertedTextSize);
+if (InsertedTextSize != Size)
+  AddReplaceDelta(OrigOffset, -(Size - InsertedTextSize));
+  } else {
+AddReplaceDelta(OrigOffset, -Size

[clang] [Rewrite] Fix offset computation in RemoveText (PR #73827)

2024-04-18 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

@jansvoboda11 this is another PR where I would appreciate your feedback if you 
can find the time.

https://github.com/llvm/llvm-project/pull/73827
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-04-19 Thread Sebastian Poeplau via cfe-commits

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

>From dd0f87b25733b4569b89ce445630ee843e3bfb2b Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Mon, 11 Apr 2022 16:53:24 +0200
Subject: [PATCH] [libclang] Compute the right spelling location

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.
---
 clang/docs/ReleaseNotes.rst   |  3 ++
 clang/tools/c-index-test/c-index-test.c   | 54 +++
 clang/tools/libclang/CXSourceLocation.cpp |  3 +-
 clang/unittests/libclang/LibclangTest.cpp | 25 +++
 4 files changed, 55 insertions(+), 30 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3fe15934323c53..6fdc285da6ecd3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -678,6 +678,9 @@ clang-format
 libclang
 
 
+- ``clang_getSpellingLocation`` now correctly resolves macro expansions; that
+  is, it returns the spelling location instead of the expansion location.
+
 Static Analyzer
 ---
 
diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 21619888cfa5f3..e078e9bdce027a 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -464,10 +464,10 @@ static void PrintRange(CXSourceRange R, const char *str) {
   CXFile begin_file, end_file;
   unsigned begin_line, begin_column, end_line, end_column;
 
-  clang_getSpellingLocation(clang_getRangeStart(R),
-&begin_file, &begin_line, &begin_column, 0);
-  clang_getSpellingLocation(clang_getRangeEnd(R),
-&end_file, &end_line, &end_column, 0);
+  clang_getFileLocation(clang_getRangeStart(R), &begin_file, &begin_line,
+&begin_column, 0);
+  clang_getFileLocation(clang_getRangeEnd(R), &end_file, &end_line, 
&end_column,
+0);
   if (!begin_file || !end_file)
 return;
 
@@ -849,13 +849,13 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 printf(", ");
   
   Loc = clang_getCursorLocation(Ovl);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf("%d:%d", line, column);  
 }
 printf("]");
   } else {
 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf(":%d:%d", line, column);
   }
 
@@ -1047,7 +1047,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
   CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
   CXString Name = clang_getCursorSpelling(SpecializationOf);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf(" [Specialization of %s:%d:%d]",
  clang_getCString(Name), line, column);
   clang_disposeString(Name);
@@ -1094,7 +1094,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
   printf(" [Overrides ");
   for (I = 0; I != num_overridden; ++I) {
 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 lineCols[I].line = line;
 lineCols[I].col = column;
   }
@@ -1257,8 +1257,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
   fprintf(stderr, "%s\n", clang_getCString(Msg));
   clang_disposeString(Msg);
 
-  clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
-&file, 0, 0, 0);
+  clang_getFileLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0,
+0);
   if (!file)
 return;
 
@@ -1271,9 +1271,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
 CXSourceLocation end = clang_getRangeEnd(range);
 unsigned start_line, start_column, end_line, end_column;
 CXFile start_file, end_file;
-clang_getSpellingLocation(start, &start_file, &start_line,
-  &start_column, 0);
-clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
+clang_getFileLocation(start, &start_file, &start_line, &start_column, 0);
+clang_getFileLocation(end, &end_file, &end_line, &end_column, 0);
 if (clang_equalLocations(start, end)) {
   /* Insertion. */
   if (start_file == file)
@@ -1356,7 +1355,7 @@ 

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

2024-04-19 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

Thanks for the review @jansvoboda11; I've added an entry in the release notes 
and a unit test.

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-04-22 Thread Sebastian Poeplau via cfe-commits

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

>From cc15f9bb8b37fb1b68bd33241ab37e98d57d274c Mon Sep 17 00:00:00 2001
From: Matthieu Eyraud 
Date: Mon, 11 Apr 2022 16:53:24 +0200
Subject: [PATCH] [libclang] Compute the right spelling location

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.
---
 clang/docs/ReleaseNotes.rst   |  3 ++
 clang/tools/c-index-test/c-index-test.c   | 54 +++
 clang/tools/libclang/CXSourceLocation.cpp |  3 +-
 clang/unittests/libclang/LibclangTest.cpp | 31 +
 4 files changed, 61 insertions(+), 30 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3fe15934323c53..6fdc285da6ecd3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -678,6 +678,9 @@ clang-format
 libclang
 
 
+- ``clang_getSpellingLocation`` now correctly resolves macro expansions; that
+  is, it returns the spelling location instead of the expansion location.
+
 Static Analyzer
 ---
 
diff --git a/clang/tools/c-index-test/c-index-test.c 
b/clang/tools/c-index-test/c-index-test.c
index 21619888cfa5f3..e078e9bdce027a 100644
--- a/clang/tools/c-index-test/c-index-test.c
+++ b/clang/tools/c-index-test/c-index-test.c
@@ -464,10 +464,10 @@ static void PrintRange(CXSourceRange R, const char *str) {
   CXFile begin_file, end_file;
   unsigned begin_line, begin_column, end_line, end_column;
 
-  clang_getSpellingLocation(clang_getRangeStart(R),
-&begin_file, &begin_line, &begin_column, 0);
-  clang_getSpellingLocation(clang_getRangeEnd(R),
-&end_file, &end_line, &end_column, 0);
+  clang_getFileLocation(clang_getRangeStart(R), &begin_file, &begin_line,
+&begin_column, 0);
+  clang_getFileLocation(clang_getRangeEnd(R), &end_file, &end_line, 
&end_column,
+0);
   if (!begin_file || !end_file)
 return;
 
@@ -849,13 +849,13 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 printf(", ");
   
   Loc = clang_getCursorLocation(Ovl);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf("%d:%d", line, column);  
 }
 printf("]");
   } else {
 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 printf(":%d:%d", line, column);
   }
 
@@ -1047,7 +1047,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
   CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
   CXString Name = clang_getCursorSpelling(SpecializationOf);
-  clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+  clang_getFileLocation(Loc, 0, &line, &column, 0);
   printf(" [Specialization of %s:%d:%d]",
  clang_getCString(Name), line, column);
   clang_disposeString(Name);
@@ -1094,7 +1094,7 @@ static void PrintCursor(CXCursor Cursor, const char 
*CommentSchemaFile) {
   printf(" [Overrides ");
   for (I = 0; I != num_overridden; ++I) {
 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
-clang_getSpellingLocation(Loc, 0, &line, &column, 0);
+clang_getFileLocation(Loc, 0, &line, &column, 0);
 lineCols[I].line = line;
 lineCols[I].col = column;
   }
@@ -1257,8 +1257,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
   fprintf(stderr, "%s\n", clang_getCString(Msg));
   clang_disposeString(Msg);
 
-  clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
-&file, 0, 0, 0);
+  clang_getFileLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0,
+0);
   if (!file)
 return;
 
@@ -1271,9 +1271,8 @@ void PrintDiagnostic(CXDiagnostic Diagnostic) {
 CXSourceLocation end = clang_getRangeEnd(range);
 unsigned start_line, start_column, end_line, end_column;
 CXFile start_file, end_file;
-clang_getSpellingLocation(start, &start_file, &start_line,
-  &start_column, 0);
-clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
+clang_getFileLocation(start, &start_file, &start_line, &start_column, 0);
+clang_getFileLocation(end, &end_file, &end_line, &end_column, 0);
 if (clang_equalLocations(start, end)) {
   /* Insertion. */
   if (start_file == file)
@@ -1356,7 +1355,7 @

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

2024-04-22 Thread Sebastian Poeplau via cfe-commits


@@ -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;
+  });
+}

sebastianpoeplau wrote:

Good point, I've updated the test.

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-04-24 Thread Sebastian Poeplau via cfe-commits

sebastianpoeplau wrote:

Thanks for the approval @jansvoboda11! Would you mind merging the change? I 
don't have the required permissions...

https://github.com/llvm/llvm-project/pull/72400
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits