Skip to content

Commit 6b464e4

Browse files
[clang][SourceManager] Use getFileLoc when computing getPresumedLoc (#166255)
Now the files location is used for macro expansions. This provides more accurate location when reporting compilation errors. Move from `getDecomposedExpansionLoc(Loc)` to `getDecomposedLoc(getFileLoc(Loc))` when computing Presumed location.
1 parent 515924f commit 6b464e4

File tree

8 files changed

+23
-24
lines changed

8 files changed

+23
-24
lines changed

clang/include/clang/Basic/SourceManager.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,8 +1464,9 @@ class SourceManager : public RefCountedBase<SourceManager> {
14641464
/// directives. This provides a view on the data that a user should see
14651465
/// in diagnostics, for example.
14661466
///
1467-
/// Note that a presumed location is always given as the expansion point of
1468-
/// an expansion location, not at the spelling location.
1467+
/// If \p Loc is a macro expansion location, the presumed location
1468+
/// computation uses the spelling location for macro arguments and the
1469+
/// expansion location for other macro expansions.
14691470
///
14701471
/// \returns The presumed location of the specified SourceLocation. If the
14711472
/// presumed location cannot be calculated (e.g., because \p Loc is invalid

clang/lib/Basic/SourceManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
14351435
if (Loc.isInvalid()) return PresumedLoc();
14361436

14371437
// Presumed locations are always for expansion points.
1438-
FileIDAndOffset LocInfo = getDecomposedExpansionLoc(Loc);
1438+
FileIDAndOffset LocInfo = getDecomposedLoc(getFileLoc(Loc));
14391439

14401440
bool Invalid = false;
14411441
const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);

clang/test/Analysis/plist-macros-with-expansion.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,14 @@ void commaInBracketsTest() {
405405
code
406406

407407
void commaInBracesTest() {
408-
PASTE_CODE({ // expected-warning{{Dereference of null pointer}}
408+
PASTE_CODE({
409409
// NOTE: If we were to add a new variable here after a comma, we'd get a
410410
// compilation error, so this test is mainly here to show that this was also
411411
// investigated.
412412
//
413413
// int *ptr = nullptr, a;
414414
int *ptr = nullptr;
415-
*ptr = 5;
415+
*ptr = 5; // expected-warning{{Dereference of null pointer}}
416416
})
417417
}
418418

@@ -425,14 +425,14 @@ void commaInBracesTest() {
425425
// CHECK-NEXT: <key>col</key><integer>3</integer>
426426
// CHECK-NEXT: <key>file</key><integer>0</integer>
427427
// CHECK-NEXT: </dict>
428-
// CHECK-NEXT: <key>name</key><string>PASTE_CODE({ // expected-
428+
// CHECK-NEXT: <key>name</key><string>PASTE_CODE({
429429
// CHECK-NEXT: // NOTE: If we were to add a new variable here after a comma, we&apos;d get a
430430
// CHECK-NEXT: // compilation error, so this test is mainly here to show that this was also
431431
// CHECK-NEXT: // investigated.
432432
// CHECK-NEXT: //
433433
// CHECK-NEXT: // int *ptr = nullptr, a;
434434
// CHECK-NEXT: int *ptr = nullptr;
435-
// CHECK-NEXT: *ptr = 5;
435+
// CHECK-NEXT: *ptr = 5; // expected-
436436
// CHECK-NEXT: })</string>
437437
// CHECK-NEXT: <key>expansion</key><string>{int *ptr =nullptr ;*ptr =5;}</string>
438438
// CHECK-NEXT: </dict>

clang/test/C/C23/n2350.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,10 @@ int struct_in_second_param(void) {
4747

4848
int macro(void) {
4949
return offsetof(struct A // cpp-error {{'A' cannot be defined in a type specifier}} \
50-
expected-warning 2 {{defining a type within 'offsetof' is a C23 extension}}
50+
expected-warning {{defining a type within 'offsetof' is a C23 extension}}
5151
{
5252
int a;
53-
struct B // verifier seems to think the error is emitted by the macro
54-
// In fact the location of the error is "B" on the line above
53+
struct B // expected-warning {{defining a type within 'offsetof' is a C23 extension}}
5554
{
5655
int c;
5756
int d;

clang/test/ExtractAPI/macro_undefined.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ FUNC_GEN(bar, const int *, unsigned);
8989
},
9090
"location": {
9191
"position": {
92-
"character": 0,
92+
"character": 9,
9393
"line": 2
9494
},
9595
"uri": "file://INPUT_DIR/input.h"
@@ -241,7 +241,7 @@ FUNC_GEN(bar, const int *, unsigned);
241241
},
242242
"location": {
243243
"position": {
244-
"character": 0,
244+
"character": 9,
245245
"line": 3
246246
},
247247
"uri": "file://INPUT_DIR/input.h"

clang/test/FixIt/format.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ void a(N::E NEVal, S *SPtr, S &SRef) {
5656
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
5757
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
5858

59-
LOG( // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
59+
LOG(
6060
"%d",
61-
SPtr->Type
61+
SPtr->Type // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
6262
);
6363
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:7-[[@LINE-2]]:7}:"static_cast<int>("
6464
// CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:17-[[@LINE-3]]:17}:")"
@@ -68,8 +68,8 @@ void a(N::E NEVal, S *SPtr, S &SRef) {
6868
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
6969
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
7070

71-
LOG("%d", // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
72-
SRef.Type);
71+
LOG("%d",
72+
SRef.Type); // expected-warning{{format specifies type 'int' but the argument has type 'N::E'}}
7373
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:7}:"static_cast<int>("
7474
// CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:16-[[@LINE-2]]:16}:")"
7575

clang/test/Preprocessor/macro_arg_directive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void fail(const char *);
1818
({ int result = 0; __VA_ARGS__; if (!result) { fail(#__VA_ARGS__); }; result })
1919

2020
static inline int f(int k) {
21-
return MUNCH( // expected-error {{expected ')'}} expected-note {{to match this '('}} expected-error {{returning 'void'}} expected-note {{expansion of macro 'MUNCH' requested here}}
21+
return MUNCH( // expected-note {{to match this '('}} expected-error {{returning 'void'}} expected-note {{expansion of macro 'MUNCH' requested here}}
2222
if (k < 3)
2323
result = 24;
2424
else if (k > 4)
@@ -27,6 +27,6 @@ static inline int f(int k) {
2727

2828
#include "macro_arg_directive.h" // expected-error {{embedding a #include directive within macro arguments is not supported}}
2929

30-
int g(int k) {
30+
int g(int k) { // expected-error {{expected ')'}}
3131
return f(k) + f(k-1));
3232
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/* RUN: %clang_cc1 -E %s | grep 'a 3'
2-
* RUN: %clang_cc1 -E %s | grep 'b 16'
3-
* RUN: %clang_cc1 -E -P %s | grep 'a 3'
4-
* RUN: %clang_cc1 -E -P %s | grep 'b 16'
1+
/* RUN: %clang_cc1 -E %s | grep -z 'a.3'
2+
* RUN: %clang_cc1 -E %s | grep -z 'b.16'
3+
* RUN: %clang_cc1 -E -P %s | grep -z 'a.3'
4+
* RUN: %clang_cc1 -E -P %s | grep -z 'b.16'
55
* RUN: %clang_cc1 -E %s | not grep '# 0 '
6-
* RUN: %clang_cc1 -E -P %s | count 2
6+
* RUN: %clang_cc1 -E -P %s | count 4
77
* PR1848 PR3437 PR7360
88
*/
99

@@ -14,4 +14,3 @@ t(a
1414

1515
t(b
1616
__LINE__)
17-

0 commit comments

Comments
 (0)