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