[clang-tools-extra] [clang-tidy] Add `llvm-formatv-string` (PR #195974)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Sun May 10 12:40:39 PDT 2026
================
@@ -0,0 +1,189 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatvStringCheck.h"
+#include "../utils/OptionsUtils.h"
+#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/Expr.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallBitVector.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Error.h"
+#include <iterator>
+#include <vector>
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::llvm_check {
+
+namespace {
+
+struct ParseResult {
+ SmallVector<unsigned, 4> Indices;
+ unsigned MaxIndex = 0;
+};
+
+} // namespace
+
+static Expected<ParseResult> parseFormatvString(StringRef Fmt) {
+ ParseResult Result;
+ unsigned NextAutoIndex = 0;
+ bool HasAutomatic = false;
+ bool HasExplicit = false;
+
+ while (!Fmt.empty()) {
+ const size_t OpenBrace = Fmt.find('{');
+ if (OpenBrace == StringRef::npos)
+ break;
+
+ Fmt = Fmt.drop_front(OpenBrace);
+
+ // Handle escaped braces '{{'.
+ if (Fmt.size() > 1 && Fmt[1] == '{') {
+ Fmt = Fmt.drop_front(2);
+ continue;
+ }
+
+ // Find the closing '}'.
+ const size_t CloseBrace = Fmt.find('}');
+ if (CloseBrace == StringRef::npos)
+ return llvm::createStringError("unterminated brace in format string");
+
+ // Extract the content between braces.
+ const StringRef Content = Fmt.substr(1, CloseBrace - 1);
+ Fmt = Fmt.drop_front(CloseBrace + 1);
+
+ // Parse the replacement field: [index] ["," layout] [":" format]
+ StringRef IndexStr = Content;
+
+ // Strip layout and format parts for index parsing.
+ const size_t CommaPos = Content.find(',');
+ const size_t ColonPos = Content.find(':');
+ if (CommaPos != StringRef::npos)
+ IndexStr = Content.substr(0, CommaPos);
+ else if (ColonPos != StringRef::npos)
+ IndexStr = Content.substr(0, ColonPos);
+
+ IndexStr = IndexStr.trim();
+
+ unsigned Index = 0;
+ if (IndexStr.empty()) {
+ Index = NextAutoIndex++;
+ HasAutomatic = true;
+ } else {
+ if (IndexStr.getAsInteger(10, Index))
+ return llvm::createStringError(
+ "invalid replacement index in format string");
+ HasExplicit = true;
+ }
+
+ Result.Indices.push_back(Index);
+ Result.MaxIndex = std::max(Result.MaxIndex, Index);
+ }
+
+ if (HasAutomatic && HasExplicit)
+ return llvm::createStringError(
+ "format string mixes automatic and explicit indices");
+
+ return Result;
+}
+
+FormatvStringCheck::FormatvStringCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ AdditionalFunctions(Options.get("AdditionalFunctions", "")) {
+ Functions = {"llvm::formatv", "llvm::createStringErrorV"};
+
+ std::vector<StringRef> CustomFunctions =
+ utils::options::parseStringList(AdditionalFunctions);
+ copy(CustomFunctions, std::back_inserter(Functions));
----------------
localspook wrote:
> Now I wonder, after parsing the AdditionalFunctions, should a :: namespace qualifier be added to its functions (if not present)?
(For context, the motivation for fully qualifying is to not match on things like `my_custom_namespace::llvm::formatv`)
While I agree that a user probably *intended* to fully qualify their own `AdditionalFunctions`, adding the `::` wouold be an invisible behavior-changing transformation, and we don't do it any other check, so I lean towards not touching what the user wrote.
> there could be a mix of qualified and unqualified names given to `hasAnyName`.
Having a mix is okay AFAIK, `hasAnyName` considers each name independently of the others.
https://github.com/llvm/llvm-project/pull/195974
More information about the cfe-commits
mailing list