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

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 3 02:19:32 PST 2024


https://github.com/ilya-biryukov created https://github.com/llvm/llvm-project/pull/76804

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.

>From d8598a382fb1496a96d6ff8317c06cf73606ad84 Mon Sep 17 00:00:00 2001
From: Ilya Biryukov <ibiryukov at google.com>
Date: Wed, 3 Jan 2024 11:07:17 +0100
Subject: [PATCH] [Format] Fix isStartOfName to recognize attributes

This addresses a problem with formatting attributes in a different way
as before. For 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.
---
 clang/lib/Format/Format.cpp         | 2 --
 clang/lib/Format/TokenAnnotator.cpp | 3 ++-
 2 files changed, 2 insertions(+), 3 deletions(-)

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;
     }
 



More information about the cfe-commits mailing list