================
@@ -0,0 +1,183 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/SmallBitVector.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/Error.h"
+#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]
+ // Strip the format part first, since it may contain commas (e.g.
{0:$[,]}).
+ StringRef IndexStr = Content;
+
+ const size_t ColonPos = Content.find(':');
+ if (ColonPos != StringRef::npos)
+ IndexStr = Content.substr(0, ColonPos);
+
+ const size_t CommaPos = IndexStr.find(',');
+ if (CommaPos != StringRef::npos)
+ IndexStr = IndexStr.substr(0, CommaPos);
+
+ IndexStr = IndexStr.trim();
----------------
localspook wrote:
If I'm understanding right, this line allows us to successfully parse something
like `{ 0 :x}` as a valid index? But does `formatv` actually allow that? If
it does, can we add tests for it?
https://github.com/llvm/llvm-project/pull/195974
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits