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

via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 3 02:20:02 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Ilya Biryukov (ilya-biryukov)

<details>
<summary>Changes</summary>

This addresses a problem with formatting attributes. Some context:
- 199fc973ced20016b04ba540cf63a1d4914fa513 changed `isStartOfName` to fix problems inside macro directives (judging by the added tests), but this behavior changed formatting of attribute macros in an undesirable way.
- efeb546865c233dfa7706ee0316c676de9f69897 changed Google format style to fix some widely used attributes.

Instead of changing the format style, this commit specializes behavior introduced in 199fc973ced20016b04ba540cf63a1d4914fa513 to macro directives. This seems to work well in both cases.

Also update the test with two `GUARDED_BY` directives. While the formatting after efeb546865c233dfa7706ee0316c676de9f69897 seems better, this case is rare enough to not warrant the extra complexity. We are reverting it back to the state it had before
efeb546865c233dfa7706ee0316c676de9f69897.

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


2 Files Affected:

- (modified) clang/lib/Format/Format.cpp (-2) 
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2-1) 


``````````diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f798d555bf9929..38974f578fe1d2 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1698,8 +1698,6 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
           /*BasedOnStyle=*/"google",
       },
   };
-  GoogleStyle.AttributeMacros.push_back("GUARDED_BY");
-  GoogleStyle.AttributeMacros.push_back("ABSL_GUARDED_BY");
 
   GoogleStyle.SpacesBeforeTrailingComments = 2;
   GoogleStyle.Standard = FormatStyle::LS_Auto;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3ac3aa3c5e3a22..94fe5b21cfc6e6 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2209,7 +2209,8 @@ class AnnotatingParser {
         (!NextNonComment && !Line.InMacroBody) ||
         (NextNonComment &&
          (NextNonComment->isPointerOrReference() ||
-          NextNonComment->isOneOf(tok::identifier, tok::string_literal)))) {
+          (Line.InPragmaDirective &&
+           NextNonComment->isOneOf(tok::identifier, tok::string_literal))))) {
       return false;
     }
 

``````````

</details>


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


More information about the cfe-commits mailing list