[clang-tools-extra] 2cdb843 - [clang-tidy] Sort options in --dump-config

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 10:56:39 PDT 2023


Author: Piotr Zegar
Date: 2023-07-27T17:50:59Z
New Revision: 2cdb843767529b2be641b93e43d70837900345c1

URL: https://github.com/llvm/llvm-project/commit/2cdb843767529b2be641b93e43d70837900345c1
DIFF: https://github.com/llvm/llvm-project/commit/2cdb843767529b2be641b93e43d70837900345c1.diff

LOG: [clang-tidy] Sort options in --dump-config

Sort printed options in --dump-config output.

Fixes: #64153

Reviewed By: carlosgalvezp

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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/infrastructure/config-files.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
index 1efe35a8b8f015..f800e560331016 100644
--- a/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
+++ b/clang-tools-extra/clang-tidy/ClangTidyOptions.cpp
@@ -16,6 +16,7 @@
 #include "llvm/Support/MemoryBufferRef.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/YAMLTraits.h"
+#include <algorithm>
 #include <optional>
 #include <utility>
 
@@ -85,14 +86,21 @@ template <>
 void yamlize(IO &IO, ClangTidyOptions::OptionMap &Options, bool,
              EmptyContext &Ctx) {
   if (IO.outputting()) {
+    // Ensure check options are sorted
+    std::vector<std::pair<StringRef, StringRef>> SortedOptions;
+    SortedOptions.reserve(Options.size());
+    for (auto &Key : Options) {
+      SortedOptions.emplace_back(Key.getKey(), Key.getValue().Value);
+    }
+    std::sort(SortedOptions.begin(), SortedOptions.end());
+
     IO.beginMapping();
     // Only output as a map
-    for (auto &Key : Options) {
-      bool UseDefault;
-      void *SaveInfo;
-      IO.preflightKey(Key.getKey().data(), true, false, UseDefault, SaveInfo);
-      StringRef S = Key.getValue().Value;
-      IO.scalarString(S, needsQuotes(S));
+    for (auto &Option : SortedOptions) {
+      bool UseDefault = false;
+      void *SaveInfo = nullptr;
+      IO.preflightKey(Option.first.data(), true, false, UseDefault, SaveInfo);
+      IO.scalarString(Option.second, needsQuotes(Option.second));
       IO.postflightKey(SaveInfo);
     }
     IO.endMapping();

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 4441473774b2c8..d8c55c17be3e2f 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -111,6 +111,8 @@ Improvements to clang-tidy
 - Remove configuration option `AnalyzeTemporaryDestructors`, which was deprecated since
   :program:`clang-tidy` 16.
 
+- Improved `--dump-config` to print check options in alphabetical order.
+
 New checks
 ^^^^^^^^^^
 

diff  --git a/clang-tools-extra/test/clang-tidy/infrastructure/config-files.cpp b/clang-tools-extra/test/clang-tidy/infrastructure/config-files.cpp
index 6c42bd7f495f78..ac71b1846a61ee 100644
--- a/clang-tools-extra/test/clang-tidy/infrastructure/config-files.cpp
+++ b/clang-tools-extra/test/clang-tidy/infrastructure/config-files.cpp
@@ -54,3 +54,6 @@
 // RUN: %S/Inputs/config-files/4/44/- -- | FileCheck %s -check-prefix=CHECK-CHILD6
 // CHECK-CHILD6: Checks: {{.*-llvm-qualified-auto'? *$}}
 // CHECK-CHILD6-NOT: modernize-use-using.IgnoreMacros
+
+// Validate that check options are printed in alphabetical order:
+// RUN: clang-tidy --checks="-*,readability-identifier-*" --dump-config | grep readability-identifier-naming | sort --check


        


More information about the cfe-commits mailing list