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