[clang-tools-extra] b15fcda - [JSON] Export sortedElements and fix CLANGD_TRACE non-determinism

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 28 22:10:19 PDT 2024


Author: Fangrui Song
Date: 2024-06-28T22:10:15-07:00
New Revision: b15fcdaf79e9b5931e8a7f9e09f8e3842d262dd7

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

LOG: [JSON] Export sortedElements and fix CLANGD_TRACE non-determinism

clangd/test/trace.test might fail as llvm::hash_value(StringRef) is
non-deterministic per process (#96282).

Added: 
    

Modified: 
    clang-tools-extra/clangd/support/Trace.cpp
    clang-tools-extra/clangd/test/trace.test
    llvm/include/llvm/Support/JSON.h
    llvm/lib/Support/JSON.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/support/Trace.cpp b/clang-tools-extra/clangd/support/Trace.cpp
index 419c2eee99ec8..7c6490f2e92fb 100644
--- a/clang-tools-extra/clangd/support/Trace.cpp
+++ b/clang-tools-extra/clangd/support/Trace.cpp
@@ -159,8 +159,8 @@ class JSONTracer : public EventTracer {
     Out.object([&] {
       Out.attribute("pid", 0);
       Out.attribute("ph", Phase);
-      for (const auto &KV : Event)
-        Out.attribute(KV.first, KV.second);
+      for (const auto *KV : llvm::json::sortedElements(Event))
+        Out.attribute(KV->first, KV->second);
     });
   }
 

diff  --git a/clang-tools-extra/clangd/test/trace.test b/clang-tools-extra/clangd/test/trace.test
index cb6a226b496a4..56f2d6333f1de 100644
--- a/clang-tools-extra/clangd/test/trace.test
+++ b/clang-tools-extra/clangd/test/trace.test
@@ -8,17 +8,17 @@
 # CHECK:   "traceEvents": [
 # CHECK:     {
 # CHECK:       "ph": "X",
-# CHECK:       "name": "BuildPreamble",
 # CHECK:       "args": {
 # CHECK:         "File": "{{.*(/|\\)}}foo.c"
 # CHECK:       },
+# CHECK:       "name": "BuildPreamble",
 # CHECK:     }
 # CHECK:     {
 # CHECK:       "ph": "X",
-# CHECK:       "name": "BuildAST",
 # CHECK:       "args": {
 # CHECK:         "File": "{{.*(/|\\)}}foo.c"
 # CHECK:       },
+# CHECK:       "name": "BuildAST",
 # CHECK:     }
 # CHECK:   ]
 # CHECK: }

diff  --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h
index 8b437bbabd962..14a5c7142ed8c 100644
--- a/llvm/include/llvm/Support/JSON.h
+++ b/llvm/include/llvm/Support/JSON.h
@@ -647,6 +647,8 @@ inline bool Object::erase(StringRef K) {
   return M.erase(ObjectKey(K));
 }
 
+std::vector<const Object::value_type *> sortedElements(const Object &O);
+
 /// A "cursor" marking a position within a Value.
 /// The Value is a tree, and this is the path from the root to the current node.
 /// This is used to associate errors with particular subobjects.

diff  --git a/llvm/lib/Support/JSON.cpp b/llvm/lib/Support/JSON.cpp
index 8f70d94827272..17779b58f81b7 100644
--- a/llvm/lib/Support/JSON.cpp
+++ b/llvm/lib/Support/JSON.cpp
@@ -242,7 +242,7 @@ Error Path::Root::getError() const {
   return createStringError(llvm::inconvertibleErrorCode(), OS.str());
 }
 
-static std::vector<const Object::value_type *> sortedElements(const Object &O) {
+std::vector<const Object::value_type *> sortedElements(const Object &O) {
   std::vector<const Object::value_type *> Elements;
   for (const auto &E : O)
     Elements.push_back(&E);


        


More information about the cfe-commits mailing list