[llvm] abdc6a2 - [Instrumentor] Add spellcheck for instrumentor properties (#205416)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 2 09:48:49 PDT 2026
Author: Ethan Luis McDonough
Date: 2026-07-02T11:48:44-05:00
New Revision: abdc6a2181cdca2f23978ef577f493157e1e45fe
URL: https://github.com/llvm/llvm-project/commit/abdc6a2181cdca2f23978ef577f493157e1e45fe
DIFF: https://github.com/llvm/llvm-project/commit/abdc6a2181cdca2f23978ef577f493157e1e45fe.diff
LOG: [Instrumentor] Add spellcheck for instrumentor properties (#205416)
This patch adds spellcheck hints for unrecognized properties in the
instrumentor's JSON configuration.
Added:
llvm/test/Instrumentation/Instrumentor/bad_spelling.ll
llvm/test/Instrumentation/Instrumentor/bad_spelling_config.json
Modified:
llvm/lib/Transforms/IPO/InstrumentorConfigFile.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/IPO/InstrumentorConfigFile.cpp b/llvm/lib/Transforms/IPO/InstrumentorConfigFile.cpp
index 1038badc189a6..a2600b0551b65 100644
--- a/llvm/lib/Transforms/IPO/InstrumentorConfigFile.cpp
+++ b/llvm/lib/Transforms/IPO/InstrumentorConfigFile.cpp
@@ -15,6 +15,7 @@
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/ErrorHandling.h"
@@ -112,6 +113,20 @@ void writeConfigToJSON(InstrumentationConfig &IConf, StringRef OutputFile,
J.objectEnd();
}
+template <typename Map>
+static StringRef closestOption(const Map &Options, StringRef Missing) {
+ uint32_t MaxEdit = 5;
+ StringRef Closest;
+ for (const auto &Key : Options.keys()) {
+ auto EditDist = Missing.edit_distance_insensitive(Key, true, MaxEdit);
+ if (EditDist < MaxEdit) {
+ Closest = Key;
+ MaxEdit = EditDist;
+ }
+ }
+ return Closest;
+}
+
bool readConfigFromJSON(InstrumentationConfig &IConf, StringRef InputFile,
LLVMContext &Ctx, vfs::FileSystem &FS) {
if (InputFile.empty())
@@ -182,10 +197,12 @@ bool readConfigFromJSON(InstrumentationConfig &IConf, StringRef InputFile,
break;
}
} else if (!StringRef(ObjIt.first).ends_with(".description")) {
- Ctx.diagnose(DiagnosticInfoInstrumentation(
- Twine("configuration key '") + StringRef(ObjIt.first) +
- Twine("' not found and ignored"),
- DS_Warning));
+ std::string Diag = "configuration key '" + ObjIt.first.str() +
+ "' not found and ignored";
+ StringRef Closest = closestOption(BCOMap, ObjIt.first);
+ if (!Closest.empty())
+ Diag += "; did you mean '" + Closest.str() + "'?";
+ Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
}
}
continue;
@@ -202,16 +219,24 @@ bool readConfigFromJSON(InstrumentationConfig &IConf, StringRef InputFile,
}
auto *IO = IChoiceMap.lookup(ObjIt.first);
if (!IO) {
- Ctx.diagnose(DiagnosticInfoInstrumentation(
- Twine("malformed JSON configuration, expected an object matching "
- "an instrumentor choice, got ") +
- StringRef(ObjIt.first),
- DS_Warning));
+ std::string Diag =
+ "malformed JSON configuration, expected an object matching "
+ "an instrumentor choice, got '" +
+ ObjIt.first.str() + "'";
+ StringRef Closest = closestOption(IChoiceMap, ObjIt.first);
+ if (!Closest.empty())
+ Diag += "; did you mean '" + Closest.str() + "'?";
+ Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
continue;
}
SeenIOs.insert(IO);
StringMap<bool> ValueMap, ReplaceMap;
StringRef FilterStr;
+ StringSet<> IOOpts;
+ IOOpts.insert("enabled");
+ IOOpts.insert("filter");
+ for (auto &IRArg : IO->IRTArgs)
+ IOOpts.insert(IRArg.Name);
for (auto &InnerObjIt : *InnerObj) {
auto Name = StringRef(InnerObjIt.first);
if (Name == "filter") {
@@ -222,6 +247,15 @@ bool readConfigFromJSON(InstrumentationConfig &IConf, StringRef InputFile,
} else {
ValueMap[Name] = InnerObjIt.second.getAsBoolean().value_or(false);
}
+ if (!IOOpts.contains(Name)) {
+ std::string Diag = "unrecognized JSON property '" + Name.str() +
+ "' in configuration for '" + IO->getName().str() +
+ "'";
+ StringRef Closest = closestOption(IOOpts, Name);
+ if (!Closest.empty())
+ Diag += "; did you mean '" + Closest.str() + "'?";
+ Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
+ }
}
IO->Enabled = ValueMap["enabled"];
IO->Filter = FilterStr;
diff --git a/llvm/test/Instrumentation/Instrumentor/bad_spelling.ll b/llvm/test/Instrumentation/Instrumentor/bad_spelling.ll
new file mode 100644
index 0000000000000..4c1a9362489d0
--- /dev/null
+++ b/llvm/test/Instrumentation/Instrumentor/bad_spelling.ll
@@ -0,0 +1,7 @@
+; RUN: opt < %s -passes=instrumentor -instrumentor-read-config-files=%S/bad_spelling_config.json -S 2>&1 | FileCheck %s
+
+; CHECK-DAG: warning: malformed JSON configuration, expected an object matching an instrumentor choice, got 'numerci'; did you mean 'numeric'?
+; CHECK-DAG: warning: configuration key 'gpu_enabeld' not found and ignored; did you mean 'gpu_enabled'?
+; CHECK-DAG: warning: unrecognized JSON property 'opocde' in configuration for 'numeric'; did you mean 'opcode'?
+; CHECK-DAG: warning: unrecognized JSON property 'zzzzzz' in configuration for 'numeric'
+; CHECK-DAG: warning: unrecognized JSON property 'enabld' in configuration for 'numeric'; did you mean 'enabled'?
diff --git a/llvm/test/Instrumentation/Instrumentor/bad_spelling_config.json b/llvm/test/Instrumentation/Instrumentor/bad_spelling_config.json
new file mode 100644
index 0000000000000..f0fa47ca0b4a5
--- /dev/null
+++ b/llvm/test/Instrumentation/Instrumentor/bad_spelling_config.json
@@ -0,0 +1,30 @@
+{
+ "configuration": {
+ "runtime_prefix": "__instrumentor_",
+ "gpu_enabeld": false
+ },
+ "instruction_pre": {
+ "numeric": {
+ "enabld": true,
+ "type_id": true,
+ "size": true,
+ "opocde": true,
+ "left": true,
+ "right": true,
+ "zzzzzz": false,
+ "id": true
+ }
+ },
+ "instruction_post": {
+ "numerci": {
+ "enabled": true,
+ "type_id": true,
+ "size": true,
+ "opcode": true,
+ "left": true,
+ "right": true,
+ "result": true,
+ "id": true
+ }
+ }
+}
More information about the llvm-commits
mailing list