[clang] [Format] Fix isStartOfName to recognize attributes (PR #76804)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 17:21:50 PST 2024


owenca wrote:

> > can you also add a test to clang/unittests/Format/TokenAnnotatorTest.cpp that ensures trailing attribute-like macros receive `StartOfName` annotation to make sure we don't regress the signal in the future?
> 
> ok, that opened a whole can of worms.
> 
> ```
>  Tokens = annotate("void foo GUARDED_BY(x)");
> ```
> 
> gets annotated as
> 
> ```
> {(void, "void" , Unknown),
>   (identifier, "foo" , StartOfName),
>   (identifier, "GUARDED_BY" , FunctionDeclarationName),
>   (l_paren, "(" , Unknown),
>   (identifier, "x" , Unknown),
>   (r_paren, ")" , Unknown),
>   (eof, "" , Unknown)}
> ```
> 
> I expected to get some heuristics for attributes, but instead `GUARDED_BY` gets annotated as a function declaration name. It feels that the current behavior is a result of two mistakes cancelling each other out. I don't think adding a unit test like this is warranted, even if formatting behavior is actually correct.
> 
> @owenca what are your thoughts on this change and whether we should add a test here?

We usually add a FIXME test wrapped in a `#if 0` block:
```
// FIXME: ...
#if 0
Tokens = annotate("void foo GUARDED_BY(x);");
...
Tokens = annotate("void foo GUARDED_BY(x) {}");
...
#endif
```

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


More information about the cfe-commits mailing list