[clang-format] ensure clang-format command-line argument sets up the default left...
authormydeveloperday <mydeveloperday@gmail.com>
Fri, 24 Sep 2021 07:11:05 +0000 (08:11 +0100)
committermydeveloperday <mydeveloperday@gmail.com>
Fri, 24 Sep 2021 07:11:53 +0000 (08:11 +0100)
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

clang/tools/clang-format/ClangFormat.cpp

index 16d76a6..368e63e 100644 (file)
@@ -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;