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