[clang] 87ab958 - [clang-format] ensure clang-format command-line argument sets up the default left/right qualifier ordering

via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 24 00:12:18 PDT 2021


Author: mydeveloperday
Date: 2021-09-24T08:11:53+01:00
New Revision: 87ab958641fa30ed1e5880aaf43b909c552af0dc

URL: https://github.com/llvm/llvm-project/commit/87ab958641fa30ed1e5880aaf43b909c552af0dc
DIFF: https://github.com/llvm/llvm-project/commit/87ab958641fa30ed1e5880aaf43b909c552af0dc.diff

LOG: [clang-format] ensure clang-format command-line argument sets up the default left/right qualifier ordering

When specifying the alignment direction on the command line ensure
we set up the default ordering.

Fix spelling mistakes in the command-line argument

Reviewed By: HazardyKnusperkeks

Differential Revision: https://reviews.llvm.org/D110359

Added: 
    

Modified: 
    clang/tools/clang-format/ClangFormat.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/ClangFormat.cpp b/clang/tools/clang-format/ClangFormat.cpp
index 16d76a66afabc..368e63ef0aeab 100644
--- a/clang/tools/clang-format/ClangFormat.cpp
+++ b/clang/tools/clang-format/ClangFormat.cpp
@@ -106,9 +106,9 @@ static cl::opt<bool> SortIncludes(
     cl::cat(ClangFormatCategory));
 
 static cl::opt<std::string> QualifierAlignment(
-    "qualifier-aligment",
+    "qualifier-alignment",
     cl::desc(
-        "If set, overrides the cvqualifier style behavior determined by the "
+        "If set, overrides the qualifier alignment style determined by the "
         "QualifierAlignment style flag"),
     cl::init(""), cl::cat(ClangFormatCategory));
 
@@ -410,14 +410,27 @@ static bool format(StringRef FileName) {
     return true;
   }
 
-  StringRef ConstAlignment = QualifierAlignment;
+  StringRef QualifierAlignmentOrder = QualifierAlignment;
 
   FormatStyle->QualifierAlignment =
-      StringSwitch<FormatStyle::QualifierAlignmentStyle>(ConstAlignment.lower())
+      StringSwitch<FormatStyle::QualifierAlignmentStyle>(
+          QualifierAlignmentOrder.lower())
           .Case("right", FormatStyle::QAS_Right)
           .Case("left", FormatStyle::QAS_Left)
           .Default(FormatStyle->QualifierAlignment);
 
+  if (FormatStyle->QualifierAlignment == FormatStyle::QAS_Left)
+    FormatStyle->QualifierOrder = {"const", "volatile", "type"};
+  else if (FormatStyle->QualifierAlignment == FormatStyle::QAS_Right)
+    FormatStyle->QualifierOrder = {"type", "const", "volatile"};
+  else if (QualifierAlignmentOrder.contains("type")) {
+    FormatStyle->QualifierAlignment = FormatStyle::QAS_Custom;
+    SmallVector<StringRef> Qualifiers;
+    QualifierAlignmentOrder.split(Qualifiers, " ", /*MaxSplit=*/-1,
+                                  /*KeepEmpty=*/false);
+    FormatStyle->QualifierOrder = {Qualifiers.begin(), Qualifiers.end()};
+  }
+
   if (SortIncludes.getNumOccurrences() != 0) {
     if (SortIncludes)
       FormatStyle->SortIncludes = FormatStyle::SI_CaseSensitive;


        


More information about the cfe-commits mailing list