https://github.com/budimirarandjelovichtec updated 
https://github.com/llvm/llvm-project/pull/105479

From f2a8d63ccb5e3e7a748532944c31c4935a9b6115 Mon Sep 17 00:00:00 2001
From: budimirarandjelovicsyrmia <budimir.arandjelo...@syrmia.com>
Date: Fri, 5 Apr 2024 15:20:37 +0200
Subject: [PATCH] [clang] Catch missing format attributes

---
 clang/docs/ReleaseNotes.rst                   |   3 +
 clang/include/clang/Basic/DiagnosticGroups.td |   1 -
 .../clang/Basic/DiagnosticSemaKinds.td        |   3 +
 clang/include/clang/Sema/Attr.h               |   7 +
 clang/include/clang/Sema/Sema.h               |   2 +
 clang/lib/Sema/SemaDecl.cpp                   |   2 +
 clang/lib/Sema/SemaDeclAttr.cpp               | 219 +++++++++++++++++-
 clang/test/Sema/attr-format-missing.c         | 217 +++++++++++++++++
 clang/test/Sema/attr-format-missing.cpp       | 180 ++++++++++++++
 9 files changed, 631 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Sema/attr-format-missing.c
 create mode 100644 clang/test/Sema/attr-format-missing.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 44d5f348ed2d54..0a663eb97c7770 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -378,6 +378,9 @@ Improvements to Clang's diagnostics
 
 - Clang now emits a diagnostic note at the class declaration when the method 
definition does not match any declaration (#GH110638).
 
+- Clang now diagnoses missing format attributes for non-template functions and
+  class/struct/union members. Fixes #GH60718
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 41e719d4d57816..850823ee7c48e3 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -529,7 +529,6 @@ def MainReturnType : DiagGroup<"main-return-type">;
 def MaxUnsignedZero : DiagGroup<"max-unsigned-zero">;
 def MissingBraces : DiagGroup<"missing-braces">;
 def MissingDeclarations: DiagGroup<"missing-declarations">;
-def : DiagGroup<"missing-format-attribute">;
 def MissingIncludeDirs : DiagGroup<"missing-include-dirs">;
 def MissingNoreturn : DiagGroup<"missing-noreturn">;
 def MultiChar : DiagGroup<"multichar">;
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td 
b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 59dc81cfeb7111..f165cb6d8dc207 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1046,6 +1046,9 @@ def err_opencl_invalid_param : Error<
   "declaring function parameter of type %0 is not allowed%select{; did you 
forget * ?|}1">;
 def err_opencl_invalid_return : Error<
   "declaring function return value of type %0 is not allowed %select{; did you 
forget * ?|}1">;
+def warn_missing_format_attribute : Warning<
+  "diagnostic behavior may be improved by adding the %0 format attribute to 
the declaration of %1">,
+  InGroup<DiagGroup<"missing-format-attribute">>, DefaultIgnore;
 def warn_pragma_options_align_reset_failed : Warning<
   "#pragma options align=reset failed: %0">,
   InGroup<IgnoredPragmas>;
diff --git a/clang/include/clang/Sema/Attr.h b/clang/include/clang/Sema/Attr.h
index 3f0b10212789a4..37c124ca7b454a 100644
--- a/clang/include/clang/Sema/Attr.h
+++ b/clang/include/clang/Sema/Attr.h
@@ -123,6 +123,13 @@ inline bool isInstanceMethod(const Decl *D) {
   return false;
 }
 
+inline bool checkIfMethodHasImplicitObjectParameter(const Decl *D) {
+  if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
+    return MethodDecl->isInstance() &&
+           !MethodDecl->hasCXXExplicitFunctionObjectParameter();
+  return false;
+}
+
 /// Diagnose mutually exclusive attributes when present on a given
 /// declaration. Returns true if diagnosed.
 template <typename AttrTy>
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index bede971ce0191b..7129fa8c8f1359 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4574,6 +4574,8 @@ class Sema final : public SemaBase {
 
   enum class RetainOwnershipKind { NS, CF, OS };
 
+  void DiagnoseMissingFormatAttributes(Stmt *Body, const FunctionDecl *FDecl);
+
   UuidAttr *mergeUuidAttr(Decl *D, const AttributeCommonInfo &CI,
                           StringRef UuidAsWritten, MSGuidDecl *GuidDecl);
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 2bf610746bc317..0ec29cfeb728bf 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16020,6 +16020,8 @@ Decl *Sema::ActOnFinishFunctionBody(Decl *dcl, Stmt 
*Body,
         }
       }
 
+      DiagnoseMissingFormatAttributes(Body, FD);
+
       // We might not have found a prototype because we didn't wish to warn on
       // the lack of a missing prototype. Try again without the checks for
       // whether we want to warn on the missing prototype.
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index c9b9f3a0007daa..813a530092507e 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -3575,7 +3575,7 @@ static void handleFormatAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
 
   // In C++ the implicit 'this' function parameter also counts, and they are
   // counted from one.
-  bool HasImplicitThisParam = isInstanceMethod(D);
+  bool HasImplicitThisParam = checkIfMethodHasImplicitObjectParameter(D);
   unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
 
   IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
@@ -3688,7 +3688,7 @@ static void handleCallbackAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
     return;
   }
 
-  bool HasImplicitThisParam = isInstanceMethod(D);
+  bool HasImplicitThisParam = checkIfMethodHasImplicitObjectParameter(D);
   int32_t NumArgs = getFunctionOrMethodNumParams(D);
 
   FunctionDecl *FD = D->getAsFunction();
@@ -5391,6 +5391,221 @@ static void handlePreferredTypeAttr(Sema &S, Decl *D, 
const ParsedAttr &AL) {
   D->addAttr(::new (S.Context) PreferredTypeAttr(S.Context, AL, ParmTSI));
 }
 
+// Returns vector of format attributes. There are no two attributes with same
+// arguments in returning vector. There can be attributes that effectively only
+// store information about format type.
+static std::vector<FormatAttr *>
+GetMissingFormatAttributes(Sema &S, Stmt *Body, const FunctionDecl *FDecl) {
+  unsigned int ArgumentIndexOffset =
+      checkIfMethodHasImplicitObjectParameter(FDecl) ? 2 : 1;
+
+  std::vector<FormatAttr *> MissingAttributes;
+
+  // Iterate over body statements.
+  for (auto *Child : Body->children()) {
+    // If child statement is compound statement, recursively get missing
+    // attributes.
+    if (dyn_cast_or_null<CompoundStmt>(Child)) {
+      std::vector<FormatAttr *> CompoundStmtMissingAttributes =
+          GetMissingFormatAttributes(S, Child, FDecl);
+
+      // If there are already missing attributes with same arguments, do not 
add
+      // duplicates.
+      for (FormatAttr *FA : CompoundStmtMissingAttributes) {
+        if (!llvm::any_of(MissingAttributes, [&](const FormatAttr *Attr) {
+              return FA->getType() == Attr->getType() &&
+                     FA->getFormatIdx() == Attr->getFormatIdx() &&
+                     FA->getFirstArg() == Attr->getFirstArg();
+            }))
+          MissingAttributes.push_back(FA);
+      }
+
+      continue;
+    }
+
+    ValueStmt *VS = dyn_cast<ValueStmt>(Child);
+    if (!VS)
+      continue;
+    CallExpr *TheCall = dyn_cast_or_null<CallExpr>(VS->getExprStmt());
+    if (!TheCall)
+      continue;
+
+    const FunctionDecl *CalleeFunction =
+        dyn_cast_or_null<FunctionDecl>(TheCall->getCalleeDecl());
+    if (!CalleeFunction || !CalleeFunction->hasAttr<FormatAttr>())
+      continue;
+
+    // va_list is not intended to be passed to variadic function.
+    if (CalleeFunction->isVariadic())
+      continue;
+
+    Expr **Args = TheCall->getArgs();
+    unsigned int NumArgs = TheCall->getNumArgs();
+
+    // Check if va_list is passed to callee function.
+    // If va_list is not passed, continue to the next iteration.
+    bool hasVaList = false;
+    for (const auto *Param : CalleeFunction->parameters()) {
+      if (Param->getOriginalType().getCanonicalType() ==
+          S.getASTContext().getBuiltinVaListType().getCanonicalType()) {
+        hasVaList = true;
+        break;
+      }
+    }
+    if (!hasVaList)
+      continue;
+
+    // If callee expression is function, check if it is format function.
+    // If it is, check if caller function misses format attributes.
+
+    unsigned int CalleeFunctionFormatArgumentIndexOffset =
+        checkIfMethodHasImplicitObjectParameter(CalleeFunction) ? 2 : 1;
+
+    // If callee function is format function and format arguments are not
+    // relevant to emit diagnostic, save only information about format type
+    // (format index and first-to-check argument index are set to -1).
+    // Information about format type is later used to determine if there are
+    // more than one format type found.
+
+    // Check if function has format attribute with forwarded format string.
+    IdentifierInfo *AttrType;
+    const ParmVarDecl *FormatArg;
+    if (!llvm::any_of(CalleeFunction->specific_attrs<FormatAttr>(),
+                      [&](const FormatAttr *Attr) {
+                        AttrType = Attr->getType();
+
+                        int OffsetFormatIndex =
+                            Attr->getFormatIdx() -
+                            CalleeFunctionFormatArgumentIndexOffset;
+                        if (OffsetFormatIndex < 0 ||
+                            (unsigned)OffsetFormatIndex >= NumArgs)
+                          return false;
+
+                        if (const auto *FormatArgExpr = dyn_cast<DeclRefExpr>(
+                                Args[OffsetFormatIndex]->IgnoreParenCasts()))
+                          if (FormatArg = dyn_cast_or_null<ParmVarDecl>(
+                                  FormatArgExpr->getReferencedDeclOfCallee()))
+                            return true;
+                        return false;
+                      })) {
+      MissingAttributes.push_back(
+          FormatAttr::CreateImplicit(S.getASTContext(), AttrType, -1, -1));
+      continue;
+    }
+
+    // Do not add in a vector format attributes whose type is different than
+    // caller function attribute type.
+    if (llvm::any_of(FDecl->specific_attrs<FormatAttr>(),
+                     [AttrType](const FormatAttr *FunctionAttr) {
+                       return AttrType != FunctionAttr->getType();
+                     }))
+      continue;
+
+    // If format string argument is caller function parameter, get string 
index.
+    // Otherwise, save only attribute type and go to next iteration.
+    int StringIndex = [=]() -> int {
+      for (const ParmVarDecl *Param : FDecl->parameters()) {
+        if (Param == FormatArg)
+          return Param->getFunctionScopeIndex() + ArgumentIndexOffset;
+      }
+      return 0;
+    }();
+
+    if (StringIndex == 0) {
+      MissingAttributes.push_back(
+          FormatAttr::CreateImplicit(S.getASTContext(), AttrType, -1, -1));
+      continue;
+    }
+
+    unsigned NumOfCallerFunctionParams = FDecl->getNumParams();
+
+    // Compare caller and callee function format attribute arguments (archetype
+    // and format string).
+    if (llvm::any_of(
+            FDecl->specific_attrs<FormatAttr>(), [&](const FormatAttr *Attr) {
+              if (Attr->getType() != AttrType)
+                return false;
+              int OffsetFormatIndex =
+                  Attr->getFormatIdx() - ArgumentIndexOffset;
+
+              if (OffsetFormatIndex < 0 ||
+                  (unsigned)OffsetFormatIndex >= NumOfCallerFunctionParams)
+                return false;
+
+              if (FDecl->parameters()[OffsetFormatIndex] != FormatArg)
+                return false;
+
+              return true;
+            })) {
+      MissingAttributes.push_back(
+          FormatAttr::CreateImplicit(S.getASTContext(), AttrType, -1, -1));
+      continue;
+    }
+
+    // Get first argument index
+    int FirstToCheck = FDecl->isVariadic()
+                           ? (NumOfCallerFunctionParams + ArgumentIndexOffset)
+                           : 0;
+
+    // If there are already attributes which arguments matches arguments
+    // detected in this iteration, do not add new attribute as it would be
+    // duplicate.
+    if (!llvm::any_of(MissingAttributes, [&](const FormatAttr *Attr) {
+          return Attr->getType() == AttrType &&
+                 Attr->getFormatIdx() == StringIndex &&
+                 Attr->getFirstArg() == FirstToCheck;
+        }))
+      MissingAttributes.push_back(FormatAttr::CreateImplicit(
+          S.getASTContext(), AttrType, StringIndex, FirstToCheck));
+  }
+
+  return MissingAttributes;
+}
+
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Emit warnings if caller function misses format attributes.
+void Sema::DiagnoseMissingFormatAttributes(Stmt *Body,
+                                           const FunctionDecl *FDecl) {
+  assert(FDecl);
+
+  // If there are no function body, exit.
+  if (!Body)
+    return;
+
+  // Get missing format attributes
+  std::vector<FormatAttr *> MissingFormatAttributes =
+      GetMissingFormatAttributes(*this, Body, FDecl);
+  if (MissingFormatAttributes.empty())
+    return;
+
+  // Check if there are more than one format type found. In that case do not
+  // emit diagnostic.
+  const clang::IdentifierInfo *AttrType = 
MissingFormatAttributes[0]->getType();
+  for (unsigned i = 1; i < MissingFormatAttributes.size(); ++i) {
+    if (AttrType != MissingFormatAttributes[i]->getType())
+      return;
+  }
+
+  for (const FormatAttr *FA : MissingFormatAttributes) {
+    // If format index and first-to-check argument index are negative, it means
+    // that this attribute is only saved for multiple format types checking.
+    if (FA->getFormatIdx() < 0 || FA->getFirstArg() < 0)
+      continue;
+
+    // Emit diagnostic
+    SourceLocation Loc = FDecl->getFirstDecl()->getLocation();
+    Diag(Loc, diag::warn_missing_format_attribute)
+        << FA->getType() << FDecl
+        << FixItHint::CreateInsertion(Loc,
+                                      (llvm::Twine("__attribute__((format(") +
+                                       FA->getType()->getName() + ", " +
+                                       llvm::Twine(FA->getFormatIdx()) + ", " +
+                                       llvm::Twine(FA->getFirstArg()) + ")))")
+                                          .str());
+  }
+}
+
 
//===----------------------------------------------------------------------===//
 // Microsoft specific attribute handlers.
 
//===----------------------------------------------------------------------===//
diff --git a/clang/test/Sema/attr-format-missing.c 
b/clang/test/Sema/attr-format-missing.c
new file mode 100644
index 00000000000000..b2f09410ac37cb
--- /dev/null
+++ b/clang/test/Sema/attr-format-missing.c
@@ -0,0 +1,217 @@
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,c_diagnostics 
-Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -verify=expected,cpp_diagnostics 
-Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -verify=expected,cpp_diagnostics 
-std=c++23 -Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -x c++ -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+#ifndef __cplusplus
+typedef __CHAR16_TYPE__ char16_t;
+typedef __CHAR32_TYPE__ char32_t;
+typedef __WCHAR_TYPE__ wchar_t;
+#endif
+
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *, size_t, const char *, va_list); // #vsnprintf
+
+#ifndef __cplusplus
+int vwscanf(const wchar_t *, va_list); // #vwscanf
+#endif
+
+__attribute__((__format__(__scanf__, 1, 4)))
+void f1(char *out, const size_t len, const char *format, ... /* args */) // #f1
+{
+    va_list args;
+    vsnprintf(out, len, format, args); // expected-no-warning@#f1
+}
+
+__attribute__((__format__(__printf__, 1, 4)))
+void f2(char *out, const size_t len, const char *format, ... /* args */) // #f2
+{
+    va_list args;
+    vsnprintf(out, len, format, args); // expected-warning@#f2 {{diagnostic 
behavior may be improved by adding the 'printf' format attribute to the 
declaration of 'f2'}}
+                                       // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:6-[[@LINE-4]]:6}:"__attribute__((format(printf, 3, 
4)))"
+}
+
+void f3(char *out, va_list args) // #f3
+{
+    vprintf(out, args); // expected-warning@#f3 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f3'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-3]]:6-[[@LINE-3]]:6}:"__attribute__((format(printf, 1, 
0)))"
+}
+
+void f4(char* out, ... /* args */) // #f4
+{
+    va_list args;
+    vprintf("test", args); // expected-no-warning@#f4
+
+    const char *ch;
+    vprintf(ch, args); // expected-no-warning@#f4
+}
+
+void f5(va_list args) // #f5
+{
+    char *ch;
+    vscanf(ch, args); // expected-no-warning@#f5
+}
+
+void f6(char *out, va_list args) // #f6
+{
+    char *ch;
+    vprintf(ch, args); // expected-no-warning@#f6
+    vprintf("test", args); // expected-no-warning@#f6
+    vprintf(out, args); // expected-warning@#f6 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f6'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-6]]:6-[[@LINE-6]]:6}:"__attribute__((format(printf, 1, 
0)))"
+}
+
+void f7(const char *out, ... /* args */) // #f7
+{
+    va_list args;
+
+    vscanf(out, args); // expected-warning@#f7 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f7'}}
+                       // CHECK: 
fix-it:"{{.*}}":{[[@LINE-5]]:6-[[@LINE-5]]:6}:"__attribute__((format(scanf, 1, 
2)))"
+}
+
+void f8(const char *out, ... /* args */) // #f8
+{
+    va_list args;
+
+    vscanf(out, args); // expected-no-warning@#f8
+    vprintf(out, args); // expected-no-warning@#f8
+}
+
+void f9(const char out[], ... /* args */) // #f9
+{
+    va_list args;
+    char *ch;
+    vprintf(ch, args); // expected-no-warning
+    vsprintf(ch, out, args); // expected-warning@#f9 {{diagnostic behavior may 
be improved by adding the 'printf' format attribute to the declaration of 'f9'}}
+                             // CHECK: 
fix-it:"{{.*}}":{[[@LINE-6]]:6-[[@LINE-6]]:6}:"__attribute__((format(printf, 1, 
2)))"
+}
+
+#ifndef __cplusplus
+void f10(const wchar_t *out, ... /* args */) // #f10
+{
+    va_list args;
+    vwscanf(out, args); // expected-no-warning@#f10
+}
+#endif
+
+void f11(const char *out) // #f11
+{
+    va_list args;
+    vscanf(out, args); // expected-warning@#f11 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f11'}}
+                       // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:6-[[@LINE-4]]:6}:"__attribute__((format(scanf, 1, 
0)))"
+}
+
+void f12(char* out) // #f12
+{
+    va_list args;
+    const char* ch;
+    vsprintf(out, ch, args); // expected-no-warning@#f12
+    vprintf(out, args); // expected-warning@#f12 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f12'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-6]]:6-[[@LINE-6]]:6}:"__attribute__((format(printf, 1, 
0)))"
+}
+
+void f13(const char *out, ... /* args */) // #f13
+{
+    va_list args;
+    printf(out, args); // expected-no-warning@#f13
+}
+
+void f14(char *out, ... /* args */) // #f14
+{
+    va_list args;
+    vscanf(out, args); // expected-no-warning@#f14
+    vprintf(out, args); // expected-no-warning@#f14
+}
+
+void f15(char *out, ... /* args */) // #f15
+{
+    va_list args;
+    vscanf(out, args); // expected-no-warning@#f15
+    {
+        vprintf(out, args); // expected-no-warning@#f15
+    }
+}
+
+void f16(char *out, va_list args) // #f16
+{
+    {
+        vscanf(out, args); // expected-no-warning@#f16
+        vprintf(out, args); // expected-no-warning@#f16
+    }
+}
+
+// expected-warning@#f17 {{diagnostic behavior may be improved by adding the 
'scanf' format attribute to the declaration of 'f17'}}
+// CHECK: 
fix-it:"{{.*}}":{[[@LINE+1]]:6-[[@LINE+1]]:6}:"__attribute__((format(scanf, 1, 
2)))"
+void f17(char *out, ... /* args */) // #f17
+{
+    va_list args;
+    vscanf(out, args);
+    {
+        vscanf(out, args);
+    }
+}
+
+void f18(char *ch, const char *out, ... /* args */) // #f18
+{
+    va_list args;
+    vprintf(ch, args); // expected-warning@#f18 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f18}}
+                       // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:6-[[@LINE-4]]:6}:"__attribute__((format(printf, 1, 
3)))"
+    vprintf(out, args); // expected-warning@#f18 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f18'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-6]]:6-[[@LINE-6]]:6}:"__attribute__((format(printf, 2, 
3)))"
+}
+
+typedef va_list tdVaList;
+typedef int tdInt;
+
+void f19(const char *out, ... /* args */) // #f19
+{
+    tdVaList args;
+    vprintf(out, args); // expected-warning@#f19 {{diagnostic behavior may be 
improved by adding the 'printf' format attribute to the declaration of 'f19'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:6-[[@LINE-4]]:6}:"__attribute__((format(printf, 1, 
2)))"
+}
+
+void f20(const char *out, tdVaList args) // #f20
+{
+    vscanf(out, args); // expected-warning@#f20 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'f20'}}
+                       // CHECK: 
fix-it:"{{.*}}":{[[@LINE-3]]:6-[[@LINE-3]]:6}:"__attribute__((format(scanf, 1, 
0)))"
+}
+
+void f21(const char *out, tdVaList args) // #f21
+{
+    vscanf(out, args); // expected-no-warning@#f21
+    vprintf(out, args); // expected-no-warning@#f21
+}
+
+void f22(char *out, ... /* args */) // #f22
+{
+    va_list args;
+    char *ch;
+    vscanf(ch, args); // expected-no-warning@#f22
+    vprintf(out, args); // expected-no-warning@#f22
+}
+
+void f23(char *out, ... /* args */) // #f23
+{
+    va_list args;
+    vscanf("%s", args); // expected-no-warning@#f23
+    vprintf(out, args); // expected-no-warning@#f23
+}
diff --git a/clang/test/Sema/attr-format-missing.cpp 
b/clang/test/Sema/attr-format-missing.cpp
new file mode 100644
index 00000000000000..16096a04fb5dbc
--- /dev/null
+++ b/clang/test/Sema/attr-format-missing.cpp
@@ -0,0 +1,180 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wmissing-format-attribute %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 -Wmissing-format-attribute 
%s
+// RUN: not %clang_cc1 -fsyntax-only -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+// RUN: not %clang_cc1 -fsyntax-only -Wmissing-format-attribute 
-fdiagnostics-parseable-fixits -std=c++23 %s 2>&1
+// FileCheck %s --check-prefixes=CHECK,CHECK-EXPLICIT-THIS-PARAMETER
+
+typedef __SIZE_TYPE__ size_t;
+typedef __builtin_va_list va_list;
+
+namespace std
+{
+    template<class Elem> struct basic_string_view {};
+    template<class Elem> struct basic_string {
+        const Elem *c_str() const noexcept;
+        basic_string(const basic_string_view<Elem> SW);
+    };
+
+    using string = basic_string<char>;
+    using wstring = basic_string<wchar_t>;
+    using string_view = basic_string_view<char>;
+    using wstring_view = basic_string_view<wchar_t>;
+}
+
+__attribute__((__format__(__printf__, 1, 2)))
+int printf(const char *, ...); // #printf
+
+__attribute__((__format__(__scanf__, 1, 2)))
+int scanf(const char *, ...); // #scanf
+
+__attribute__((__format__(__printf__, 1, 0)))
+int vprintf(const char *, va_list); // #vprintf
+
+__attribute__((__format__(__scanf__, 1, 0)))
+int vscanf(const char *, va_list); // #vscanf
+
+__attribute__((__format__(__printf__, 2, 0)))
+int vsprintf(char *, const char *, va_list); // #vsprintf
+
+__attribute__((__format__(__printf__, 3, 0)))
+int vsnprintf(char *, size_t, const char *, va_list); // #vsnprintf
+
+int vwprintf(const wchar_t *, va_list); // #vwprintf
+
+void f1(const std::string &str, ... /* args */) // #f1
+{
+    va_list args;
+    vscanf(str.c_str(), args); // expected-no-warning@#f1
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f2(const std::string &str, ... /* args */); // #f2
+
+void f3(std::string_view str, ... /* args */) // #f3
+{
+    va_list args;
+    vscanf(std::string(str).c_str(), args); // expected-no-warning@#f3
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f4(std::string_view str, ... /* args */); // #f4
+
+void f5(const std::wstring &str, ... /* args */) // #f5
+{
+    va_list args;
+    vwprintf(str.c_str(), args); // expected-no-warning@#f5
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f6(const std::wstring &str, ... /* args */); // #f6
+
+void f7(std::wstring_view str, ... /* args */) // #f7
+{
+    va_list args;
+    vwprintf(std::wstring(str).c_str(), args); // expected-no-warning@#f7
+}
+
+__attribute__((format(printf, 1, 2))) // expected-error {{format argument not 
a string type}}
+void f8(std::wstring_view str, ... /* args */); // #f8
+
+struct S1
+{
+    void fn1(const char *out, ... /* args */) // #S1_fn1
+    {
+        va_list args;
+        vscanf(out, args); // expected-warning@#S1_fn1 {{diagnostic behavior 
may be improved by adding the 'scanf' format attribute to the declaration of 
'fn1'}}
+                           // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:10-[[@LINE-4]]:10}:"__attribute__((format(scanf, 
2, 3)))"
+    }
+
+    __attribute__((format(scanf, 2, 0)))
+    void fn2(const char *out, va_list args); // #S1_fn2
+
+    void fn3(const char *out, ... /* args */); // #S1_fn3
+
+#if __has_extension(cxx_explicit_this_parameter)
+    void fn4(this S1& explicitThis, const char *out, va_list args) // #S1_fn4
+    {
+        explicitThis.fn2(out, args); // expected-warning@#S1_fn4 {{diagnostic 
behavior may be improved by adding the 'scanf' format attribute to the 
declaration of 'fn4'}}
+                                     // CHECK-EXPLICIT-THIS-PARAMETER: 
fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"__attribute__((format(scanf, 
2, 0)))"
+    }
+#endif
+};
+
+void S1::fn3(const char *out, ... /* args */)
+{
+    va_list args;
+    fn2(out, args); // expected-warning@#S1_fn3 {{diagnostic behavior may be 
improved by adding the 'scanf' format attribute to the declaration of 'fn3'}}
+                    // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:10-[[@LINE-4]]:10}:"__attribute__((format(scanf, 
2, 3)))"
+}
+
+union U1
+{
+    __attribute__((format(printf, 2, 0)))
+    void fn1(const char *out, va_list args); // #U1_fn1
+
+    void fn2(const char *out, ... /* args */) // #U1_fn2
+    {
+        va_list args;
+        fn1(out, args); // expected-warning@#U1_fn2 {{diagnostic behavior may 
be improved by adding the 'printf' format attribute to the declaration of 
'fn2'}}
+                        // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:10-[[@LINE-4]]:10}:"__attribute__((format(printf, 
2, 3)))"
+    }
+
+#if __has_extension(cxx_explicit_this_parameter)
+    void fn3(this U1&, const char *out) // #U1_fn3
+    {
+        va_list args;
+        vprintf(out, args); // expected-warning@#U1_fn3 {{diagnostic behavior 
may be improved by adding the 'printf' format attribute to the declaration of 
'fn3'}}
+                           // CHECK-EXPLICIT-THIS-PARAMETER: 
fix-it:"{{.*}}":{[[@LINE-4]]:10-[[@LINE-4]]:10}:"__attribute__((format(printf, 
2, 0)))"
+    }
+#endif
+};
+
+class C1
+{
+    __attribute__((format(printf, 3, 0)))
+    void fn1(const int n, const char *out, va_list args); // #C1_fn1
+
+    void fn2(const char *out, const int n, ... /* args */) // #C1_fn2
+    {
+        va_list args;
+        fn1(n, out, args); // expected-warning@#C1_fn2 {{diagnostic behavior 
may be improved by adding the 'printf' format attribute to the declaration of 
'fn2'}}
+                           // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:10-[[@LINE-4]]:10}:"__attribute__((format(printf, 
2, 4)))"
+    }
+
+#if __has_extension(cxx_explicit_this_parameter)
+    void fn3(this const C1&, const char *out, va_list args) // #C1_fn3
+    {
+        vscanf(out, args); // expected-warning@#C1_fn3 {{diagnostic behavior 
may be improved by adding the 'scanf' format attribute to the declaration of 
'fn3'}}
+                          // CHECK-EXPLICIT-THIS-PARAMETER: 
fix-it:"{{.*}}":{[[@LINE-3]]:10-[[@LINE-3]]:10}:"__attribute__((format(scanf, 
2, 0)))"
+    }
+#endif
+
+    C1(const int n, const char *out) //#C1_C1a
+    {
+        va_list args;
+        fn1(n, out, args); // expected-warning@#C1_C1a {{diagnostic behavior 
may be improved by adding the 'printf' format attribute to the declaration of 
'C1'}}
+                           // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:5-[[@LINE-4]]:5}:"__attribute__((format(printf, 3, 
0)))"
+    }
+
+    C1(const char *out, ... /* args */) // #C1_C1b
+    {
+        va_list args;
+        vprintf(out, args); // expected-warning@#C1_C1b {{diagnostic behavior 
may be improved by adding the 'printf' format attribute to the declaration of 
'C1'}}
+                           // CHECK: 
fix-it:"{{.*}}":{[[@LINE-4]]:5-[[@LINE-4]]:5}:"__attribute__((format(printf, 2, 
3)))"
+    }
+
+    ~C1() // #d_C1
+    {
+        const char *out;
+        va_list args;
+        vprintf(out, args); // expected-no-warning@#d_C1
+    }
+};
+
+// TODO: implement for templates
+template <int N>
+void func(char (&str)[N], ... /* args */) // #func
+{
+    va_list args;
+    vprintf(str, args); // expected-no-warning@#func
+}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to