[clang] [LifetimeSafety] Add fix-it for misplaced lifetimebound attributes (PR #199149)
David Meng via cfe-commits
cfe-commits at lists.llvm.org
Wed May 27 09:59:18 PDT 2026
================
@@ -1,17 +1,24 @@
// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-cross-tu-misplaced-lifetimebound -Wno-dangling -I%t -verify %t/cross.cpp
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-cross-tu-misplaced-lifetimebound -Wno-dangling -I%t -fdiagnostics-parseable-fixits %t/cross.cpp 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -Wlifetime-safety-cross-tu-misplaced-lifetimebound -Wno-dangling -I%t -fixit %t/cross.cpp
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-cross-tu-misplaced-lifetimebound -Wno-dangling -I%t -Werror %t/cross.cpp
//--- cross.h
struct HeaderObj {
~HeaderObj() {}
};
-HeaderObj &header_param(HeaderObj &obj); // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers in other translation units; add it to the declaration instead}}
+HeaderObj &header_param(HeaderObj & // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers in other translation units; add it to the declaration instead}}
+ obj // CHECK: fix-it:"{{.*}}cross.h":{[[#]]:{{[0-9]+}}-[[#]]:{{[0-9]+}}}:" {{\[\[}}clang::lifetimebound{{\]\]}}"
+);
struct HeaderS {
HeaderObj data;
- HeaderObj &header_this(); // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers in other translation units; add it to the declaration instead}}
+ HeaderObj &header_this(
+ ); // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers in other translation units; add it to the declaration instead}}
+ // CHECK: fix-it:"{{.*}}cross.h":{[[#]]:{{[0-9]+}}-[[#]]:{{[0-9]+}}}:" {{\[\[}}clang::lifetimebound{{\]\]}}"
----------------
davidmenggx wrote:
No, the fix-it would appear on the same line as the warning.
To be more explicit this could also work:
```cpp
struct HeaderS {
HeaderObj data;
HeaderObj &header_this(
); // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers in other translation units; add it to the declaration instead}}
// CHECK: cross.h:[[#WARN_LINE:]]:{{[0-9]+}}: warning: 'lifetimebound' attribute on this definition is not visible
// CHECK: fix-it:"{{.*}}cross.h":{[[#WARN_LINE]]:{{[0-9]+}}-[[#WARN_LINE]]:{{[0-9]+}}}:" {{\[\[}}clang::lifetimebound{{\]\]}}"
};
```
`@LINE-1` doesn't work here due to the mismatch between the test file's line numbers and `cross.h`'s line numbers
https://github.com/llvm/llvm-project/pull/199149
More information about the cfe-commits
mailing list