@@ -3659,7 +3659,7 @@ static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3659
3659
3660
3660
// In C++ the implicit 'this' function parameter also counts, and they are
3661
3661
// counted from one.
3662
- bool HasImplicitThisParam = isInstanceMethod (D);
3662
+ bool HasImplicitThisParam = checkIfMethodHasImplicitObjectParameter (D);
3663
3663
unsigned NumArgs = getFunctionOrMethodNumParams (D) + HasImplicitThisParam;
3664
3664
3665
3665
IdentifierInfo *II = AL.getArgAsIdent (0 )->Ident ;
@@ -3772,7 +3772,7 @@ static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3772
3772
return ;
3773
3773
}
3774
3774
3775
- bool HasImplicitThisParam = isInstanceMethod (D);
3775
+ bool HasImplicitThisParam = checkIfMethodHasImplicitObjectParameter (D);
3776
3776
int32_t NumArgs = getFunctionOrMethodNumParams (D);
3777
3777
3778
3778
FunctionDecl *FD = D->getAsFunction ();
@@ -5578,6 +5578,181 @@ static void handlePreferredTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
5578
5578
D->addAttr (::new (S.Context ) PreferredTypeAttr (S.Context , AL, ParmTSI));
5579
5579
}
5580
5580
5581
+ // Diagnosing missing format attributes is implemented in two steps:
5582
+ // 1. Detect missing format attributes while checking function calls.
5583
+ // 2. Emit diagnostic in part that processes function body.
5584
+ // For this purpose it is created vector that stores information about format
5585
+ // attributes. There are no two format attributes with same arguments in a
5586
+ // vector. Vector could contains attributes that only store information about
5587
+ // format type (format string and first to check argument are set to -1).
5588
+ namespace {
5589
+ std::vector<FormatAttr *> MissingAttributes;
5590
+ } // end anonymous namespace
5591
+
5592
+ // This function is called only if function call is not inside template body.
5593
+ // TODO: Add call for function calls inside template body.
5594
+ // Detects and stores missing format attributes in a vector.
5595
+ void Sema::DetectMissingFormatAttributes (const FunctionDecl *Callee,
5596
+ ArrayRef<const Expr *> Args,
5597
+ SourceLocation Loc) {
5598
+ assert (Callee);
5599
+
5600
+ // If there is no caller, exit.
5601
+ const FunctionDecl *Caller = getCurFunctionDecl ();
5602
+ if (!getCurFunctionDecl ())
5603
+ return ;
5604
+
5605
+ // Check if callee function is a format function.
5606
+ // If it is, check if caller function misses format attributes.
5607
+
5608
+ if (!Callee->hasAttr <FormatAttr>())
5609
+ return ;
5610
+
5611
+ // va_list is not intended to be passed to variadic function.
5612
+ if (Callee->isVariadic ())
5613
+ return ;
5614
+
5615
+ // Check if va_list is passed to callee function.
5616
+ // If va_list is not passed, return.
5617
+ bool hasVaList = false ;
5618
+ for (const auto *Param : Callee->parameters ()) {
5619
+ if (Param->getOriginalType ().getCanonicalType () ==
5620
+ getASTContext ().getBuiltinVaListType ().getCanonicalType ()) {
5621
+ hasVaList = true ;
5622
+ break ;
5623
+ }
5624
+ }
5625
+ if (!hasVaList)
5626
+ return ;
5627
+
5628
+ unsigned int FormatArgumentIndexOffset =
5629
+ checkIfMethodHasImplicitObjectParameter (Callee) ? 2 : 1 ;
5630
+
5631
+ // If callee function is format function and format arguments are not
5632
+ // relevant to emit diagnostic, save only information about format type
5633
+ // (format index and first-to-check argument index are set to -1).
5634
+ // Information about format type is later used to determine if there are
5635
+ // more than one format type found.
5636
+
5637
+ unsigned int NumArgs = Args.size ();
5638
+ // Check if function has format attribute with forwarded format string.
5639
+ IdentifierInfo *AttrType;
5640
+ const ParmVarDecl *FormatStringArg;
5641
+ if (!llvm::any_of (
5642
+ Callee->specific_attrs <FormatAttr>(), [&](const FormatAttr *Attr) {
5643
+ AttrType = Attr->getType ();
5644
+
5645
+ int OffsetFormatIndex =
5646
+ Attr->getFormatIdx () - FormatArgumentIndexOffset;
5647
+ if (OffsetFormatIndex < 0 || (unsigned )OffsetFormatIndex >= NumArgs)
5648
+ return false ;
5649
+
5650
+ if (const auto *FormatArgExpr = dyn_cast<DeclRefExpr>(
5651
+ Args[OffsetFormatIndex]->IgnoreParenCasts ()))
5652
+ if (FormatStringArg = dyn_cast_or_null<ParmVarDecl>(
5653
+ FormatArgExpr->getReferencedDeclOfCallee ()))
5654
+ return true ;
5655
+ return false ;
5656
+ })) {
5657
+ MissingAttributes.push_back (
5658
+ FormatAttr::CreateImplicit (getASTContext (), AttrType, -1 , -1 ));
5659
+ return ;
5660
+ }
5661
+
5662
+ unsigned ArgumentIndexOffset =
5663
+ checkIfMethodHasImplicitObjectParameter (Caller) ? 2 : 1 ;
5664
+
5665
+ unsigned NumOfCallerFunctionParams = Caller->getNumParams ();
5666
+
5667
+ // Compare caller and callee function format attribute arguments (archetype
5668
+ // and format string). If they don't match, caller misses format attribute.
5669
+ if (llvm::any_of (
5670
+ Caller->specific_attrs <FormatAttr>(), [&](const FormatAttr *Attr) {
5671
+ if (Attr->getType () != AttrType)
5672
+ return false ;
5673
+ int OffsetFormatIndex = Attr->getFormatIdx () - ArgumentIndexOffset;
5674
+
5675
+ if (OffsetFormatIndex < 0 ||
5676
+ (unsigned )OffsetFormatIndex >= NumOfCallerFunctionParams)
5677
+ return false ;
5678
+
5679
+ if (Caller->parameters ()[OffsetFormatIndex] != FormatStringArg)
5680
+ return false ;
5681
+
5682
+ return true ;
5683
+ })) {
5684
+ MissingAttributes.push_back (
5685
+ FormatAttr::CreateImplicit (getASTContext (), AttrType, -1 , -1 ));
5686
+ return ;
5687
+ }
5688
+
5689
+ // Get format string index
5690
+ int FormatStringIndex =
5691
+ FormatStringArg->getFunctionScopeIndex () + ArgumentIndexOffset;
5692
+
5693
+ // Get first argument index
5694
+ int FirstToCheck = Caller->isVariadic ()
5695
+ ? (NumOfCallerFunctionParams + ArgumentIndexOffset)
5696
+ : 0 ;
5697
+
5698
+ // Do not add duplicate in a vector of missing format attributes.
5699
+ if (!llvm::any_of (MissingAttributes, [&](const FormatAttr *Attr) {
5700
+ return Attr->getType () == AttrType &&
5701
+ Attr->getFormatIdx () == FormatStringIndex &&
5702
+ Attr->getFirstArg () == FirstToCheck;
5703
+ }))
5704
+ MissingAttributes.push_back (FormatAttr::CreateImplicit (
5705
+ getASTContext (), AttrType, FormatStringIndex, FirstToCheck, Loc));
5706
+ }
5707
+
5708
+ // This function is called only if caller function is not template.
5709
+ // TODO: Add call for template functions.
5710
+ // Emits missing format attribute diagnostics.
5711
+ void Sema::EmitMissingFormatAttributesDiagnostic (const FunctionDecl *Caller) {
5712
+ const clang::IdentifierInfo *AttrType = MissingAttributes[0 ]->getType ();
5713
+ for (unsigned i = 1 ; i < MissingAttributes.size (); ++i) {
5714
+ if (AttrType != MissingAttributes[i]->getType ()) {
5715
+ // Clear vector of missing attributes because it could be used in
5716
+ // diagnosing missing format attributes in another caller.
5717
+ MissingAttributes.clear ();
5718
+ return ;
5719
+ }
5720
+ }
5721
+
5722
+ for (const FormatAttr *FA : MissingAttributes) {
5723
+ // If format index and first-to-check argument index are negative, it means
5724
+ // that this attribute is only saved for multiple format types checking.
5725
+ if (FA->getFormatIdx () < 0 || FA->getFirstArg () < 0 )
5726
+ continue ;
5727
+
5728
+ // If caller function has format attributes and callee format attribute type
5729
+ // mismatches caller attribute type, do not emit diagnostic.
5730
+ if (Caller->hasAttr <FormatAttr>() &&
5731
+ !llvm::any_of (Caller->specific_attrs <FormatAttr>(),
5732
+ [FA](const FormatAttr *FunctionAttr) {
5733
+ return FA->getType () == FunctionAttr->getType ();
5734
+ }))
5735
+ continue ;
5736
+
5737
+ // Emit diagnostic
5738
+ SourceLocation Loc = Caller->getFirstDecl ()->getLocation ();
5739
+ Diag (Loc, diag::warn_missing_format_attribute)
5740
+ << FA->getType () << Caller
5741
+ << FixItHint::CreateInsertion (Loc,
5742
+ (llvm::Twine (" __attribute__((format(" ) +
5743
+ FA->getType ()->getName () + " , " +
5744
+ llvm::Twine (FA->getFormatIdx ()) + " , " +
5745
+ llvm::Twine (FA->getFirstArg ()) + " )))" )
5746
+ .str ());
5747
+ Diag (FA->getLocation (), diag::note_format_function) << FA->getType ();
5748
+ }
5749
+
5750
+ // Clear vector of missing attributes after emitting diagnostics for caller
5751
+ // function because it could be used in diagnosing missing format attributes
5752
+ // in another caller.
5753
+ MissingAttributes.clear ();
5754
+ }
5755
+
5581
5756
// ===----------------------------------------------------------------------===//
5582
5757
// Microsoft specific attribute handlers.
5583
5758
// ===----------------------------------------------------------------------===//
0 commit comments