Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Sema/SemaDeclAttr.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file implements decl-related attribute processing.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/AST/ASTConsumer.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/ASTMutationListener.h"
16
#include "clang/AST/CXXInheritance.h"
17
#include "clang/AST/DeclCXX.h"
18
#include "clang/AST/DeclObjC.h"
19
#include "clang/AST/DeclTemplate.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/ExprCXX.h"
22
#include "clang/AST/Mangle.h"
23
#include "clang/AST/RecursiveASTVisitor.h"
24
#include "clang/AST/Type.h"
25
#include "clang/Basic/CharInfo.h"
26
#include "clang/Basic/SourceManager.h"
27
#include "clang/Basic/TargetBuiltins.h"
28
#include "clang/Basic/TargetInfo.h"
29
#include "clang/Lex/Preprocessor.h"
30
#include "clang/Sema/DeclSpec.h"
31
#include "clang/Sema/DelayedDiagnostic.h"
32
#include "clang/Sema/Initialization.h"
33
#include "clang/Sema/Lookup.h"
34
#include "clang/Sema/ParsedAttr.h"
35
#include "clang/Sema/Scope.h"
36
#include "clang/Sema/ScopeInfo.h"
37
#include "clang/Sema/SemaInternal.h"
38
#include "llvm/ADT/Optional.h"
39
#include "llvm/ADT/STLExtras.h"
40
#include "llvm/ADT/StringExtras.h"
41
#include "llvm/Support/MathExtras.h"
42
#include "llvm/Support/raw_ostream.h"
43
44
using namespace clang;
45
using namespace sema;
46
47
namespace AttributeLangSupport {
48
  enum LANG {
49
    C,
50
    Cpp,
51
    ObjC
52
  };
53
} // end namespace AttributeLangSupport
54
55
//===----------------------------------------------------------------------===//
56
//  Helper functions
57
//===----------------------------------------------------------------------===//
58
59
/// isFunctionOrMethod - Return true if the given decl has function
60
/// type (function or function-typed variable) or an Objective-C
61
/// method.
62
78.1k
static bool isFunctionOrMethod(const Decl *D) {
63
78.1k
  return (D->getFunctionType() != nullptr) || 
isa<ObjCMethodDecl>(D)316
;
64
78.1k
}
65
66
/// Return true if the given decl has function type (function or
67
/// function-typed variable) or an Objective-C method or a block.
68
77.9k
static bool isFunctionOrMethodOrBlock(const Decl *D) {
69
77.9k
  return isFunctionOrMethod(D) || 
isa<BlockDecl>(D)1
;
70
77.9k
}
71
72
/// Return true if the given decl has a declarator that should have
73
/// been processed by Sema::GetTypeForDeclarator.
74
16.0k
static bool hasDeclarator(const Decl *D) {
75
  // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
76
16.0k
  return isa<DeclaratorDecl>(D) || 
isa<BlockDecl>(D)359
||
isa<TypedefNameDecl>(D)350
||
77
205
         isa<ObjCPropertyDecl>(D);
78
16.0k
}
79
80
/// hasFunctionProto - Return true if the given decl has a argument
81
/// information. This decl should have already passed
82
/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
83
78.1k
static bool hasFunctionProto(const Decl *D) {
84
78.1k
  if (const FunctionType *FnTy = D->getFunctionType())
85
77.8k
    return isa<FunctionProtoType>(FnTy);
86
320
  return isa<ObjCMethodDecl>(D) || 
isa<BlockDecl>(D)9
;
87
320
}
88
89
/// getFunctionOrMethodNumParams - Return number of function or method
90
/// parameters. It is an error to call this on a K&R function (use
91
/// hasFunctionProto first).
92
186k
static unsigned getFunctionOrMethodNumParams(const Decl *D) {
93
186k
  if (const FunctionType *FnTy = D->getFunctionType())
94
182k
    return cast<FunctionProtoType>(FnTy)->getNumParams();
95
3.88k
  if (const auto *BD = dyn_cast<BlockDecl>(D))
96
17
    return BD->getNumParams();
97
3.86k
  return cast<ObjCMethodDecl>(D)->param_size();
98
3.86k
}
99
100
static const ParmVarDecl *getFunctionOrMethodParam(const Decl *D,
101
70.5k
                                                   unsigned Idx) {
102
70.5k
  if (const auto *FD = dyn_cast<FunctionDecl>(D))
103
70.2k
    return FD->getParamDecl(Idx);
104
338
  if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
105
23
    return MD->getParamDecl(Idx);
106
315
  if (const auto *BD = dyn_cast<BlockDecl>(D))
107
16
    return BD->getParamDecl(Idx);
108
299
  return nullptr;
109
299
}
110
111
109k
static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) {
112
109k
  if (const FunctionType *FnTy = D->getFunctionType())
113
105k
    return cast<FunctionProtoType>(FnTy)->getParamType(Idx);
114
3.85k
  if (const auto *BD = dyn_cast<BlockDecl>(D))
115
8
    return BD->getParamDecl(Idx)->getType();
116
117
3.84k
  return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType();
118
3.84k
}
119
120
70.5k
static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) {
121
70.5k
  if (auto *PVD = getFunctionOrMethodParam(D, Idx))
122
70.2k
    return PVD->getSourceRange();
123
299
  return SourceRange();
124
299
}
125
126
16.4k
static QualType getFunctionOrMethodResultType(const Decl *D) {
127
16.4k
  if (const FunctionType *FnTy = D->getFunctionType())
128
16.1k
    return FnTy->getReturnType();
129
308
  return cast<ObjCMethodDecl>(D)->getReturnType();
130
308
}
131
132
80
static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) {
133
80
  if (const auto *FD = dyn_cast<FunctionDecl>(D))
134
76
    return FD->getReturnTypeSourceRange();
135
4
  if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
136
4
    return MD->getReturnTypeSourceRange();
137
0
  return SourceRange();
138
0
}
139
140
96.7k
static bool isFunctionOrMethodVariadic(const Decl *D) {
141
96.7k
  if (const FunctionType *FnTy = D->getFunctionType())
142
93.7k
    return cast<FunctionProtoType>(FnTy)->isVariadic();
143
2.96k
  if (const auto *BD = dyn_cast<BlockDecl>(D))
144
6
    return BD->isVariadic();
145
2.95k
  return cast<ObjCMethodDecl>(D)->isVariadic();
146
2.95k
}
147
148
115k
static bool isInstanceMethod(const Decl *D) {
149
115k
  if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(D))
150
246
    return MethodDecl->isInstance();
151
115k
  return false;
152
115k
}
153
154
6.79k
static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
155
6.79k
  const auto *PT = T->getAs<ObjCObjectPointerType>();
156
6.79k
  if (!PT)
157
1.40k
    return false;
158
159
5.38k
  ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
160
5.38k
  if (!Cls)
161
0
    return false;
162
163
5.38k
  IdentifierInfo* ClsName = Cls->getIdentifier();
164
165
  // FIXME: Should we walk the chain of classes?
166
5.38k
  return ClsName == &Ctx.Idents.get("NSString") ||
167
0
         ClsName == &Ctx.Idents.get("NSMutableString");
168
5.38k
}
169
170
2.59k
static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
171
2.59k
  const auto *PT = T->getAs<PointerType>();
172
2.59k
  if (!PT)
173
3
    return false;
174
175
2.59k
  const auto *RT = PT->getPointeeType()->getAs<RecordType>();
176
2.59k
  if (!RT)
177
518
    return false;
178
179
2.07k
  const RecordDecl *RD = RT->getDecl();
180
2.07k
  if (RD->getTagKind() != TTK_Struct)
181
0
    return false;
182
183
2.07k
  return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
184
2.07k
}
185
186
47.9M
static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
187
  // FIXME: Include the type in the argument list.
188
47.9M
  return AL.getNumArgs() + AL.hasParsedType();
189
47.9M
}
190
191
template <typename Compare>
192
static bool checkAttributeNumArgsImpl(Sema &S, const ParsedAttr &AL,
193
                                      unsigned Num, unsigned Diag,
194
47.9M
                                      Compare Comp) {
195
47.9M
  if (Comp(getNumAttributeArgs(AL), Num)) {
196
344
    S.Diag(AL.getLoc(), Diag) << AL << Num;
197
344
    return false;
198
344
  }
199
200
47.9M
  return true;
201
47.9M
}
SemaDeclAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::not_equal_to<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::not_equal_to<unsigned int>)
Line
Count
Source
194
47.8M
                                      Compare Comp) {
195
47.8M
  if (Comp(getNumAttributeArgs(AL), Num)) {
196
277
    S.Diag(AL.getLoc(), Diag) << AL << Num;
197
277
    return false;
198
277
  }
199
200
47.8M
  return true;
201
47.8M
}
SemaDeclAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::less<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::less<unsigned int>)
Line
Count
Source
194
18.5k
                                      Compare Comp) {
195
18.5k
  if (Comp(getNumAttributeArgs(AL), Num)) {
196
48
    S.Diag(AL.getLoc(), Diag) << AL << Num;
197
48
    return false;
198
48
  }
199
200
18.4k
  return true;
201
18.4k
}
SemaDeclAttr.cpp:bool checkAttributeNumArgsImpl<std::__1::greater<unsigned int> >(clang::Sema&, clang::ParsedAttr const&, unsigned int, unsigned int, std::__1::greater<unsigned int>)
Line
Count
Source
194
141k
                                      Compare Comp) {
195
141k
  if (Comp(getNumAttributeArgs(AL), Num)) {
196
19
    S.Diag(AL.getLoc(), Diag) << AL << Num;
197
19
    return false;
198
19
  }
199
200
141k
  return true;
201
141k
}
202
203
/// Check if the attribute has exactly as many args as Num. May
204
/// output an error.
205
47.8M
static bool checkAttributeNumArgs(Sema &S, const ParsedAttr &AL, unsigned Num) {
206
47.8M
  return checkAttributeNumArgsImpl(S, AL, Num,
207
47.8M
                                   diag::err_attribute_wrong_number_arguments,
208
47.8M
                                   std::not_equal_to<unsigned>());
209
47.8M
}
210
211
/// Check if the attribute has at least as many args as Num. May
212
/// output an error.
213
static bool checkAttributeAtLeastNumArgs(Sema &S, const ParsedAttr &AL,
214
18.5k
                                         unsigned Num) {
215
18.5k
  return checkAttributeNumArgsImpl(S, AL, Num,
216
18.5k
                                   diag::err_attribute_too_few_arguments,
217
18.5k
                                   std::less<unsigned>());
218
18.5k
}
219
220
/// Check if the attribute has at most as many args as Num. May
221
/// output an error.
222
static bool checkAttributeAtMostNumArgs(Sema &S, const ParsedAttr &AL,
223
141k
                                        unsigned Num) {
224
141k
  return checkAttributeNumArgsImpl(S, AL, Num,
225
141k
                                   diag::err_attribute_too_many_arguments,
226
141k
                                   std::greater<unsigned>());
227
141k
}
228
229
/// A helper function to provide Attribute Location for the Attr types
230
/// AND the ParsedAttr.
231
template <typename AttrInfo>
232
static std::enable_if_t<std::is_base_of<Attr, AttrInfo>::value, SourceLocation>
233
23
getAttrLoc(const AttrInfo &AL) {
234
23
  return AL.getLocation();
235
23
}
SemaDeclAttr.cpp:std::__1::enable_if<std::is_base_of<clang::Attr, clang::AMDGPUFlatWorkGroupSizeAttr>::value, clang::SourceLocation>::type getAttrLoc<clang::AMDGPUFlatWorkGroupSizeAttr>(clang::AMDGPUFlatWorkGroupSizeAttr const&)
Line
Count
Source
233
9
getAttrLoc(const AttrInfo &AL) {
234
9
  return AL.getLocation();
235
9
}
SemaDeclAttr.cpp:std::__1::enable_if<std::is_base_of<clang::Attr, clang::AMDGPUWavesPerEUAttr>::value, clang::SourceLocation>::type getAttrLoc<clang::AMDGPUWavesPerEUAttr>(clang::AMDGPUWavesPerEUAttr const&)
Line
Count
Source
233
9
getAttrLoc(const AttrInfo &AL) {
234
9
  return AL.getLocation();
235
9
}
SemaDeclAttr.cpp:std::__1::enable_if<std::is_base_of<clang::Attr, clang::AllocAlignAttr>::value, clang::SourceLocation>::type getAttrLoc<clang::AllocAlignAttr>(clang::AllocAlignAttr const&)
Line
Count
Source
233
5
getAttrLoc(const AttrInfo &AL) {
234
5
  return AL.getLocation();
235
5
}
236
73
static SourceLocation getAttrLoc(const ParsedAttr &AL) { return AL.getLoc(); }
237
238
/// If Expr is a valid integer constant, get the value of the integer
239
/// expression and return success or failure. May output an error.
240
///
241
/// Negative argument is implicitly converted to unsigned, unless
242
/// \p StrictlyUnsigned is true.
243
template <typename AttrInfo>
244
static bool checkUInt32Argument(Sema &S, const AttrInfo &AI, const Expr *Expr,
245
                                uint32_t &Val, unsigned Idx = UINT_MAX,
246
1.11M
                                bool StrictlyUnsigned = false) {
247
1.11M
  Optional<llvm::APSInt> I = llvm::APSInt(32);
248
1.11M
  if (Expr->isTypeDependent() || Expr->isValueDependent() ||
249
1.11M
      !(I = Expr->getIntegerConstantExpr(S.Context))) {
250
32
    if (Idx != UINT_MAX)
251
22
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
252
22
          << &AI << Idx << AANT_ArgumentIntegerConstant
253
22
          << Expr->getSourceRange();
254
10
    else
255
10
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
256
10
          << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
257
32
    return false;
258
32
  }
259
260
1.11M
  if (!I->isIntN(32)) {
261
10
    S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
262
10
        << I->toString(10, false) << 32 << /* Unsigned */ 1;
263
10
    return false;
264
10
  }
265
266
1.11M
  if (StrictlyUnsigned && 
I->isSigned()87
&&
I->isNegative()87
) {
267
3
    S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
268
3
        << &AI << /*non-negative*/ 1;
269
3
    return false;
270
3
  }
271
272
1.11M
  Val = (uint32_t)I->getZExtValue();
273
1.11M
  return true;
274
1.11M
}
SemaDeclAttr.cpp:bool checkUInt32Argument<clang::AMDGPUFlatWorkGroupSizeAttr>(clang::Sema&, clang::AMDGPUFlatWorkGroupSizeAttr const&, clang::Expr const*, unsigned int&, unsigned int, bool)
Line
Count
Source
246
205
                                bool StrictlyUnsigned = false) {
247
205
  Optional<llvm::APSInt> I = llvm::APSInt(32);
248
205
  if (Expr->isTypeDependent() || Expr->isValueDependent() ||
249
205
      !(I = Expr->getIntegerConstantExpr(S.Context))) {
250
9
    if (Idx != UINT_MAX)
251
9
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
252
9
          << &AI << Idx << AANT_ArgumentIntegerConstant
253
9
          << Expr->getSourceRange();
254
0
    else
255
0
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
256
0
          << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
257
9
    return false;
258
9
  }
259
260
196
  if (!I->isIntN(32)) {
261
3
    S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
262
3
        << I->toString(10, false) << 32 << /* Unsigned */ 1;
263
3
    return false;
264
3
  }
265
266
193
  if (StrictlyUnsigned && 
I->isSigned()0
&&
I->isNegative()0
) {
267
0
    S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
268
0
        << &AI << /*non-negative*/ 1;
269
0
    return false;
270
0
  }
271
272
193
  Val = (uint32_t)I->getZExtValue();
273
193
  return true;
274
193
}
SemaDeclAttr.cpp:bool checkUInt32Argument<clang::AMDGPUWavesPerEUAttr>(clang::Sema&, clang::AMDGPUWavesPerEUAttr const&, clang::Expr const*, unsigned int&, unsigned int, bool)
Line
Count
Source
246
192
                                bool StrictlyUnsigned = false) {
247
192
  Optional<llvm::APSInt> I = llvm::APSInt(32);
248
192
  if (Expr->isTypeDependent() || Expr->isValueDependent() ||
249
192
      !(I = Expr->getIntegerConstantExpr(S.Context))) {
250
9
    if (Idx != UINT_MAX)
251
9
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
252
9
          << &AI << Idx << AANT_ArgumentIntegerConstant
253
9
          << Expr->getSourceRange();
254
0
    else
255
0
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
256
0
          << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
257
9
    return false;
258
9
  }
259
260
183
  if (!I->isIntN(32)) {
261
3
    S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
262
3
        << I->toString(10, false) << 32 << /* Unsigned */ 1;
263
3
    return false;
264
3
  }
265
266
180
  if (StrictlyUnsigned && 
I->isSigned()0
&&
I->isNegative()0
) {
267
0
    S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
268
0
        << &AI << /*non-negative*/ 1;
269
0
    return false;
270
0
  }
271
272
180
  Val = (uint32_t)I->getZExtValue();
273
180
  return true;
274
180
}
SemaDeclAttr.cpp:bool checkUInt32Argument<clang::ParsedAttr>(clang::Sema&, clang::ParsedAttr const&, clang::Expr const*, unsigned int&, unsigned int, bool)
Line
Count
Source
246
1.11M
                                bool StrictlyUnsigned = false) {
247
1.11M
  Optional<llvm::APSInt> I = llvm::APSInt(32);
248
1.11M
  if (Expr->isTypeDependent() || Expr->isValueDependent() ||
249
1.11M
      !(I = Expr->getIntegerConstantExpr(S.Context))) {
250
14
    if (Idx != UINT_MAX)
251
4
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
252
4
          << &AI << Idx << AANT_ArgumentIntegerConstant
253
4
          << Expr->getSourceRange();
254
10
    else
255
10
      S.Diag(getAttrLoc(AI), diag::err_attribute_argument_type)
256
10
          << &AI << AANT_ArgumentIntegerConstant << Expr->getSourceRange();
257
14
    return false;
258
14
  }
259
260
1.11M
  if (!I->isIntN(32)) {
261
4
    S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
262
4
        << I->toString(10, false) << 32 << /* Unsigned */ 1;
263
4
    return false;
264
4
  }
265
266
1.11M
  if (StrictlyUnsigned && 
I->isSigned()87
&&
I->isNegative()87
) {
267
3
    S.Diag(getAttrLoc(AI), diag::err_attribute_requires_positive_integer)
268
3
        << &AI << /*non-negative*/ 1;
269
3
    return false;
270
3
  }
271
272
1.11M
  Val = (uint32_t)I->getZExtValue();
273
1.11M
  return true;
274
1.11M
}
275
276
/// Wrapper around checkUInt32Argument, with an extra check to be sure
277
/// that the result will fit into a regular (signed) int. All args have the same
278
/// purpose as they do in checkUInt32Argument.
279
template <typename AttrInfo>
280
static bool checkPositiveIntArgument(Sema &S, const AttrInfo &AI, const Expr *Expr,
281
6.12k
                                     int &Val, unsigned Idx = UINT_MAX) {
282
6.12k
  uint32_t UVal;
283
6.12k
  if (!checkUInt32Argument(S, AI, Expr, UVal, Idx))
284
2
    return false;
285
286
6.12k
  if (UVal > (uint32_t)std::numeric_limits<int>::max()) {
287
1
    llvm::APSInt I(32); // for toString
288
1
    I = UVal;
289
1
    S.Diag(Expr->getExprLoc(), diag::err_ice_too_large)
290
1
        << I.toString(10, false) << 32 << /* Unsigned */ 0;
291
1
    return false;
292
1
  }
293
294
6.12k
  Val = UVal;
295
6.12k
  return true;
296
6.12k
}
297
298
/// Diagnose mutually exclusive attributes when present on a given
299
/// declaration. Returns true if diagnosed.
300
template <typename AttrTy>
301
8.11M
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
8.11M
  if (const auto *A = D->getAttr<AttrTy>()) {
303
49
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
49
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
49
    return true;
306
49
  }
307
8.11M
  return false;
308
8.11M
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::Mips16Attr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
28
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
28
  if (const auto *A = D->getAttr<AttrTy>()) {
303
4
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
4
    return true;
306
4
  }
307
24
  return false;
308
24
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::MicroMipsAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
11
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
11
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
10
  return false;
308
10
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::MipsInterruptAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
10
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
10
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
8
  return false;
308
8
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::MipsShortCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
16
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
16
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
14
  return false;
308
14
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::MipsLongCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
16
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
16
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
14
  return false;
308
14
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::NotTailCalledAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
8.10M
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
8.10M
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
8.10M
  return false;
308
8.10M
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDASharedAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
176
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
176
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
174
  return false;
308
174
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDADeviceAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
271
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
271
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
269
  return false;
308
269
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDAHostAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
269
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
269
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
267
  return false;
308
267
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDAGlobalAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
2.64k
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
2.64k
  if (const auto *A = D->getAttr<AttrTy>()) {
303
4
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
4
    return true;
306
4
  }
307
2.63k
  return false;
308
2.63k
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDADeviceBuiltinTextureTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
10
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
10
  if (const auto *A = D->getAttr<AttrTy>()) {
303
0
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
0
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
0
    return true;
306
0
  }
307
10
  return false;
308
10
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDADeviceBuiltinSurfaceTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
8
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
8
  if (const auto *A = D->getAttr<AttrTy>()) {
303
0
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
0
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
0
    return true;
306
0
  }
307
8
  return false;
308
8
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::HotAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
3.77k
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
3.77k
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
3.77k
  return false;
308
3.77k
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::ColdAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
7
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
7
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
6
  return false;
308
6
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::DisableTailCallsAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
31
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
31
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
30
  return false;
308
30
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CUDAConstantAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
234
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
234
  if (const auto *A = D->getAttr<AttrTy>()) {
303
4
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
4
    return true;
306
4
  }
307
230
  return false;
308
230
}
Unexecuted instantiation: SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CFUnknownTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CFAuditedTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
1
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
1
  if (const auto *A = D->getAttr<AttrTy>()) {
303
0
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
0
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
0
    return true;
306
0
  }
307
1
  return false;
308
1
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::NoSpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
27
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
27
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
25
  return false;
308
25
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::SpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
27
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
27
  if (const auto *A = D->getAttr<AttrTy>()) {
303
2
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
2
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
2
    return true;
306
2
  }
307
25
  return false;
308
25
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::AlwaysInlineAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
668
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
668
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
667
  return false;
308
667
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::NakedAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
422
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
422
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
421
  return false;
308
421
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::PointerAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
28
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
28
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
27
  return false;
308
27
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::OwnerAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
16
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
16
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
15
  return false;
308
15
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::NoDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
28
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
28
  if (const auto *A = D->getAttr<AttrTy>()) {
303
4
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
4
    return true;
306
4
  }
307
24
  return false;
308
24
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::AlwaysDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
42
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
42
  if (const auto *A = D->getAttr<AttrTy>()) {
303
4
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
4
    return true;
306
4
  }
307
38
  return false;
308
38
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::InternalLinkageAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
9
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
9
  if (const auto *A = D->getAttr<AttrTy>()) {
303
1
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
1
    return true;
306
1
  }
307
8
  return false;
308
8
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CommonAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
301
1.16k
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const ParsedAttr &AL) {
302
1.16k
  if (const auto *A = D->getAttr<AttrTy>()) {
303
3
    S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << A;
304
3
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
305
3
    return true;
306
3
  }
307
1.16k
  return false;
308
1.16k
}
309
310
template <typename AttrTy>
311
11
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
312
11
  if (const auto *A = D->getAttr<AttrTy>()) {
313
4
    S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
314
4
                                                                      << A;
315
4
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
316
4
    return true;
317
4
  }
318
7
  return false;
319
7
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::InternalLinkageAttr>(clang::Sema&, clang::Decl*, clang::Attr const&)
Line
Count
Source
311
1
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
312
1
  if (const auto *A = D->getAttr<AttrTy>()) {
313
1
    S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
314
1
                                                                      << A;
315
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
316
1
    return true;
317
1
  }
318
0
  return false;
319
0
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::CommonAttr>(clang::Sema&, clang::Decl*, clang::Attr const&)
Line
Count
Source
311
8
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
312
8
  if (const auto *A = D->getAttr<AttrTy>()) {
313
1
    S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
314
1
                                                                      << A;
315
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
316
1
    return true;
317
1
  }
318
7
  return false;
319
7
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::SpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::Attr const&)
Line
Count
Source
311
1
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
312
1
  if (const auto *A = D->getAttr<AttrTy>()) {
313
1
    S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
314
1
                                                                      << A;
315
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
316
1
    return true;
317
1
  }
318
0
  return false;
319
0
}
SemaDeclAttr.cpp:bool checkAttrMutualExclusion<clang::NoSpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::Attr const&)
Line
Count
Source
311
1
static bool checkAttrMutualExclusion(Sema &S, Decl *D, const Attr &AL) {
312
1
  if (const auto *A = D->getAttr<AttrTy>()) {
313
1
    S.Diag(AL.getLocation(), diag::err_attributes_are_not_compatible) << &AL
314
1
                                                                      << A;
315
1
    S.Diag(A->getLocation(), diag::note_conflicting_attribute);
316
1
    return true;
317
1
  }
318
0
  return false;
319
0
}
320
321
/// Check if IdxExpr is a valid parameter index for a function or
322
/// instance method D.  May output an error.
323
///
324
/// \returns true if IdxExpr is a valid index.
325
template <typename AttrInfo>
326
static bool checkFunctionOrMethodParameterIndex(
327
    Sema &S, const Decl *D, const AttrInfo &AI, unsigned AttrArgNum,
328
77.9k
    const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
329
77.9k
  assert(isFunctionOrMethodOrBlock(D));
330
331
  // In C++ the implicit 'this' function parameter also counts.
332
  // Parameters are counted from one.
333
77.9k
  bool HP = hasFunctionProto(D);
334
77.9k
  bool HasImplicitThisParam = isInstanceMethod(D);
335
77.9k
  bool IV = HP && 
isFunctionOrMethodVariadic(D)77.9k
;
336
77.9k
  unsigned NumParams =
337
77.9k
      (HP ? getFunctionOrMethodNumParams(D) : 
03
) + HasImplicitThisParam;
338
339
77.9k
  Optional<llvm::APSInt> IdxInt;
340
77.9k
  if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
341
77.9k
      !(IdxInt = IdxExpr->getIntegerConstantExpr(S.Context))) {
342
13
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
343
13
        << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
344
13
        << IdxExpr->getSourceRange();
345
13
    return false;
346
13
  }
347
348
77.8k
  unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX);
349
77.8k
  if (IdxSource < 1 || 
(77.8k
!IV77.8k
&&
IdxSource > NumParams77.1k
)) {
350
42
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
351
42
        << &AI << AttrArgNum << IdxExpr->getSourceRange();
352
42
    return false;
353
42
  }
354
77.8k
  if (HasImplicitThisParam && 
!CanIndexImplicitThis72
) {
355
69
    if (IdxSource == 1) {
356
5
      S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
357
5
          << &AI << IdxExpr->getSourceRange();
358
5
      return false;
359
5
    }
360
77.8k
  }
361
362
77.8k
  Idx = ParamIdx(IdxSource, D);
363
77.8k
  return true;
364
77.8k
}
SemaDeclAttr.cpp:bool checkFunctionOrMethodParameterIndex<clang::ParsedAttr>(clang::Sema&, clang::Decl const*, clang::ParsedAttr const&, unsigned int, clang::Expr const*, clang::ParamIdx&, bool)
Line
Count
Source
328
77.8k
    const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
329
77.8k
  assert(isFunctionOrMethodOrBlock(D));
330
331
  // In C++ the implicit 'this' function parameter also counts.
332
  // Parameters are counted from one.
333
77.8k
  bool HP = hasFunctionProto(D);
334
77.8k
  bool HasImplicitThisParam = isInstanceMethod(D);
335
77.8k
  bool IV = HP && 
isFunctionOrMethodVariadic(D)77.8k
;
336
77.8k
  unsigned NumParams =
337
77.8k
      (HP ? getFunctionOrMethodNumParams(D) : 
03
) + HasImplicitThisParam;
338
339
77.8k
  Optional<llvm::APSInt> IdxInt;
340
77.8k
  if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
341
77.8k
      !(IdxInt = IdxExpr->getIntegerConstantExpr(S.Context))) {
342
11
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
343
11
        << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
344
11
        << IdxExpr->getSourceRange();
345
11
    return false;
346
11
  }
347
348
77.8k
  unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX);
349
77.8k
  if (IdxSource < 1 || 
(77.8k
!IV77.8k
&&
IdxSource > NumParams77.1k
)) {
350
40
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
351
40
        << &AI << AttrArgNum << IdxExpr->getSourceRange();
352
40
    return false;
353
40
  }
354
77.8k
  if (HasImplicitThisParam && 
!CanIndexImplicitThis61
) {
355
58
    if (IdxSource == 1) {
356
4
      S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
357
4
          << &AI << IdxExpr->getSourceRange();
358
4
      return false;
359
4
    }
360
77.8k
  }
361
362
77.8k
  Idx = ParamIdx(IdxSource, D);
363
77.8k
  return true;
364
77.8k
}
SemaDeclAttr.cpp:bool checkFunctionOrMethodParameterIndex<clang::AllocAlignAttr>(clang::Sema&, clang::Decl const*, clang::AllocAlignAttr const&, unsigned int, clang::Expr const*, clang::ParamIdx&, bool)
Line
Count
Source
328
42
    const Expr *IdxExpr, ParamIdx &Idx, bool CanIndexImplicitThis = false) {
329
42
  assert(isFunctionOrMethodOrBlock(D));
330
331
  // In C++ the implicit 'this' function parameter also counts.
332
  // Parameters are counted from one.
333
42
  bool HP = hasFunctionProto(D);
334
42
  bool HasImplicitThisParam = isInstanceMethod(D);
335
42
  bool IV = HP && isFunctionOrMethodVariadic(D);
336
42
  unsigned NumParams =
337
42
      (HP ? getFunctionOrMethodNumParams(D) : 
00
) + HasImplicitThisParam;
338
339
42
  Optional<llvm::APSInt> IdxInt;
340
42
  if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
341
41
      !(IdxInt = IdxExpr->getIntegerConstantExpr(S.Context))) {
342
2
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_n_type)
343
2
        << &AI << AttrArgNum << AANT_ArgumentIntegerConstant
344
2
        << IdxExpr->getSourceRange();
345
2
    return false;
346
2
  }
347
348
40
  unsigned IdxSource = IdxInt->getLimitedValue(UINT_MAX);
349
40
  if (IdxSource < 1 || 
(39
!IV39
&&
IdxSource > NumParams39
)) {
350
2
    S.Diag(getAttrLoc(AI), diag::err_attribute_argument_out_of_bounds)
351
2
        << &AI << AttrArgNum << IdxExpr->getSourceRange();
352
2
    return false;
353
2
  }
354
38
  if (HasImplicitThisParam && 
!CanIndexImplicitThis11
) {
355
11
    if (IdxSource == 1) {
356
1
      S.Diag(getAttrLoc(AI), diag::err_attribute_invalid_implicit_this_argument)
357
1
          << &AI << IdxExpr->getSourceRange();
358
1
      return false;
359
1
    }
360
37
  }
361
362
37
  Idx = ParamIdx(IdxSource, D);
363
37
  return true;
364
37
}
365
366
/// Check if the argument \p ArgNum of \p Attr is a ASCII string literal.
367
/// If not emit an error and return false. If the argument is an identifier it
368
/// will emit an error with a fixit hint and treat it as if it was a string
369
/// literal.
370
bool Sema::checkStringLiteralArgumentAttr(const ParsedAttr &AL, unsigned ArgNum,
371
                                          StringRef &Str,
372
2.85M
                                          SourceLocation *ArgLocation) {
373
  // Look for identifiers. If we have one emit a hint to fix it to a literal.
374
2.85M
  if (AL.isArgIdent(ArgNum)) {
375
11
    IdentifierLoc *Loc = AL.getArgAsIdent(ArgNum);
376
11
    Diag(Loc->Loc, diag::err_attribute_argument_type)
377
11
        << AL << AANT_ArgumentString
378
11
        << FixItHint::CreateInsertion(Loc->Loc, "\"")
379
11
        << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\"");
380
11
    Str = Loc->Ident->getName();
381
11
    if (ArgLocation)
382
8
      *ArgLocation = Loc->Loc;
383
11
    return true;
384
11
  }
385
386
  // Now check for an actual string literal.
387
2.85M
  Expr *ArgExpr = AL.getArgAsExpr(ArgNum);
388
2.85M
  const auto *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts());
389
2.85M
  if (ArgLocation)
390
2.82M
    *ArgLocation = ArgExpr->getBeginLoc();
391
392
2.85M
  if (!Literal || 
!Literal->isAscii()2.85M
) {
393
26
    Diag(ArgExpr->getBeginLoc(), diag::err_attribute_argument_type)
394
26
        << AL << AANT_ArgumentString;
395
26
    return false;
396
26
  }
397
398
2.85M
  Str = Literal->getString();
399
2.85M
  return true;
400
2.85M
}
401
402
/// Applies the given attribute to the Decl without performing any
403
/// additional semantic checking.
404
template <typename AttrType>
405
static void handleSimpleAttribute(Sema &S, Decl *D,
406
124k
                                  const AttributeCommonInfo &CI) {
407
124k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
124k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::AVRInterruptAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
2
                                  const AttributeCommonInfo &CI) {
407
2
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
2
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::Mips16Attr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
8
                                  const AttributeCommonInfo &CI) {
407
8
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
8
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::MicroMipsAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
3
                                  const AttributeCommonInfo &CI) {
407
3
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
3
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::MipsLongCallAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
14
                                  const AttributeCommonInfo &CI) {
407
14
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
14
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::MipsShortCallAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
14
                                  const AttributeCommonInfo &CI) {
407
14
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
14
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::AVRSignalAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
2
                                  const AttributeCommonInfo &CI) {
407
2
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
2
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::SYCLKernelAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
6
                                  const AttributeCommonInfo &CI) {
407
6
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
6
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CUDADeviceAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
1.82k
                                  const AttributeCommonInfo &CI) {
407
1.82k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
1.82k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CUDAHostAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
811
                                  const AttributeCommonInfo &CI) {
407
811
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
811
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CUDADeviceBuiltinSurfaceTypeAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
10
                                  const AttributeCommonInfo &CI) {
407
10
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
10
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CUDADeviceBuiltinTextureTypeAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
8
                                  const AttributeCommonInfo &CI) {
407
8
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
8
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::ColdAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
3.77k
                                  const AttributeCommonInfo &CI) {
407
3.77k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
3.77k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::HotAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
6
                                  const AttributeCommonInfo &CI) {
407
6
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
6
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::AnyX86NoCfCheckAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
10
                                  const AttributeCommonInfo &CI) {
407
10
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
10
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NoThrowAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
33.3k
                                  const AttributeCommonInfo &CI) {
407
33.3k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
33.3k
}
Unexecuted instantiation: SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CFAuditedTransferAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CFUnknownTransferAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
1
                                  const AttributeCommonInfo &CI) {
407
1
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
1
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::OSReturnsRetainedOnZeroAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
3
                                  const AttributeCommonInfo &CI) {
407
3
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
3
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::OSReturnsRetainedOnNonZeroAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
2
                                  const AttributeCommonInfo &CI) {
407
2
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
2
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NSReturnsAutoreleasedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
6
                                  const AttributeCommonInfo &CI) {
407
6
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
6
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CFReturnsNotRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
681
                                  const AttributeCommonInfo &CI) {
407
681
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
681
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NSReturnsNotRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
113
                                  const AttributeCommonInfo &CI) {
407
113
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
113
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CFReturnsRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
42.8k
                                  const AttributeCommonInfo &CI) {
407
42.8k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
42.8k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NSReturnsRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
32.1k
                                  const AttributeCommonInfo &CI) {
407
32.1k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
32.1k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::OSReturnsRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
19
                                  const AttributeCommonInfo &CI) {
407
19
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
19
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::OSReturnsNotRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
11
                                  const AttributeCommonInfo &CI) {
407
11
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
11
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::SpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
25
                                  const AttributeCommonInfo &CI) {
407
25
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
25
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NoSpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
25
                                  const AttributeCommonInfo &CI) {
407
25
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
25
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::ObjCDirectAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
108
                                  const AttributeCommonInfo &CI) {
407
108
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
108
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::ObjCNonRuntimeProtocolAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
21
                                  const AttributeCommonInfo &CI) {
407
21
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
21
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::ObjCDirectMembersAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
16
                                  const AttributeCommonInfo &CI) {
407
16
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
16
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NotTailCalledAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
667
                                  const AttributeCommonInfo &CI) {
407
667
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
667
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::DisableTailCallsAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
421
                                  const AttributeCommonInfo &CI) {
407
421
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
421
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::SwiftBridgedTypedefAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
449
                                  const AttributeCommonInfo &CI) {
407
449
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
449
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::SwiftObjCMembersAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
1
                                  const AttributeCommonInfo &CI) {
407
1
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
1
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::SwiftPrivateAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
4.07k
                                  const AttributeCommonInfo &CI) {
407
4.07k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
4.07k
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::AlwaysDestroyAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
24
                                  const AttributeCommonInfo &CI) {
407
24
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
24
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NoDestroyAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
38
                                  const AttributeCommonInfo &CI) {
407
38
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
38
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::LoaderUninitializedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
45
                                  const AttributeCommonInfo &CI) {
407
45
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
45
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::ObjCExternallyRetainedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
51
                                  const AttributeCommonInfo &CI) {
407
51
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
51
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::MIGServerRoutineAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
26
                                  const AttributeCommonInfo &CI) {
407
26
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
26
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::MSAllocatorAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
5
                                  const AttributeCommonInfo &CI) {
407
5
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
5
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::OSConsumedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
9
                                  const AttributeCommonInfo &CI) {
407
9
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
9
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::NSConsumedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
493
                                  const AttributeCommonInfo &CI) {
407
493
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
493
}
SemaDeclAttr.cpp:void handleSimpleAttribute<clang::CFConsumedAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&)
Line
Count
Source
406
2.42k
                                  const AttributeCommonInfo &CI) {
407
2.42k
  D->addAttr(::new (S.Context) AttrType(S.Context, CI));
408
2.42k
}
409
410
template <typename... DiagnosticArgs>
411
static const Sema::SemaDiagnosticBuilder&
412
4
appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr) {
413
4
  return Bldr;
414
4
}
415
416
template <typename T, typename... DiagnosticArgs>
417
static const Sema::SemaDiagnosticBuilder&
418
appendDiagnostics(const Sema::SemaDiagnosticBuilder &Bldr, T &&ExtraArg,
419
12
                  DiagnosticArgs &&... ExtraArgs) {
420
12
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
12
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
12
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<clang::ParsedAttr const&, int, clang::SourceRange>(clang::Sema::SemaDiagnosticBuilder const&, clang::ParsedAttr const&, int&&, clang::SourceRange&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<int, clang::SourceRange>(clang::Sema::SemaDiagnosticBuilder const&, int&&, clang::SourceRange&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<clang::SourceRange>(clang::Sema::SemaDiagnosticBuilder const&, clang::SourceRange&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<clang::SourceRange, char const (&) [12], int>(clang::Sema::SemaDiagnosticBuilder const&, clang::SourceRange&&, char const (&) [12], int&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<char const (&) [12], int>(clang::Sema::SemaDiagnosticBuilder const&, char const (&) [12], int&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
SemaDeclAttr.cpp:clang::Sema::SemaDiagnosticBuilder const& appendDiagnostics<int>(clang::Sema::SemaDiagnosticBuilder const&, int&&)
Line
Count
Source
419
2
                  DiagnosticArgs &&... ExtraArgs) {
420
2
  return appendDiagnostics(Bldr << std::forward<T>(ExtraArg),
421
2
                           std::forward<DiagnosticArgs>(ExtraArgs)...);
422
2
}
423
424
/// Add an attribute {@code AttrType} to declaration {@code D}, provided that
425
/// {@code PassesCheck} is true.
426
/// Otherwise, emit diagnostic {@code DiagID}, passing in all parameters
427
/// specified in {@code ExtraArgs}.
428
template <typename AttrType, typename... DiagnosticArgs>
429
static void handleSimpleAttributeOrDiagnose(Sema &S, Decl *D,
430
                                            const AttributeCommonInfo &CI,
431
                                            bool PassesCheck, unsigned DiagID,
432
2.93k
                                            DiagnosticArgs &&... ExtraArgs) {
433
2.93k
  if (!PassesCheck) {
434
4
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
4
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
4
    return;
437
4
  }
438
2.93k
  handleSimpleAttribute<AttrType>(S, D, CI);
439
2.93k
}
SemaDeclAttr.cpp:void handleSimpleAttributeOrDiagnose<clang::OSReturnsRetainedOnZeroAttr, clang::ParsedAttr const&, int, clang::SourceRange>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, bool, unsigned int, clang::ParsedAttr const&, int&&, clang::SourceRange&&)
Line
Count
Source
432
4
                                            DiagnosticArgs &&... ExtraArgs) {
433
4
  if (!PassesCheck) {
434
1
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
1
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
1
    return;
437
1
  }
438
3
  handleSimpleAttribute<AttrType>(S, D, CI);
439
3
}
SemaDeclAttr.cpp:void handleSimpleAttributeOrDiagnose<clang::OSReturnsRetainedOnNonZeroAttr, clang::ParsedAttr const&, int, clang::SourceRange>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, bool, unsigned int, clang::ParsedAttr const&, int&&, clang::SourceRange&&)
Line
Count
Source
432
3
                                            DiagnosticArgs &&... ExtraArgs) {
433
3
  if (!PassesCheck) {
434
1
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
1
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
1
    return;
437
1
  }
438
2
  handleSimpleAttribute<AttrType>(S, D, CI);
439
2
}
SemaDeclAttr.cpp:void handleSimpleAttributeOrDiagnose<clang::OSConsumedAttr, clang::SourceRange, char const (&) [12], int>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, bool, unsigned int, clang::SourceRange&&, char const (&) [12], int&&)
Line
Count
Source
432
10
                                            DiagnosticArgs &&... ExtraArgs) {
433
10
  if (!PassesCheck) {
434
1
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
1
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
1
    return;
437
1
  }
438
9
  handleSimpleAttribute<AttrType>(S, D, CI);
439
9
}
SemaDeclAttr.cpp:void handleSimpleAttributeOrDiagnose<clang::NSConsumedAttr, clang::SourceRange, char const (&) [12], int>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, bool, unsigned int, clang::SourceRange&&, char const (&) [12], int&&)
Line
Count
Source
432
494
                                            DiagnosticArgs &&... ExtraArgs) {
433
494
  if (!PassesCheck) {
434
1
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
1
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
1
    return;
437
1
  }
438
493
  handleSimpleAttribute<AttrType>(S, D, CI);
439
493
}
SemaDeclAttr.cpp:void handleSimpleAttributeOrDiagnose<clang::CFConsumedAttr, clang::SourceRange, char const (&) [12], int>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, bool, unsigned int, clang::SourceRange&&, char const (&) [12], int&&)
Line
Count
Source
432
2.42k
                                            DiagnosticArgs &&... ExtraArgs) {
433
2.42k
  if (!PassesCheck) {
434
0
    Sema::SemaDiagnosticBuilder DB = S.Diag(D->getBeginLoc(), DiagID);
435
0
    appendDiagnostics(DB, std::forward<DiagnosticArgs>(ExtraArgs)...);
436
0
    return;
437
0
  }
438
2.42k
  handleSimpleAttribute<AttrType>(S, D, CI);
439
2.42k
}
440
441
template <typename AttrType>
442
static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
443
7.67k
                                                const ParsedAttr &AL) {
444
7.67k
  handleSimpleAttribute<AttrType>(S, D, AL);
445
7.67k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::Mips16Attr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
8
                                                const ParsedAttr &AL) {
444
8
  handleSimpleAttribute<AttrType>(S, D, AL);
445
8
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MicroMipsAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
3
                                                const ParsedAttr &AL) {
444
3
  handleSimpleAttribute<AttrType>(S, D, AL);
445
3
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MipsLongCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
14
                                                const ParsedAttr &AL) {
444
14
  handleSimpleAttribute<AttrType>(S, D, AL);
445
14
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MipsShortCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
14
                                                const ParsedAttr &AL) {
444
14
  handleSimpleAttribute<AttrType>(S, D, AL);
445
14
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
1.82k
                                                const ParsedAttr &AL) {
444
1.82k
  handleSimpleAttribute<AttrType>(S, D, AL);
445
1.82k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDAHostAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
811
                                                const ParsedAttr &AL) {
444
811
  handleSimpleAttribute<AttrType>(S, D, AL);
445
811
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceBuiltinSurfaceTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
10
                                                const ParsedAttr &AL) {
444
10
  handleSimpleAttribute<AttrType>(S, D, AL);
445
10
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceBuiltinTextureTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
8
                                                const ParsedAttr &AL) {
444
8
  handleSimpleAttribute<AttrType>(S, D, AL);
445
8
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::ColdAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
3.77k
                                                const ParsedAttr &AL) {
444
3.77k
  handleSimpleAttribute<AttrType>(S, D, AL);
445
3.77k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::HotAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
6
                                                const ParsedAttr &AL) {
444
6
  handleSimpleAttribute<AttrType>(S, D, AL);
445
6
}
Unexecuted instantiation: SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CFAuditedTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CFUnknownTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
1
                                                const ParsedAttr &AL) {
444
1
  handleSimpleAttribute<AttrType>(S, D, AL);
445
1
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::SpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
25
                                                const ParsedAttr &AL) {
444
25
  handleSimpleAttribute<AttrType>(S, D, AL);
445
25
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NoSpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
25
                                                const ParsedAttr &AL) {
444
25
  handleSimpleAttribute<AttrType>(S, D, AL);
445
25
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NotTailCalledAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
667
                                                const ParsedAttr &AL) {
444
667
  handleSimpleAttribute<AttrType>(S, D, AL);
445
667
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::DisableTailCallsAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
421
                                                const ParsedAttr &AL) {
444
421
  handleSimpleAttribute<AttrType>(S, D, AL);
445
421
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::AlwaysDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
24
                                                const ParsedAttr &AL) {
444
24
  handleSimpleAttribute<AttrType>(S, D, AL);
445
24
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NoDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
443
38
                                                const ParsedAttr &AL) {
444
38
  handleSimpleAttribute<AttrType>(S, D, AL);
445
38
}
446
447
/// Applies the given attribute to the Decl so long as the Decl doesn't
448
/// already have one of the given incompatible attributes.
449
template <typename AttrType, typename IncompatibleAttrType,
450
          typename... IncompatibleAttrTypes>
451
static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D,
452
7.71k
                                                const ParsedAttr &AL) {
453
7.71k
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
29
    return;
455
7.68k
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
7.68k
                                                                          AL);
457
7.68k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::Mips16Attr, clang::MicroMipsAttr, clang::MipsInterruptAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
11
                                                const ParsedAttr &AL) {
453
11
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
1
    return;
455
10
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
10
                                                                          AL);
457
10
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::Mips16Attr, clang::MipsInterruptAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
10
                                                const ParsedAttr &AL) {
453
10
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
8
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
8
                                                                          AL);
457
8
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MicroMipsAttr, clang::Mips16Attr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
5
                                                const ParsedAttr &AL) {
453
5
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
3
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
3
                                                                          AL);
457
3
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MipsLongCallAttr, clang::MipsShortCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
16
                                                const ParsedAttr &AL) {
453
16
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
14
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
14
                                                                          AL);
457
14
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::MipsShortCallAttr, clang::MipsLongCallAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
16
                                                const ParsedAttr &AL) {
453
16
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
14
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
14
                                                                          AL);
457
14
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceAttr, clang::CUDAGlobalAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
1.82k
                                                const ParsedAttr &AL) {
453
1.82k
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
1.82k
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
1.82k
                                                                          AL);
457
1.82k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDAHostAttr, clang::CUDAGlobalAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
813
                                                const ParsedAttr &AL) {
453
813
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
811
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
811
                                                                          AL);
457
811
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceBuiltinSurfaceTypeAttr, clang::CUDADeviceBuiltinTextureTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
10
                                                const ParsedAttr &AL) {
453
10
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
0
    return;
455
10
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
10
                                                                          AL);
457
10
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CUDADeviceBuiltinTextureTypeAttr, clang::CUDADeviceBuiltinSurfaceTypeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
8
                                                const ParsedAttr &AL) {
453
8
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
0
    return;
455
8
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
8
                                                                          AL);
457
8
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::ColdAttr, clang::HotAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
3.77k
                                                const ParsedAttr &AL) {
453
3.77k
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
1
    return;
455
3.77k
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
3.77k
                                                                          AL);
457
3.77k
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::HotAttr, clang::ColdAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
7
                                                const ParsedAttr &AL) {
453
7
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
1
    return;
455
6
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
6
                                                                          AL);
457
6
}
Unexecuted instantiation: SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CFAuditedTransferAttr, clang::CFUnknownTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::CFUnknownTransferAttr, clang::CFAuditedTransferAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
1
                                                const ParsedAttr &AL) {
453
1
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
0
    return;
455
1
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
1
                                                                          AL);
457
1
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::SpeculativeLoadHardeningAttr, clang::NoSpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
27
                                                const ParsedAttr &AL) {
453
27
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
25
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
25
                                                                          AL);
457
25
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NoSpeculativeLoadHardeningAttr, clang::SpeculativeLoadHardeningAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
27
                                                const ParsedAttr &AL) {
453
27
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
2
    return;
455
25
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
25
                                                                          AL);
457
25
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NotTailCalledAttr, clang::AlwaysInlineAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
668
                                                const ParsedAttr &AL) {
453
668
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
1
    return;
455
667
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
667
                                                                          AL);
457
667
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::DisableTailCallsAttr, clang::NakedAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
422
                                                const ParsedAttr &AL) {
453
422
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
1
    return;
455
421
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
421
                                                                          AL);
457
421
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::AlwaysDestroyAttr, clang::NoDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
28
                                                const ParsedAttr &AL) {
453
28
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
4
    return;
455
24
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
24
                                                                          AL);
457
24
}
SemaDeclAttr.cpp:void handleSimpleAttributeWithExclusions<clang::NoDestroyAttr, clang::AlwaysDestroyAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
452
42
                                                const ParsedAttr &AL) {
453
42
  if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, AL))
454
4
    return;
455
38
  handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D,
456
38
                                                                          AL);
457
38
}
458
459
/// Check if the passed-in expression is of type int or bool.
460
176
static bool isIntOrBool(Expr *Exp) {
461
176
  QualType QT = Exp->getType();
462
176
  return QT->isBooleanType() || 
QT->isIntegerType()120
;
463
176
}
464
465
466
// Check to see if the type is a smart pointer of some kind.  We assume
467
// it's a smart pointer if it defines both operator-> and operator*.
468
2.75k
static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) {
469
2.75k
  auto IsOverloadedOperatorPresent = [&S](const RecordDecl *Record,
470
5.59k
                                          OverloadedOperatorKind Op) {
471
5.59k
    DeclContextLookupResult Result =
472
5.59k
        Record->lookup(S.Context.DeclarationNames.getCXXOperatorName(Op));
473
5.59k
    return !Result.empty();
474
5.59k
  };
475
476
2.75k
  const RecordDecl *Record = RT->getDecl();
477
2.75k
  bool foundStarOperator = IsOverloadedOperatorPresent(Record, OO_Star);
478
2.75k
  bool foundArrowOperator = IsOverloadedOperatorPresent(Record, OO_Arrow);
479
2.75k
  if (foundStarOperator && 
foundArrowOperator46
)
480
46
    return true;
481
482
2.70k
  const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Record);
483
2.70k
  if (!CXXRecord)
484
21
    return false;
485
486
2.68k
  for (auto BaseSpecifier : CXXRecord->bases()) {
487
44
    if (!foundStarOperator)
488
44
      foundStarOperator = IsOverloadedOperatorPresent(
489
44
          BaseSpecifier.getType()->getAsRecordDecl(), OO_Star);
490
44
    if (!foundArrowOperator)
491
44
      foundArrowOperator = IsOverloadedOperatorPresent(
492
44
          BaseSpecifier.getType()->getAsRecordDecl(), OO_Arrow);
493
44
  }
494
495
2.68k
  if (foundStarOperator && 
foundArrowOperator16
)
496
16
    return true;
497
498
2.67k
  return false;
499
2.67k
}
500
501
/// Check if passed in Decl is a pointer type.
502
/// Note that this function may produce an error message.
503
/// \return true if the Decl is a pointer type; false otherwise
504
static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D,
505
153
                                       const ParsedAttr &AL) {
506
153
  const auto *VD = cast<ValueDecl>(D);
507
153
  QualType QT = VD->getType();
508
153
  if (QT->isAnyPointerType())
509
114
    return true;
510
511
39
  if (const auto *RT = QT->getAs<RecordType>()) {
512
    // If it's an incomplete type, it could be a smart pointer; skip it.
513
    // (We don't want to force template instantiation if we can avoid it,
514
    // since that would alter the order in which templates are instantiated.)
515
22
    if (RT->isIncompleteType())
516
3
      return true;
517
518
19
    if (threadSafetyCheckIsSmartPointer(S, RT))
519
19
      return true;
520
17
  }
521
522
17
  S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_pointer) << AL << QT;
523
17
  return false;
524
17
}
525
526
/// Checks that the passed in QualType either is of RecordType or points
527
/// to RecordType. Returns the relevant RecordType, null if it does not exit.
528
5.49k
static const RecordType *getRecordType(QualType QT) {
529
5.49k
  if (const auto *RT = QT->getAs<RecordType>())
530
4.47k
    return RT;
531
532
  // Now check if we point to record type.
533
1.02k
  if (const auto *PT = QT->getAs<PointerType>())
534
796
    return PT->getPointeeType()->getAs<RecordType>();
535
536
229
  return nullptr;
537
229
}
538
539
template <typename AttrType>
540
3.22k
static bool checkRecordDeclForAttr(const RecordDecl *RD) {
541
  // Check if the record itself has the attribute.
542
3.22k
  if (RD->hasAttr<AttrType>())
543
2.90k
    return true;
544
545
  // Else check if any base classes have the attribute.
546
323
  if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
547
321
    CXXBasePaths BPaths(false, false);
548
321
    if (CRD->lookupInBases(
549
49
            [](const CXXBaseSpecifier *BS, CXXBasePath &) {
550
49
              const auto &Ty = *BS->getType();
551
              // If it's type-dependent, we assume it could have the attribute.
552
49
              if (Ty.isDependentType())
553
3
                return true;
554
46
              return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
555
46
            },
SemaDeclAttr.cpp:bool checkRecordDeclForAttr<clang::CapabilityAttr>(clang::RecordDecl const*)::'lambda'(clang::CXXBaseSpecifier const*, clang::CXXBasePath&)::operator()(clang::CXXBaseSpecifier const*, clang::CXXBasePath&) const
Line
Count
Source
549
43
            [](const CXXBaseSpecifier *BS, CXXBasePath &) {
550
43
              const auto &Ty = *BS->getType();
551
              // If it's type-dependent, we assume it could have the attribute.
552
43
              if (Ty.isDependentType())
553
3
                return true;
554
40
              return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
555
40
            },
SemaDeclAttr.cpp:bool checkRecordDeclForAttr<clang::ScopedLockableAttr>(clang::RecordDecl const*)::'lambda'(clang::CXXBaseSpecifier const*, clang::CXXBasePath&)::operator()(clang::CXXBaseSpecifier const*, clang::CXXBasePath&) const
Line
Count
Source
549
6
            [](const CXXBaseSpecifier *BS, CXXBasePath &) {
550
6
              const auto &Ty = *BS->getType();
551
              // If it's type-dependent, we assume it could have the attribute.
552
6
              if (Ty.isDependentType())
553
0
                return true;
554
6
              return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
555
6
            },
556
321
            BPaths, true))
557
22
      return true;
558
301
  }
559
301
  return false;
560
301
}
SemaDeclAttr.cpp:bool checkRecordDeclForAttr<clang::CapabilityAttr>(clang::RecordDecl const*)
Line
Count
Source
540
3.06k
static bool checkRecordDeclForAttr(const RecordDecl *RD) {
541
  // Check if the record itself has the attribute.
542
3.06k
  if (RD->hasAttr<AttrType>())
543
2.76k
    return true;
544
545
  // Else check if any base classes have the attribute.
546
295
  if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
547
293
    CXXBasePaths BPaths(false, false);
548
293
    if (CRD->lookupInBases(
549
293
            [](const CXXBaseSpecifier *BS, CXXBasePath &) {
550
293
              const auto &Ty = *BS->getType();
551
              // If it's type-dependent, we assume it could have the attribute.
552
293
              if (Ty.isDependentType())
553
293
                return true;
554
293
              return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
555
293
            },
556
293
            BPaths, true))
557
19
      return true;
558
276
  }
559
276
  return false;
560
276
}
SemaDeclAttr.cpp:bool checkRecordDeclForAttr<clang::ScopedLockableAttr>(clang::RecordDecl const*)
Line
Count
Source
540
165
static bool checkRecordDeclForAttr(const RecordDecl *RD) {
541
  // Check if the record itself has the attribute.
542
165
  if (RD->hasAttr<AttrType>())
543
137
    return true;
544
545
  // Else check if any base classes have the attribute.
546
28
  if (const auto *CRD = dyn_cast<CXXRecordDecl>(RD)) {
547
28
    CXXBasePaths BPaths(false, false);
548
28
    if (CRD->lookupInBases(
549
28
            [](const CXXBaseSpecifier *BS, CXXBasePath &) {
550
28
              const auto &Ty = *BS->getType();
551
              // If it's type-dependent, we assume it could have the attribute.
552
28
              if (Ty.isDependentType())
553
28
                return true;
554
28
              return Ty.castAs<RecordType>()->getDecl()->hasAttr<AttrType>();
555
28
            },
556
28
            BPaths, true))
557
3
      return true;
558
25
  }
559
25
  return false;
560
25
}
561
562
2.89k
static bool checkRecordTypeForCapability(Sema &S, QualType Ty) {
563
2.89k
  const RecordType *RT = getRecordType(Ty);
564
565
2.89k
  if (!RT)
566
160
    return false;
567
568
  // Don't check for the capability if the class hasn't been defined yet.
569
2.73k
  if (RT->isIncompleteType())
570
0
    return true;
571
572
  // Allow smart pointers to be used as capability objects.
573
  // FIXME -- Check the type that the smart pointer points to.
574
2.73k
  if (threadSafetyCheckIsSmartPointer(S, RT))
575
43
    return true;
576
577
2.69k
  return checkRecordDeclForAttr<CapabilityAttr>(RT->getDecl());
578
2.69k
}
579
580
2.94k
static bool checkTypedefTypeForCapability(QualType Ty) {
581
2.94k
  const auto *TD = Ty->getAs<TypedefType>();
582
2.94k
  if (!TD)
583
2.89k
    return false;
584
585
48
  TypedefNameDecl *TN = TD->getDecl();
586
48
  if (!TN)
587
0
    return false;
588
589
48
  return TN->hasAttr<CapabilityAttr>();
590
48
}
591
592
2.94k
static bool typeHasCapability(Sema &S, QualType Ty) {
593
2.94k
  if (checkTypedefTypeForCapability(Ty))
594
48
    return true;
595
596
2.89k
  if (checkRecordTypeForCapability(S, Ty))
597
2.62k
    return true;
598
599
271
  return false;
600
271
}
601
602
203
static bool isCapabilityExpr(Sema &S, const Expr *Ex) {
603
  // Capability expressions are simple expressions involving the boolean logic
604
  // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once
605
  // a DeclRefExpr is found, its type should be checked to determine whether it
606
  // is a capability or not.
607
608
203
  if (const auto *E = dyn_cast<CastExpr>(Ex))
609
26
    return isCapabilityExpr(S, E->getSubExpr());
610
177
  else if (const auto *E = dyn_cast<ParenExpr>(Ex))
611
1
    return isCapabilityExpr(S, E->getSubExpr());
612
176
  else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) {
613
26
    if (E->getOpcode() == UO_LNot || 
E->getOpcode() == UO_AddrOf12
||
614
3
        E->getOpcode() == UO_Deref)
615
26
      return isCapabilityExpr(S, E->getSubExpr());
616
0
    return false;
617
150
  } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) {
618
5
    if (E->getOpcode() == BO_LAnd || 
E->getOpcode() == BO_LOr2
)
619
5
      return isCapabilityExpr(S, E->getLHS()) &&
620
5
             isCapabilityExpr(S, E->getRHS());
621
0
    return false;
622
0
  }
623
624
145
  return typeHasCapability(S, Ex->getType());
625
145
}
626
627
/// Checks that all attribute arguments, starting from Sidx, resolve to
628
/// a capability object.
629
/// \param Sidx The attribute argument index to start checking with.
630
/// \param ParamIdxOk Whether an argument can be indexing into a function
631
/// parameter list.
632
static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
633
                                           const ParsedAttr &AL,
634
                                           SmallVectorImpl<Expr *> &Args,
635
                                           unsigned Sidx = 0,
636
2.97k
                                           bool ParamIdxOk = false) {
637
2.97k
  if (Sidx == AL.getNumArgs()) {
638
    // If we don't have any capability arguments, the attribute implicitly
639
    // refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
640
    // a non-static method, and that the class is a (scoped) capability.
641
416
    const auto *MD = dyn_cast<const CXXMethodDecl>(D);
642
416
    if (MD && 
!MD->isStatic()371
) {
643
371
      const CXXRecordDecl *RD = MD->getParent();
644
      // FIXME -- need to check this again on template instantiation
645
371
      if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
646
165
          !checkRecordDeclForAttr<ScopedLockableAttr>(RD))
647
25
        S.Diag(AL.getLoc(),
648
25
               diag::warn_thread_attribute_not_on_capability_member)
649
25
            << AL << MD->getParent();
650
45
    } else {
651
45
      S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
652
45
          << AL;
653
45
    }
654
416
  }
655
656
5.73k
  for (unsigned Idx = Sidx; Idx < AL.getNumArgs(); 
++Idx2.75k
) {
657
2.75k
    Expr *ArgExp = AL.getArgAsExpr(Idx);
658
659
2.75k
    if (ArgExp->isTypeDependent()) {
660
      // FIXME -- need to check this again on template instantiation
661
54
      Args.push_back(ArgExp);
662
54
      continue;
663
54
    }
664
665
2.70k
    if (const auto *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
666
104
      if (StrLit->getLength() == 0 ||
667
70
          (StrLit->isAscii() && 
StrLit->getString() == StringRef("*")67
)) {
668
        // Pass empty strings to the analyzer without warnings.
669
        // Treat "*" as the universal lock.
670
62
        Args.push_back(ArgExp);
671
62
        continue;
672
62
      }
673
674
      // We allow constant strings to be used as a placeholder for expressions
675
      // that are not valid C++ syntax, but warn that they are ignored.
676
42
      S.Diag(AL.getLoc(), diag::warn_thread_attribute_ignored) << AL;
677
42
      Args.push_back(ArgExp);
678
42
      continue;
679
42
    }
680
681
2.60k
    QualType ArgTy = ArgExp->getType();
682
683
    // A pointer to member expression of the form  &MyClass::mu is treated
684
    // specially -- we need to look at the type of the member.
685
2.60k
    if (const auto *UOp = dyn_cast<UnaryOperator>(ArgExp))
686
128
      if (UOp->getOpcode() == UO_AddrOf)
687
76
        if (const auto *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
688
76
          if (DRE->getDecl()->isCXXInstanceMember())
689
37
            ArgTy = DRE->getDecl()->getType();
690
691
    // First see if we can just cast to record type, or pointer to record type.
692
2.60k
    const RecordType *RT = getRecordType(ArgTy);
693
694
    // Now check if we index into a record type function param.
695
2.60k
    if(!RT && 
ParamIdxOk186
) {
696
89
      const auto *FD = dyn_cast<FunctionDecl>(D);
697
89
      const auto *IL = dyn_cast<IntegerLiteral>(ArgExp);
698
89
      if(FD && IL) {
699
63
        unsigned int NumParams = FD->getNumParams();
700
63
        llvm::APInt ArgValue = IL->getValue();
701
63
        uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
702
63
        uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
703
63
        if (!ArgValue.isStrictlyPositive() || 
ParamIdxFromOne > NumParams36
) {
704
36
          S.Diag(AL.getLoc(),
705
36
                 diag::err_attribute_argument_out_of_bounds_extra_info)
706
36
              << AL << Idx + 1 << NumParams;
707
36
          continue;
708
36
        }
709
27
        ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
710
27
      }
711
89
    }
712
713
    // If the type does not have a capability, see if the components of the
714
    // expression have capabilities. This allows for writing C code where the
715
    // capability may be on the type, and the expression is a capability
716
    // boolean logic expression. Eg) requires_capability(A || B && !C)
717
2.56k
    if (!typeHasCapability(S, ArgTy) && 
!isCapabilityExpr(S, ArgExp)140
)
718
125
      S.Diag(AL.getLoc(), diag::warn_thread_attribute_argument_not_lockable)
719
125
          << AL << ArgTy;
720
721
2.56k
    Args.push_back(ArgExp);
722
2.56k
  }
723
2.97k
}
724
725
//===----------------------------------------------------------------------===//
726
// Attribute Implementations
727
//===----------------------------------------------------------------------===//
728
729
21
static void handlePtGuardedVarAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
730
21
  if (!threadSafetyCheckIsPointer(S, D, AL))
731
10
    return;
732
733
11
  D->addAttr(::new (S.Context) PtGuardedVarAttr(S.Context, AL));
734
11
}
735
736
static bool checkGuardedByAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
737
682
                                     Expr *&Arg) {
738
682
  SmallVector<Expr *, 1> Args;
739
  // check that all arguments are lockable objects
740
682
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
741
682
  unsigned Size = Args.size();
742
682
  if (Size != 1)
743
0
    return false;
744
745
682
  Arg = Args[0];
746
747
682
  return true;
748
682
}
749
750
550
static void handleGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
751
550
  Expr *Arg = nullptr;
752
550
  if (!checkGuardedByAttrCommon(S, D, AL, Arg))
753
0
    return;
754
755
550
  D->addAttr(::new (S.Context) GuardedByAttr(S.Context, AL, Arg));
756
550
}
757
758
132
static void handlePtGuardedByAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
759
132
  Expr *Arg = nullptr;
760
132
  if (!checkGuardedByAttrCommon(S, D, AL, Arg))
761
0
    return;
762
763
132
  if (!threadSafetyCheckIsPointer(S, D, AL))
764
7
    return;
765
766
125
  D->addAttr(::new (S.Context) PtGuardedByAttr(S.Context, AL, Arg));
767
125
}
768
769
static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
770
246
                                        SmallVectorImpl<Expr *> &Args) {
771
246
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
772
12
    return false;
773
774
  // Check that this attribute only applies to lockable types.
775
234
  QualType QT = cast<ValueDecl>(D)->getType();
776
234
  if (!QT->isDependentType() && !typeHasCapability(S, QT)) {
777
6
    S.Diag(AL.getLoc(), diag::warn_thread_attribute_decl_not_lockable) << AL;
778
6
    return false;
779
6
  }
780
781
  // Check that all arguments are lockable objects.
782
228
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
783
228
  if (Args.empty())
784
0
    return false;
785
786
228
  return true;
787
228
}
788
789
143
static void handleAcquiredAfterAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
790
143
  SmallVector<Expr *, 1> Args;
791
143
  if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
792
9
    return;
793
794
134
  Expr **StartArg = &Args[0];
795
134
  D->addAttr(::new (S.Context)
796
134
                 AcquiredAfterAttr(S.Context, AL, StartArg, Args.size()));
797
134
}
798
799
103
static void handleAcquiredBeforeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
800
103
  SmallVector<Expr *, 1> Args;
801
103
  if (!checkAcquireOrderAttrCommon(S, D, AL, Args))
802
9
    return;
803
804
94
  Expr **StartArg = &Args[0];
805
94
  D->addAttr(::new (S.Context)
806
94
                 AcquiredBeforeAttr(S.Context, AL, StartArg, Args.size()));
807
94
}
808
809
static bool checkLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
810
549
                                   SmallVectorImpl<Expr *> &Args) {
811
  // zero or more arguments ok
812
  // check that all arguments are lockable objects
813
549
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 0, /*ParamIdxOk=*/true);
814
815
549
  return true;
816
549
}
817
818
13
static void handleAssertSharedLockAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
819
13
  SmallVector<Expr *, 1> Args;
820
13
  if (!checkLockFunAttrCommon(S, D, AL, Args))
821
0
    return;
822
823
13
  unsigned Size = Args.size();
824
7
  Expr **StartArg = Size == 0 ? nullptr : 
&Args[0]6
;
825
13
  D->addAttr(::new (S.Context)
826
13
                 AssertSharedLockAttr(S.Context, AL, StartArg, Size));
827
13
}
828
829
static void handleAssertExclusiveLockAttr(Sema &S, Decl *D,
830
15
                                          const ParsedAttr &AL) {
831
15
  SmallVector<Expr *, 1> Args;
832
15
  if (!checkLockFunAttrCommon(S, D, AL, Args))
833
0
    return;
834
835
15
  unsigned Size = Args.size();
836
8
  Expr **StartArg = Size == 0 ? 
nullptr7
: &Args[0];
837
15
  D->addAttr(::new (S.Context)
838
15
                 AssertExclusiveLockAttr(S.Context, AL, StartArg, Size));
839
15
}
840
841
/// Checks to be sure that the given parameter number is in bounds, and
842
/// is an integral type. Will emit appropriate diagnostics if this returns
843
/// false.
844
///
845
/// AttrArgNo is used to actually retrieve the argument, so it's base-0.
846
template <typename AttrInfo>
847
static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD,
848
6.12k
                                    const AttrInfo &AI, unsigned AttrArgNo) {
849
6.12k
  assert(AI.isArgExpr(AttrArgNo) && "Expected expression argument");
850
6.12k
  Expr *AttrArg = AI.getArgAsExpr(AttrArgNo);
851
6.12k
  ParamIdx Idx;
852
6.12k
  if (!checkFunctionOrMethodParameterIndex(S, FD, AI, AttrArgNo + 1, AttrArg,
853
6.12k
                                           Idx))
854
6
    return false;
855
856
6.11k
  const ParmVarDecl *Param = FD->getParamDecl(Idx.getASTIndex());
857
6.11k
  if (!Param->getType()->isIntegerType() && 
!Param->getType()->isCharType()1
) {
858
1
    SourceLocation SrcLoc = AttrArg->getBeginLoc();
859
1
    S.Diag(SrcLoc, diag::err_attribute_integers_only)
860
1
        << AI << Param->getSourceRange();
861
1
    return false;
862
1
  }
863
6.11k
  return true;
864
6.11k
}
865
866
5.15k
static void handleAllocSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
867
5.15k
  if (!checkAttributeAtLeastNumArgs(S, AL, 1) ||
868
5.15k
      !checkAttributeAtMostNumArgs(S, AL, 2))
869
0
    return;
870
871
5.15k
  const auto *FD = cast<FunctionDecl>(D);
872
5.15k
  if (!FD->getReturnType()->isPointerType()) {
873
1
    S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only) << AL;
874
1
    return;
875
1
  }
876
877
5.15k
  const Expr *SizeExpr = AL.getArgAsExpr(0);
878
5.15k
  int SizeArgNoVal;
879
  // Parameter indices are 1-indexed, hence Index=1
880
5.15k
  if (!checkPositiveIntArgument(S, AL, SizeExpr, SizeArgNoVal, /*Idx=*/1))
881
2
    return;
882
5.14k
  if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/0))
883
5
    return;
884
5.14k
  ParamIdx SizeArgNo(SizeArgNoVal, D);
885
886
5.14k
  ParamIdx NumberArgNo;
887
5.14k
  if (AL.getNumArgs() == 2) {
888
974
    const Expr *NumberExpr = AL.getArgAsExpr(1);
889
974
    int Val;
890
    // Parameter indices are 1-based, hence Index=2
891
974
    if (!checkPositiveIntArgument(S, AL, NumberExpr, Val, /*Idx=*/2))
892
1
      return;
893
973
    if (!checkParamIsIntegerType(S, FD, AL, /*AttrArgNo=*/1))
894
2
      return;
895
971
    NumberArgNo = ParamIdx(Val, D);
896
971
  }
897
898
5.14k
  D->addAttr(::new (S.Context)
899
5.14k
                 AllocSizeAttr(S.Context, AL, SizeArgNo, NumberArgNo));
900
5.14k
}
901
902
static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, const ParsedAttr &AL,
903
176
                                      SmallVectorImpl<Expr *> &Args) {
904
176
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
905
0
    return false;
906
907
176
  if (!isIntOrBool(AL.getArgAsExpr(0))) {
908
18
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
909
18
        << AL << 1 << AANT_ArgumentIntOrBool;
910
18
    return false;
911
18
  }
912
913
  // check that all arguments are lockable objects
914
158
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args, 1);
915
916
158
  return true;
917
158
}
918
919
static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D,
920
70
                                            const ParsedAttr &AL) {
921
70
  SmallVector<Expr*, 2> Args;
922
70
  if (!checkTryLockFunAttrCommon(S, D, AL, Args))
923
9
    return;
924
925
61
  D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(
926
61
      S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
927
61
}
928
929
static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D,
930
78
                                               const ParsedAttr &AL) {
931
78
  SmallVector<Expr*, 2> Args;
932
78
  if (!checkTryLockFunAttrCommon(S, D, AL, Args))
933
9
    return;
934
935
69
  D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(
936
69
      S.Context, AL, AL.getArgAsExpr(0), Args.data(), Args.size()));
937
69
}
938
939
80
static void handleLockReturnedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
940
  // check that the argument is lockable object
941
80
  SmallVector<Expr*, 1> Args;
942
80
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
943
80
  unsigned Size = Args.size();
944
80
  if (Size == 0)
945
0
    return;
946
947
80
  D->addAttr(::new (S.Context) LockReturnedAttr(S.Context, AL, Args[0]));
948
80
}
949
950
142
static void handleLocksExcludedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
951
142
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
952
3
    return;
953
954
  // check that all arguments are lockable objects
955
139
  SmallVector<Expr*, 1> Args;
956
139
  checkAttrArgsAreCapabilityObjs(S, D, AL, Args);
957
139
  unsigned Size = Args.size();
958
139
  if (Size == 0)
959
0
    return;
960
139
  Expr **StartArg = &Args[0];
961
962
139
  D->addAttr(::new (S.Context)
963
139
                 LocksExcludedAttr(S.Context, AL, StartArg, Size));
964
139
}
965
966
static bool checkFunctionConditionAttr(Sema &S, Decl *D, const ParsedAttr &AL,
967
17.7k
                                       Expr *&Cond, StringRef &Msg) {
968
17.7k
  Cond = AL.getArgAsExpr(0);
969
17.7k
  if (!Cond->isTypeDependent()) {
970
17.6k
    ExprResult Converted = S.PerformContextuallyConvertToBool(Cond);
971
17.6k
    if (Converted.isInvalid())
972
0
      return false;
973
17.6k
    Cond = Converted.get();
974
17.6k
  }
975
976
17.7k
  if (!S.checkStringLiteralArgumentAttr(AL, 1, Msg))
977
2
    return false;
978
979
17.7k
  if (Msg.empty())
980
8.88k
    Msg = "<no message provided>";
981
982
17.7k
  SmallVector<PartialDiagnosticAt, 8> Diags;
983
17.7k
  if (isa<FunctionDecl>(D) && 
!Cond->isValueDependent()17.7k
&&
984
17.6k
      !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D),
985
3
                                                Diags)) {
986
3
    S.Diag(AL.getLoc(), diag::err_attr_cond_never_constant_expr) << AL;
987
3
    for (const PartialDiagnosticAt &PDiag : Diags)
988
3
      S.Diag(PDiag.first, PDiag.second);
989
3
    return false;
990
3
  }
991
17.7k
  return true;
992
17.7k
}
993
994
10.4k
static void handleEnableIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
995
10.4k
  S.Diag(AL.getLoc(), diag::ext_clang_enable_if);
996
997
10.4k
  Expr *Cond;
998
10.4k
  StringRef Msg;
999
10.4k
  if (checkFunctionConditionAttr(S, D, AL, Cond, Msg))
1000
10.4k
    D->addAttr(::new (S.Context) EnableIfAttr(S.Context, AL, Cond, Msg));
1001
10.4k
}
1002
1003
namespace {
1004
/// Determines if a given Expr references any of the given function's
1005
/// ParmVarDecls, or the function's implicit `this` parameter (if applicable).
1006
class ArgumentDependenceChecker
1007
    : public RecursiveASTVisitor<ArgumentDependenceChecker> {
1008
#ifndef NDEBUG
1009
  const CXXRecordDecl *ClassType;
1010
#endif
1011
  llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms;
1012
  bool Result;
1013
1014
public:
1015
7.21k
  ArgumentDependenceChecker(const FunctionDecl *FD) {
1016
7.21k
#ifndef NDEBUG
1017
7.21k
    if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1018
3.18k
      ClassType = MD->getParent();
1019
4.02k
    else
1020
4.02k
      ClassType = nullptr;
1021
7.21k
#endif
1022
7.21k
    Parms.insert(FD->param_begin(), FD->param_end());
1023
7.21k
  }
1024
1025
7.21k
  bool referencesArgs(Expr *E) {
1026
7.21k
    Result = false;
1027
7.21k
    TraverseStmt(E);
1028
7.21k
    return Result;
1029
7.21k
  }
1030
1031
27
  bool VisitCXXThisExpr(CXXThisExpr *E) {
1032
27
    assert(E->getType()->getPointeeCXXRecordDecl() == ClassType &&
1033
27
           "`this` doesn't refer to the enclosing class?");
1034
27
    Result = true;
1035
27
    return false;
1036
27
  }
1037
1038
7.11k
  bool VisitDeclRefExpr(DeclRefExpr *DRE) {
1039
7.11k
    if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
1040
7.10k
      if (Parms.count(PVD)) {
1041
7.10k
        Result = true;
1042
7.10k
        return false;
1043
7.10k
      }
1044
12
    return true;
1045
12
  }
1046
};
1047
}
1048
1049
7.22k
static void handleDiagnoseIfAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1050
7.22k
  S.Diag(AL.getLoc(), diag::ext_clang_diagnose_if);
1051
1052
7.22k
  Expr *Cond;
1053
7.22k
  StringRef Msg;
1054
7.22k
  if (!checkFunctionConditionAttr(S, D, AL, Cond, Msg))
1055
3
    return;
1056
1057
7.22k
  StringRef DiagTypeStr;
1058
7.22k
  if (!S.checkStringLiteralArgumentAttr(AL, 2, DiagTypeStr))
1059
0
    return;
1060
1061
7.22k
  DiagnoseIfAttr::DiagnosticType DiagType;
1062
7.22k
  if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) {
1063
3
    S.Diag(AL.getArgAsExpr(2)->getBeginLoc(),
1064
3
           diag::err_diagnose_if_invalid_diagnostic_type);
1065
3
    return;
1066
3
  }
1067
1068
7.21k
  bool ArgDependent = false;
1069
7.21k
  if (const auto *FD = dyn_cast<FunctionDecl>(D))
1070
7.21k
    ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond);
1071
7.21k
  D->addAttr(::new (S.Context) DiagnoseIfAttr(
1072
7.21k
      S.Context, AL, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D)));
1073
7.21k
}
1074
1075
31
static void handleNoBuiltinAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1076
31
  static constexpr const StringRef kWildcard = "*";
1077
1078
31
  llvm::SmallVector<StringRef, 16> Names;
1079
31
  bool HasWildcard = false;
1080
1081
41
  const auto AddBuiltinName = [&Names, &HasWildcard](StringRef Name) {
1082
41
    if (Name == kWildcard)
1083
12
      HasWildcard = true;
1084
41
    Names.push_back(Name);
1085
41
  };
1086
1087
  // Add previously defined attributes.
1088
31
  if (const auto *NBA = D->getAttr<NoBuiltinAttr>())
1089
8
    for (StringRef BuiltinName : NBA->builtinNames())
1090
8
      AddBuiltinName(BuiltinName);
1091
1092
  // Add current attributes.
1093
31
  if (AL.getNumArgs() == 0)
1094
10
    AddBuiltinName(kWildcard);
1095
21
  else
1096
45
    
for (unsigned I = 0, E = AL.getNumArgs(); 21
I != E;
++I24
) {
1097
24
      StringRef BuiltinName;
1098
24
      SourceLocation LiteralLoc;
1099
24
      if (!S.checkStringLiteralArgumentAttr(AL, I, BuiltinName, &LiteralLoc))
1100
0
        return;
1101
1102
24
      if (Builtin::Context::isBuiltinFunc(BuiltinName))
1103
23
        AddBuiltinName(BuiltinName);
1104
1
      else
1105
1
        S.Diag(LiteralLoc, diag::warn_attribute_no_builtin_invalid_builtin_name)
1106
1
            << BuiltinName << AL;
1107
24
    }
1108
1109
  // Repeating the same attribute is fine.
1110
31
  llvm::sort(Names);
1111
31
  Names.erase(std::unique(Names.begin(), Names.end()), Names.end());
1112
1113
  // Empty no_builtin must be on its own.
1114
31
  if (HasWildcard && 
Names.size() > 111
)
1115
1
    S.Diag(D->getLocation(),
1116
1
           diag::err_attribute_no_builtin_wildcard_or_builtin_name)
1117
1
        << AL;
1118
1119
31
  if (D->hasAttr<NoBuiltinAttr>())
1120
8
    D->dropAttr<NoBuiltinAttr>();
1121
31
  D->addAttr(::new (S.Context)
1122
31
                 NoBuiltinAttr(S.Context, AL, Names.data(), Names.size()));
1123
31
}
1124
1125
134
static void handlePassObjectSizeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1126
134
  if (D->hasAttr<PassObjectSizeAttr>()) {
1127
1
    S.Diag(D->getBeginLoc(), diag::err_attribute_only_once_per_parameter) << AL;
1128
1
    return;
1129
1
  }
1130
1131
133
  Expr *E = AL.getArgAsExpr(0);
1132
133
  uint32_t Type;
1133
133
  if (!checkUInt32Argument(S, AL, E, Type, /*Idx=*/1))
1134
2
    return;
1135
1136
  // pass_object_size's argument is passed in as the second argument of
1137
  // __builtin_object_size. So, it has the same constraints as that second
1138
  // argument; namely, it must be in the range [0, 3].
1139
131
  if (Type > 3) {
1140
2
    S.Diag(E->getBeginLoc(), diag::err_attribute_argument_out_of_range)
1141
2
        << AL << 0 << 3 << E->getSourceRange();
1142
2
    return;
1143
2
  }
1144
1145
  // pass_object_size is only supported on constant pointer parameters; as a
1146
  // kindness to users, we allow the parameter to be non-const for declarations.
1147
  // At this point, we have no clue if `D` belongs to a function declaration or
1148
  // definition, so we defer the constness check until later.
1149
129
  if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) {
1150
3
    S.Diag(D->getBeginLoc(), diag::err_attribute_pointers_only) << AL << 1;
1151
3
    return;
1152
3
  }
1153
1154
126
  D->addAttr(::new (S.Context) PassObjectSizeAttr(S.Context, AL, (int)Type));
1155
126
}
1156
1157
7
static void handleConsumableAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1158
7
  ConsumableAttr::ConsumedState DefaultState;
1159
1160
7
  if (AL.isArgIdent(0)) {
1161
6
    IdentifierLoc *IL = AL.getArgAsIdent(0);
1162
6
    if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1163
0
                                                   DefaultState)) {
1164
0
      S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1165
0
                                                               << IL->Ident;
1166
0
      return;
1167
0
    }
1168
1
  } else {
1169
1
    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1170
1
        << AL << AANT_ArgumentIdentifier;
1171
1
    return;
1172
1
  }
1173
1174
6
  D->addAttr(::new (S.Context) ConsumableAttr(S.Context, AL, DefaultState));
1175
6
}
1176
1177
static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD,
1178
31
                                    const ParsedAttr &AL) {
1179
31
  QualType ThisType = MD->getThisType()->getPointeeType();
1180
1181
31
  if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) {
1182
31
    if (!RD->hasAttr<ConsumableAttr>()) {
1183
3
      S.Diag(AL.getLoc(), diag::warn_attr_on_unconsumable_class) << RD;
1184
1185
3
      return false;
1186
3
    }
1187
28
  }
1188
1189
28
  return true;
1190
28
}
1191
1192
16
static void handleCallableWhenAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1193
16
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1194
1
    return;
1195
1196
15
  if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1197
1
    return;
1198
1199
14
  SmallVector<CallableWhenAttr::ConsumedState, 3> States;
1200
32
  for (unsigned ArgIndex = 0; ArgIndex < AL.getNumArgs(); 
++ArgIndex18
) {
1201
20
    CallableWhenAttr::ConsumedState CallableState;
1202
1203
20
    StringRef StateString;
1204
20
    SourceLocation Loc;
1205
20
    if (AL.isArgIdent(ArgIndex)) {
1206
1
      IdentifierLoc *Ident = AL.getArgAsIdent(ArgIndex);
1207
1
      StateString = Ident->Ident->getName();
1208
1
      Loc = Ident->Loc;
1209
19
    } else {
1210
19
      if (!S.checkStringLiteralArgumentAttr(AL, ArgIndex, StateString, &Loc))
1211
1
        return;
1212
19
    }
1213
1214
19
    if (!CallableWhenAttr::ConvertStrToConsumedState(StateString,
1215
1
                                                     CallableState)) {
1216
1
      S.Diag(Loc, diag::warn_attribute_type_not_supported) << AL << StateString;
1217
1
      return;
1218
1
    }
1219
1220
18
    States.push_back(CallableState);
1221
18
  }
1222
1223
12
  D->addAttr(::new (S.Context)
1224
12
                 CallableWhenAttr(S.Context, AL, States.data(), States.size()));
1225
12
}
1226
1227
7
static void handleParamTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1228
7
  ParamTypestateAttr::ConsumedState ParamState;
1229
1230
7
  if (AL.isArgIdent(0)) {
1231
7
    IdentifierLoc *Ident = AL.getArgAsIdent(0);
1232
7
    StringRef StateString = Ident->Ident->getName();
1233
1234
7
    if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString,
1235
0
                                                       ParamState)) {
1236
0
      S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported)
1237
0
          << AL << StateString;
1238
0
      return;
1239
0
    }
1240
0
  } else {
1241
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1242
0
        << AL << AANT_ArgumentIdentifier;
1243
0
    return;
1244
0
  }
1245
1246
  // FIXME: This check is currently being done in the analysis.  It can be
1247
  //        enabled here only after the parser propagates attributes at
1248
  //        template specialization definition, not declaration.
1249
  //QualType ReturnType = cast<ParmVarDecl>(D)->getType();
1250
  //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1251
  //
1252
  //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1253
  //    S.Diag(AL.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1254
  //      ReturnType.getAsString();
1255
  //    return;
1256
  //}
1257
1258
7
  D->addAttr(::new (S.Context) ParamTypestateAttr(S.Context, AL, ParamState));
1259
7
}
1260
1261
15
static void handleReturnTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1262
15
  ReturnTypestateAttr::ConsumedState ReturnState;
1263
1264
15
  if (AL.isArgIdent(0)) {
1265
14
    IdentifierLoc *IL = AL.getArgAsIdent(0);
1266
14
    if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(),
1267
1
                                                        ReturnState)) {
1268
1
      S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL
1269
1
                                                               << IL->Ident;
1270
1
      return;
1271
1
    }
1272
1
  } else {
1273
1
    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1274
1
        << AL << AANT_ArgumentIdentifier;
1275
1
    return;
1276
1
  }
1277
1278
  // FIXME: This check is currently being done in the analysis.  It can be
1279
  //        enabled here only after the parser propagates attributes at
1280
  //        template specialization definition, not declaration.
1281
  //QualType ReturnType;
1282
  //
1283
  //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) {
1284
  //  ReturnType = Param->getType();
1285
  //
1286
  //} else if (const CXXConstructorDecl *Constructor =
1287
  //             dyn_cast<CXXConstructorDecl>(D)) {
1288
  //  ReturnType = Constructor->getThisType()->getPointeeType();
1289
  //
1290
  //} else {
1291
  //
1292
  //  ReturnType = cast<FunctionDecl>(D)->getCallResultType();
1293
  //}
1294
  //
1295
  //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1296
  //
1297
  //if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1298
  //    S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) <<
1299
  //      ReturnType.getAsString();
1300
  //    return;
1301
  //}
1302
1303
13
  D->addAttr(::new (S.Context) ReturnTypestateAttr(S.Context, AL, ReturnState));
1304
13
}
1305
1306
10
static void handleSetTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1307
10
  if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1308
1
    return;
1309
1310
9
  SetTypestateAttr::ConsumedState NewState;
1311
9
  if (AL.isArgIdent(0)) {
1312
9
    IdentifierLoc *Ident = AL.getArgAsIdent(0);
1313
9
    StringRef Param = Ident->Ident->getName();
1314
9
    if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) {
1315
0
      S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1316
0
                                                                  << Param;
1317
0
      return;
1318
0
    }
1319
0
  } else {
1320
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1321
0
        << AL << AANT_ArgumentIdentifier;
1322
0
    return;
1323
0
  }
1324
1325
9
  D->addAttr(::new (S.Context) SetTypestateAttr(S.Context, AL, NewState));
1326
9
}
1327
1328
6
static void handleTestTypestateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1329
6
  if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), AL))
1330
1
    return;
1331
1332
5
  TestTypestateAttr::ConsumedState TestState;
1333
5
  if (AL.isArgIdent(0)) {
1334
5
    IdentifierLoc *Ident = AL.getArgAsIdent(0);
1335
5
    StringRef Param = Ident->Ident->getName();
1336
5
    if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) {
1337
0
      S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) << AL
1338
0
                                                                  << Param;
1339
0
      return;
1340
0
    }
1341
0
  } else {
1342
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1343
0
        << AL << AANT_ArgumentIdentifier;
1344
0
    return;
1345
0
  }
1346
1347
5
  D->addAttr(::new (S.Context) TestTypestateAttr(S.Context, AL, TestState));
1348
5
}
1349
1350
2.56k
static void handleExtVectorTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1351
  // Remember this typedef decl, we will need it later for diagnostics.
1352
2.56k
  S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D));
1353
2.56k
}
1354
1355
38.9k
static void handlePackedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1356
38.9k
  if (auto *TD = dyn_cast<TagDecl>(D))
1357
37.4k
    TD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1358
1.57k
  else if (auto *FD = dyn_cast<FieldDecl>(D)) {
1359
1.56k
    bool BitfieldByteAligned = (!FD->getType()->isDependentType() &&
1360
1.56k
                                !FD->getType()->isIncompleteType() &&
1361
1.30k
                                FD->isBitField() &&
1362
30
                                S.Context.getTypeAlign(FD->getType()) <= 8);
1363
1364
1.56k
    if (S.getASTContext().getTargetInfo().getTriple().isPS4()) {
1365
6
      if (BitfieldByteAligned)
1366
        // The PS4 target needs to maintain ABI backwards compatibility.
1367
1
        S.Diag(AL.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
1368
1
            << AL << FD->getType();
1369
5
      else
1370
5
        FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1371
1.56k
    } else {
1372
      // Report warning about changed offset in the newer compiler versions.
1373
1.56k
      if (BitfieldByteAligned)
1374
2
        S.Diag(AL.getLoc(), diag::warn_attribute_packed_for_bitfield);
1375
1376
1.56k
      FD->addAttr(::new (S.Context) PackedAttr(S.Context, AL));
1377
1.56k
    }
1378
1379
1.56k
  } else
1380
10
    S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
1381
38.9k
}
1382
1383
146
static bool checkIBOutletCommon(Sema &S, Decl *D, const ParsedAttr &AL) {
1384
  // The IBOutlet/IBOutletCollection attributes only apply to instance
1385
  // variables or properties of Objective-C classes.  The outlet must also
1386
  // have an object reference type.
1387
146
  if (const auto *VD = dyn_cast<ObjCIvarDecl>(D)) {
1388
39
    if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
1389
10
      S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1390
10
          << AL << VD->getType() << 0;
1391
10
      return false;
1392
10
    }
1393
107
  }
1394
107
  else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
1395
107
    if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
1396
8
      S.Diag(AL.getLoc(), diag::warn_iboutlet_object_type)
1397
8
          << AL << PD->getType() << 1;
1398
8
      return false;
1399
8
    }
1400
0
  }
1401
0
  else {
1402
0
    S.Diag(AL.getLoc(), diag::warn_attribute_iboutlet) << AL;
1403
0
    return false;
1404
0
  }
1405
1406
128
  return true;
1407
128
}
1408
1409
111
static void handleIBOutlet(Sema &S, Decl *D, const ParsedAttr &AL) {
1410
111
  if (!checkIBOutletCommon(S, D, AL))
1411
14
    return;
1412
1413
97
  D->addAttr(::new (S.Context) IBOutletAttr(S.Context, AL));
1414
97
}
1415
1416
35
static void handleIBOutletCollection(Sema &S, Decl *D, const ParsedAttr &AL) {
1417
1418
  // The iboutletcollection attribute can have zero or one arguments.
1419
35
  if (AL.getNumArgs() > 1) {
1420
0
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1421
0
    return;
1422
0
  }
1423
1424
35
  if (!checkIBOutletCommon(S, D, AL))
1425
4
    return;
1426
1427
31
  ParsedType PT;
1428
1429
31
  if (AL.hasParsedType())
1430
23
    PT = AL.getTypeArg();
1431
8
  else {
1432
8
    PT = S.getTypeName(S.Context.Idents.get("NSObject"), AL.getLoc(),
1433
8
                       S.getScopeForContext(D->getDeclContext()->getParent()));
1434
8
    if (!PT) {
1435
0
      S.Diag(AL.getLoc(), diag::err_iboutletcollection_type) << "NSObject";
1436
0
      return;
1437
0
    }
1438
31
  }
1439
1440
31
  TypeSourceInfo *QTLoc = nullptr;
1441
31
  QualType QT = S.GetTypeFromParser(PT, &QTLoc);
1442
31
  if (!QTLoc)
1443
8
    QTLoc = S.Context.getTrivialTypeSourceInfo(QT, AL.getLoc());
1444
1445
  // Diagnose use of non-object type in iboutletcollection attribute.
1446
  // FIXME. Gnu attribute extension ignores use of builtin types in
1447
  // attributes. So, __attribute__((iboutletcollection(char))) will be
1448
  // treated as __attribute__((iboutletcollection())).
1449
31
  if (!QT->isObjCIdType() && 
!QT->isObjCObjectType()26
) {
1450
4
    S.Diag(AL.getLoc(),
1451
2
           QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype
1452
2
                               : diag::err_iboutletcollection_type) << QT;
1453
4
    return;
1454
4
  }
1455
1456
27
  D->addAttr(::new (S.Context) IBOutletCollectionAttr(S.Context, AL, QTLoc));
1457
27
}
1458
1459
102k
bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1460
102k
  if (RefOkay) {
1461
31.2k
    if (T->isReferenceType())
1462
2
      return true;
1463
71.5k
  } else {
1464
71.5k
    T = T.getNonReferenceType();
1465
71.5k
  }
1466
1467
  // The nonnull attribute, and other similar attributes, can be applied to a
1468
  // transparent union that contains a pointer type.
1469
102k
  if (const RecordType *UT = T->getAsUnionType()) {
1470
18
    if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
1471
18
      RecordDecl *UD = UT->getDecl();
1472
23
      for (const auto *I : UD->fields()) {
1473
23
        QualType QT = I->getType();
1474
23
        if (QT->isAnyPointerType() || 
QT->isBlockPointerType()5
)
1475
18
          return true;
1476
23
      }
1477
18
    }
1478
18
  }
1479
1480
102k
  return T->isAnyPointerType() || 
T->isBlockPointerType()33.3k
;
1481
102k
}
1482
1483
static bool attrNonNullArgCheck(Sema &S, QualType T, const ParsedAttr &AL,
1484
                                SourceRange AttrParmRange,
1485
                                SourceRange TypeRange,
1486
70.5k
                                bool isReturnValue = false) {
1487
70.5k
  if (!S.isValidPointerAttrType(T)) {
1488
17
    if (isReturnValue)
1489
6
      S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1490
6
          << AL << AttrParmRange << TypeRange;
1491
11
    else
1492
11
      S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1493
11
          << AL << AttrParmRange << TypeRange << 0;
1494
17
    return false;
1495
17
  }
1496
70.5k
  return true;
1497
70.5k
}
1498
1499
92.9k
static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1500
92.9k
  SmallVector<ParamIdx, 8> NonNullArgs;
1501
163k
  for (unsigned I = 0; I < AL.getNumArgs(); 
++I70.4k
) {
1502
70.4k
    Expr *Ex = AL.getArgAsExpr(I);
1503
70.4k
    ParamIdx Idx;
1504
70.4k
    if (!checkFunctionOrMethodParameterIndex(S, D, AL, I + 1, Ex, Idx))
1505
5
      return;
1506
1507
    // Is the function argument a pointer type?
1508
70.4k
    if (Idx.getASTIndex() < getFunctionOrMethodNumParams(D) &&
1509
70.4k
        !attrNonNullArgCheck(
1510
70.4k
            S, getFunctionOrMethodParamType(D, Idx.getASTIndex()), AL,
1511
70.4k
            Ex->getSourceRange(),
1512
70.4k
            getFunctionOrMethodParamRange(D, Idx.getASTIndex())))
1513
7
      continue;
1514
1515
70.4k
    NonNullArgs.push_back(Idx);
1516
70.4k
  }
1517
1518
  // If no arguments were specified to __attribute__((nonnull)) then all pointer
1519
  // arguments have a nonnull attribute; warn if there aren't any. Skip this
1520
  // check if the attribute came from a macro expansion or a template
1521
  // instantiation.
1522
92.9k
  if (NonNullArgs.empty() && 
AL.getLoc().isFileID()24.8k
&&
1523
65
      !S.inTemplateInstantiation()) {
1524
65
    bool AnyPointers = isFunctionOrMethodVariadic(D);
1525
65
    for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
1526
120
         I != E && 
!AnyPointers82
;
++I55
) {
1527
55
      QualType T = getFunctionOrMethodParamType(D, I);
1528
55
      if (T->isDependentType() || 
S.isValidPointerAttrType(T)53
)
1529
47
        AnyPointers = true;
1530
55
    }
1531
1532
65
    if (!AnyPointers)
1533
6
      S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_no_pointers);
1534
65
  }
1535
1536
92.9k
  ParamIdx *Start = NonNullArgs.data();
1537
92.9k
  unsigned Size = NonNullArgs.size();
1538
92.9k
  llvm::array_pod_sort(Start, Start + Size);
1539
92.9k
  D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, Start, Size));
1540
92.9k
}
1541
1542
static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D,
1543
32
                                       const ParsedAttr &AL) {
1544
32
  if (AL.getNumArgs() > 0) {
1545
6
    if (D->getFunctionType()) {
1546
3
      handleNonNullAttr(S, D, AL);
1547
3
    } else {
1548
3
      S.Diag(AL.getLoc(), diag::warn_attribute_nonnull_parm_no_args)
1549
3
        << D->getSourceRange();
1550
3
    }
1551
6
    return;
1552
6
  }
1553
1554
  // Is the argument a pointer type?
1555
26
  if (!attrNonNullArgCheck(S, D->getType(), AL, SourceRange(),
1556
26
                           D->getSourceRange()))
1557
4
    return;
1558
1559
22
  D->addAttr(::new (S.Context) NonNullAttr(S.Context, AL, nullptr, 0));
1560
22
}
1561
1562
29
static void handleReturnsNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1563
29
  QualType ResultType = getFunctionOrMethodResultType(D);
1564
29
  SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1565
29
  if (!attrNonNullArgCheck(S, ResultType, AL, SourceRange(), SR,
1566
29
                           /* isReturnValue */ true))
1567
6
    return;
1568
1569
23
  D->addAttr(::new (S.Context) ReturnsNonNullAttr(S.Context, AL));
1570
23
}
1571
1572
31.2k
static void handleNoEscapeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1573
31.2k
  if (D->isInvalidDecl())
1574
0
    return;
1575
1576
  // noescape only applies to pointer types.
1577
31.2k
  QualType T = cast<ParmVarDecl>(D)->getType();
1578
31.2k
  if (!S.isValidPointerAttrType(T, /* RefOkay */ true)) {
1579
6
    S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
1580
6
        << AL << AL.getRange() << 0;
1581
6
    return;
1582
6
  }
1583
1584
31.2k
  D->addAttr(::new (S.Context) NoEscapeAttr(S.Context, AL));
1585
31.2k
}
1586
1587
27
static void handleAssumeAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1588
27
  Expr *E = AL.getArgAsExpr(0),
1589
18
       *OE = AL.getNumArgs() > 1 ? 
AL.getArgAsExpr(1)9
: nullptr;
1590
27
  S.AddAssumeAlignedAttr(D, AL, E, OE);
1591
27
}
1592
1593
36
static void handleAllocAlignAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1594
36
  S.AddAllocAlignAttr(D, AL, AL.getArgAsExpr(0));
1595
36
}
1596
1597
void Sema::AddAssumeAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
1598
35
                                Expr *OE) {
1599
35
  QualType ResultType = getFunctionOrMethodResultType(D);
1600
35
  SourceRange SR = getFunctionOrMethodResultSourceRange(D);
1601
1602
35
  AssumeAlignedAttr TmpAttr(Context, CI, E, OE);
1603
35
  SourceLocation AttrLoc = TmpAttr.getLocation();
1604
1605
35
  if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1606
3
    Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1607
3
        << &TmpAttr << TmpAttr.getRange() << SR;
1608
3
    return;
1609
3
  }
1610
1611
32
  if (!E->isValueDependent()) {
1612
28
    Optional<llvm::APSInt> I = llvm::APSInt(64);
1613
28
    if (!(I = E->getIntegerConstantExpr(Context))) {
1614
0
      if (OE)
1615
0
        Diag(AttrLoc, diag::err_attribute_argument_n_type)
1616
0
          << &TmpAttr << 1 << AANT_ArgumentIntegerConstant
1617
0
          << E->getSourceRange();
1618
0
      else
1619
0
        Diag(AttrLoc, diag::err_attribute_argument_type)
1620
0
          << &TmpAttr << AANT_ArgumentIntegerConstant
1621
0
          << E->getSourceRange();
1622
0
      return;
1623
0
    }
1624
1625
28
    if (!I->isPowerOf2()) {
1626
5
      Diag(AttrLoc, diag::err_alignment_not_power_of_two)
1627
5
        << E->getSourceRange();
1628
5
      return;
1629
5
    }
1630
1631
23
    if (*I > Sema::MaximumAlignment)
1632
5
      Diag(CI.getLoc(), diag::warn_assume_aligned_too_great)
1633
5
          << CI.getRange() << Sema::MaximumAlignment;
1634
23
  }
1635
1636
27
  if (OE && 
!OE->isValueDependent()10
&&
!OE->isIntegerConstantExpr(Context)8
) {
1637
0
    Diag(AttrLoc, diag::err_attribute_argument_n_type)
1638
0
        << &TmpAttr << 2 << AANT_ArgumentIntegerConstant
1639
0
        << OE->getSourceRange();
1640
0
    return;
1641
0
  }
1642
1643
27
  D->addAttr(::new (Context) AssumeAlignedAttr(Context, CI, E, OE));
1644
27
}
1645
1646
void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
1647
44
                             Expr *ParamExpr) {
1648
44
  QualType ResultType = getFunctionOrMethodResultType(D);
1649
1650
44
  AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
1651
44
  SourceLocation AttrLoc = CI.getLoc();
1652
1653
44
  if (!ResultType->isDependentType() &&
1654
42
      !isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1655
2
    Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
1656
2
        << &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
1657
2
    return;
1658
2
  }
1659
1660
42
  ParamIdx Idx;
1661
42
  const auto *FuncDecl = cast<FunctionDecl>(D);
1662
42
  if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr,
1663
42
                                           /*AttrArgNum=*/1, ParamExpr, Idx))
1664
5
    return;
1665
1666
37
  QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
1667
37
  if (!Ty->isDependentType() && 
!Ty->isIntegralType(Context)35
&&
1668
9
      !Ty->isAlignValT()) {
1669
3
    Diag(ParamExpr->getBeginLoc(), diag::err_attribute_integers_only)
1670
3
        << &TmpAttr
1671
3
        << FuncDecl->getParamDecl(Idx.getASTIndex())->getSourceRange();
1672
3
    return;
1673
3
  }
1674
1675
34
  D->addAttr(::new (Context) AllocAlignAttr(Context, CI, Idx));
1676
34
}
1677
1678
/// Normalize the attribute, __foo__ becomes foo.
1679
/// Returns true if normalization was applied.
1680
37.6k
static bool normalizeName(StringRef &AttrName) {
1681
37.6k
  if (AttrName.size() > 4 && 
AttrName.startswith("__")37.3k
&&
1682
34.6k
      AttrName.endswith("__")) {
1683
34.6k
    AttrName = AttrName.drop_front(2).drop_back(2);
1684
34.6k
    return true;
1685
34.6k
  }
1686
3.01k
  return false;
1687
3.01k
}
1688
1689
50
static void handleOwnershipAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1690
  // This attribute must be applied to a function declaration. The first
1691
  // argument to the attribute must be an identifier, the name of the resource,
1692
  // for example: malloc. The following arguments must be argument indexes, the
1693
  // arguments must be of integer type for Returns, otherwise of pointer type.
1694
  // The difference between Holds and Takes is that a pointer may still be used
1695
  // after being held. free() should be __attribute((ownership_takes)), whereas
1696
  // a list append function may well be __attribute((ownership_holds)).
1697
1698
50
  if (!AL.isArgIdent(0)) {
1699
1
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
1700
1
        << AL << 1 << AANT_ArgumentIdentifier;
1701
1
    return;
1702
1
  }
1703
1704
  // Figure out our Kind.
1705
49
  OwnershipAttr::OwnershipKind K =
1706
49
      OwnershipAttr(S.Context, AL, nullptr, nullptr, 0).getOwnKind();
1707
1708
  // Check arguments.
1709
49
  switch (K) {
1710
31
  case OwnershipAttr::Takes:
1711
31
  case OwnershipAttr::Holds:
1712
31
    if (AL.getNumArgs() < 2) {
1713
2
      S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << AL << 2;
1714
2
      return;
1715
2
    }
1716
29
    break;
1717
18
  case OwnershipAttr::Returns:
1718
18
    if (AL.getNumArgs() > 2) {
1719
1
      S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << AL << 1;
1720
1
      return;
1721
1
    }
1722
17
    break;
1723
46
  }
1724
1725
46
  IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident;
1726
1727
46
  StringRef ModuleName = Module->getName();
1728
46
  if (normalizeName(ModuleName)) {
1729
0
    Module = &S.PP.getIdentifierTable().get(ModuleName);
1730
0
  }
1731
1732
46
  SmallVector<ParamIdx, 8> OwnershipArgs;
1733
88
  for (unsigned i = 1; i < AL.getNumArgs(); 
++i42
) {
1734
51
    Expr *Ex = AL.getArgAsExpr(i);
1735
51
    ParamIdx Idx;
1736
51
    if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx))
1737
3
      return;
1738
1739
    // Is the function argument a pointer type?
1740
48
    QualType T = getFunctionOrMethodParamType(D, Idx.getASTIndex());
1741
48
    int Err = -1;  // No error
1742
48
    switch (K) {
1743
37
      case OwnershipAttr::Takes:
1744
37
      case OwnershipAttr::Holds:
1745
37
        if (!T->isAnyPointerType() && 
!T->isBlockPointerType()1
)
1746
1
          Err = 0;
1747
37
        break;
1748
11
      case OwnershipAttr::Returns:
1749
11
        if (!T->isIntegerType())
1750
2
          Err = 1;
1751
11
        break;
1752
48
    }
1753
48
    if (-1 != Err) {
1754
3
      S.Diag(AL.getLoc(), diag::err_ownership_type) << AL << Err
1755
3
                                                    << Ex->getSourceRange();
1756
3
      return;
1757
3
    }
1758
1759
    // Check we don't have a conflict with another ownership attribute.
1760
45
    for (const auto *I : D->specific_attrs<OwnershipAttr>()) {
1761
      // Cannot have two ownership attributes of different kinds for the same
1762
      // index.
1763
8
      if (I->getOwnKind() != K && I->args_end() !=
1764
1
          std::find(I->args_begin(), I->args_end(), Idx)) {
1765
1
        S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) << AL << I;
1766
1
        return;
1767
7
      } else if (K == OwnershipAttr::Returns &&
1768
2
                 I->getOwnKind() == OwnershipAttr::Returns) {
1769
        // A returns attribute conflicts with any other returns attribute using
1770
        // a different index.
1771
2
        if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) {
1772
2
          S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch)
1773
2
              << I->args_begin()->getSourceIndex();
1774
2
          if (I->args_size())
1775
2
            S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch)
1776
2
                << Idx.getSourceIndex() << Ex->getSourceRange();
1777
2
          return;
1778
2
        }
1779
2
      }
1780
8
    }
1781
42
    OwnershipArgs.push_back(Idx);
1782
42
  }
1783
1784
37
  ParamIdx *Start = OwnershipArgs.data();
1785
37
  unsigned Size = OwnershipArgs.size();
1786
37
  llvm::array_pod_sort(Start, Start + Size);
1787
37
  D->addAttr(::new (S.Context)
1788
37
                 OwnershipAttr(S.Context, AL, Module, Start, Size));
1789
37
}
1790
1791
48
static void handleWeakRefAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1792
  // Check the attribute arguments.
1793
48
  if (AL.getNumArgs() > 1) {
1794
0
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
1795
0
    return;
1796
0
  }
1797
1798
  // gcc rejects
1799
  // class c {
1800
  //   static int a __attribute__((weakref ("v2")));
1801
  //   static int b() __attribute__((weakref ("f3")));
1802
  // };
1803
  // and ignores the attributes of
1804
  // void f(void) {
1805
  //   static int a __attribute__((weakref ("v2")));
1806
  // }
1807
  // we reject them
1808
48
  const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
1809
48
  if (!Ctx->isFileContext()) {
1810
3
    S.Diag(AL.getLoc(), diag::err_attribute_weakref_not_global_context)
1811
3
        << cast<NamedDecl>(D);
1812
3
    return;
1813
3
  }
1814
1815
  // The GCC manual says
1816
  //
1817
  // At present, a declaration to which `weakref' is attached can only
1818
  // be `static'.
1819
  //
1820
  // It also says
1821
  //
1822
  // Without a TARGET,
1823
  // given as an argument to `weakref' or to `alias', `weakref' is
1824
  // equivalent to `weak'.
1825
  //
1826
  // gcc 4.4.1 will accept
1827
  // int a7 __attribute__((weakref));
1828
  // as
1829
  // int a7 __attribute__((weak));
1830
  // This looks like a bug in gcc. We reject that for now. We should revisit
1831
  // it if this behaviour is actually used.
1832
1833
  // GCC rejects
1834
  // static ((alias ("y"), weakref)).
1835
  // Should we? How to check that weakref is before or after alias?
1836
1837
  // FIXME: it would be good for us to keep the WeakRefAttr as-written instead
1838
  // of transforming it into an AliasAttr.  The WeakRefAttr never uses the
1839
  // StringRef parameter it was given anyway.
1840
45
  StringRef Str;
1841
45
  if (AL.getNumArgs() && 
S.checkStringLiteralArgumentAttr(AL, 0, Str)36
)
1842
    // GCC will accept anything as the argument of weakref. Should we
1843
    // check for an existing decl?
1844
35
    D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
1845
1846
45
  D->addAttr(::new (S.Context) WeakRefAttr(S.Context, AL));
1847
45
}
1848
1849
12
static void handleIFuncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1850
12
  StringRef Str;
1851
12
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1852
0
    return;
1853
1854
  // Aliases should be on declarations, not definitions.
1855
12
  const auto *FD = cast<FunctionDecl>(D);
1856
12
  if (FD->isThisDeclarationADefinition()) {
1857
0
    S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 1;
1858
0
    return;
1859
0
  }
1860
1861
12
  D->addAttr(::new (S.Context) IFuncAttr(S.Context, AL, Str));
1862
12
}
1863
1864
128
static void handleAliasAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1865
128
  StringRef Str;
1866
128
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
1867
0
    return;
1868
1869
128
  if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
1870
1
    S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_darwin);
1871
1
    return;
1872
1
  }
1873
127
  if (S.Context.getTargetInfo().getTriple().isNVPTX()) {
1874
1
    S.Diag(AL.getLoc(), diag::err_alias_not_supported_on_nvptx);
1875
1
  }
1876
1877
  // Aliases should be on declarations, not definitions.
1878
127
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
1879
93
    if (FD->isThisDeclarationADefinition()) {
1880
0
      S.Diag(AL.getLoc(), diag::err_alias_is_definition) << FD << 0;
1881
0
      return;
1882
0
    }
1883
34
  } else {
1884
34
    const auto *VD = cast<VarDecl>(D);
1885
34
    if (VD->isThisDeclarationADefinition() && 
VD->isExternallyVisible()11
) {
1886
1
      S.Diag(AL.getLoc(), diag::err_alias_is_definition) << VD << 0;
1887
1
      return;
1888
1
    }
1889
126
  }
1890
1891
  // Mark target used to prevent unneeded-internal-declaration warnings.
1892
126
  if (!S.LangOpts.CPlusPlus) {
1893
    // FIXME: demangle Str for C++, as the attribute refers to the mangled
1894
    // linkage name, not the pre-mangled identifier.
1895
110
    const DeclarationNameInfo target(&S.Context.Idents.get(Str), AL.getLoc());
1896
110
    LookupResult LR(S, target, Sema::LookupOrdinaryName);
1897
110
    if (S.LookupQualifiedName(LR, S.getCurLexicalContext()))
1898
89
      for (NamedDecl *ND : LR)
1899
89
        ND->markUsed(S.Context);
1900
110
  }
1901
1902
126
  D->addAttr(::new (S.Context) AliasAttr(S.Context, AL, Str));
1903
126
}
1904
1905
25
static void handleTLSModelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1906
25
  StringRef Model;
1907
25
  SourceLocation LiteralLoc;
1908
  // Check that it is a string.
1909
25
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Model, &LiteralLoc))
1910
1
    return;
1911
1912
  // Check that the value.
1913
24
  if (Model != "global-dynamic" && 
Model != "local-dynamic"20
1914
17
      && Model != "initial-exec" && 
Model != "local-exec"5
) {
1915
1
    S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg);
1916
1
    return;
1917
1
  }
1918
1919
23
  D->addAttr(::new (S.Context) TLSModelAttr(S.Context, AL, Model));
1920
23
}
1921
1922
15.0k
static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1923
15.0k
  QualType ResultType = getFunctionOrMethodResultType(D);
1924
15.0k
  if (ResultType->isAnyPointerType() || 
ResultType->isBlockPointerType()5
) {
1925
15.0k
    D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
1926
15.0k
    return;
1927
15.0k
  }
1928
1929
4
  S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
1930
4
      << AL << getFunctionOrMethodResultSourceRange(D);
1931
4
}
1932
1933
110
static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1934
110
  FunctionDecl *FD = cast<FunctionDecl>(D);
1935
1936
110
  if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
1937
18
    if (MD->getParent()->isLambda()) {
1938
1
      S.Diag(AL.getLoc(), diag::err_attribute_dll_lambda) << AL;
1939
1
      return;
1940
1
    }
1941
109
  }
1942
1943
109
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
1944
2
    return;
1945
1946
107
  SmallVector<IdentifierInfo *, 8> CPUs;
1947
266
  for (unsigned ArgNo = 0; ArgNo < getNumAttributeArgs(AL); 
++ArgNo159
) {
1948
163
    if (!AL.isArgIdent(ArgNo)) {
1949
0
      S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
1950
0
          << AL << AANT_ArgumentIdentifier;
1951
0
      return;
1952
0
    }
1953
1954
163
    IdentifierLoc *CPUArg = AL.getArgAsIdent(ArgNo);
1955
163
    StringRef CPUName = CPUArg->Ident->getName().trim();
1956
1957
163
    if (!S.Context.getTargetInfo().validateCPUSpecificCPUDispatch(CPUName)) {
1958
2
      S.Diag(CPUArg->Loc, diag::err_invalid_cpu_specific_dispatch_value)
1959
2
          << CPUName << (AL.getKind() == ParsedAttr::AT_CPUDispatch);
1960
2
      return;
1961
2
    }
1962
1963
161
    const TargetInfo &Target = S.Context.getTargetInfo();
1964
161
    if (llvm::any_of(CPUs, [CPUName, &Target](const IdentifierInfo *Cur) {
1965
83
          return Target.CPUSpecificManglingCharacter(CPUName) ==
1966
83
                 Target.CPUSpecificManglingCharacter(Cur->getName());
1967
2
        })) {
1968
2
      S.Diag(AL.getLoc(), diag::warn_multiversion_duplicate_entries);
1969
2
      return;
1970
2
    }
1971
159
    CPUs.push_back(CPUArg->Ident);
1972
159
  }
1973
1974
103
  FD->setIsMultiVersion(true);
1975
103
  if (AL.getKind() == ParsedAttr::AT_CPUSpecific)
1976
70
    D->addAttr(::new (S.Context)
1977
70
                   CPUSpecificAttr(S.Context, AL, CPUs.data(), CPUs.size()));
1978
33
  else
1979
33
    D->addAttr(::new (S.Context)
1980
33
                   CPUDispatchAttr(S.Context, AL, CPUs.data(), CPUs.size()));
1981
103
}
1982
1983
10
static void handleCommonAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1984
10
  if (S.LangOpts.CPlusPlus) {
1985
1
    S.Diag(AL.getLoc(), diag::err_attribute_not_supported_in_lang)
1986
1
        << AL << AttributeLangSupport::Cpp;
1987
1
    return;
1988
1
  }
1989
1990
9
  if (CommonAttr *CA = S.mergeCommonAttr(D, AL))
1991
8
    D->addAttr(CA);
1992
9
}
1993
1994
100
static void handleCmseNSEntryAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
1995
100
  if (S.LangOpts.CPlusPlus && 
!D->getDeclContext()->isExternCContext()2
) {
1996
1
    S.Diag(AL.getLoc(), diag::err_attribute_not_clinkage) << AL;
1997
1
    return;
1998
1
  }
1999
2000
99
  const auto *FD = cast<FunctionDecl>(D);
2001
99
  if (!FD->isExternallyVisible()) {
2002
1
    S.Diag(AL.getLoc(), diag::warn_attribute_cmse_entry_static);
2003
1
    return;
2004
1
  }
2005
2006
98
  D->addAttr(::new (S.Context) CmseNSEntryAttr(S.Context, AL));
2007
98
}
2008
2009
31
static void handleNakedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2010
31
  if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, AL))
2011
1
    return;
2012
2013
30
  if (AL.isDeclspecAttribute()) {
2014
9
    const auto &Triple = S.getASTContext().getTargetInfo().getTriple();
2015
9
    const auto &Arch = Triple.getArch();
2016
9
    if (Arch != llvm::Triple::x86 &&
2017
2
        (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) {
2018
1
      S.Diag(AL.getLoc(), diag::err_attribute_not_supported_on_arch)
2019
1
          << AL << Triple.getArchName();
2020
1
      return;
2021
1
    }
2022
29
  }
2023
2024
29
  D->addAttr(::new (S.Context) NakedAttr(S.Context, AL));
2025
29
}
2026
2027
8.16k
static void handleNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2028
8.16k
  if (hasDeclarator(D)) 
return8.14k
;
2029
2030
18
  if (!isa<ObjCMethodDecl>(D)) {
2031
4
    S.Diag(Attrs.getLoc(), diag::warn_attribute_wrong_decl_type)
2032
4
        << Attrs << ExpectedFunctionOrMethod;
2033
4
    return;
2034
4
  }
2035
2036
14
  D->addAttr(::new (S.Context) NoReturnAttr(S.Context, Attrs));
2037
14
}
2038
2039
10
static void handleNoCfCheckAttr(Sema &S, Decl *D, const ParsedAttr &Attrs) {
2040
10
  if (!S.getLangOpts().CFProtectionBranch)
2041
0
    S.Diag(Attrs.getLoc(), diag::warn_nocf_check_attribute_ignored);
2042
10
  else
2043
10
    handleSimpleAttribute<AnyX86NoCfCheckAttr>(S, D, Attrs);
2044
10
}
2045
2046
78.3k
bool Sema::CheckAttrNoArgs(const ParsedAttr &Attrs) {
2047
78.3k
  if (!checkAttributeNumArgs(*this, Attrs, 0)) {
2048
7
    Attrs.setInvalid();
2049
7
    return true;
2050
7
  }
2051
2052
78.3k
  return false;
2053
78.3k
}
2054
2055
40.1M
bool Sema::CheckAttrTarget(const ParsedAttr &AL) {
2056
  // Check whether the attribute is valid on the current target.
2057
40.1M
  if (!AL.existsInTarget(Context.getTargetInfo())) {
2058
11
    Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
2059
11
    AL.setInvalid();
2060
11
    return true;
2061
11
  }
2062
2063
40.1M
  return false;
2064
40.1M
}
2065
2066
7
static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2067
2068
  // The checking path for 'noreturn' and 'analyzer_noreturn' are different
2069
  // because 'analyzer_noreturn' does not impact the type.
2070
7
  if (!isFunctionOrMethodOrBlock(D)) {
2071
0
    ValueDecl *VD = dyn_cast<ValueDecl>(D);
2072
0
    if (!VD || (!VD->getType()->isBlockPointerType() &&
2073
0
                !VD->getType()->isFunctionPointerType())) {
2074
0
      S.Diag(AL.getLoc(), AL.isCXX11Attribute()
2075
0
                              ? diag::err_attribute_wrong_decl_type
2076
0
                              : diag::warn_attribute_wrong_decl_type)
2077
0
          << AL << ExpectedFunctionMethodOrBlock;
2078
0
      return;
2079
0
    }
2080
7
  }
2081
2082
7
  D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(S.Context, AL));
2083
7
}
2084
2085
// PS3 PPU-specific.
2086
17
static void handleVecReturnAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2087
  /*
2088
    Returning a Vector Class in Registers
2089
2090
    According to the PPU ABI specifications, a class with a single member of
2091
    vector type is returned in memory when used as the return value of a
2092
    function.
2093
    This results in inefficient code when implementing vector classes. To return
2094
    the value in a single vector register, add the vecreturn attribute to the
2095
    class definition. This attribute is also applicable to struct types.
2096
2097
    Example:
2098
2099
    struct Vector
2100
    {
2101
      __vector float xyzw;
2102
    } __attribute__((vecreturn));
2103
2104
    Vector Add(Vector lhs, Vector rhs)
2105
    {
2106
      Vector result;
2107
      result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
2108
      return result; // This will be returned in a register
2109
    }
2110
  */
2111
17
  if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) {
2112
1
    S.Diag(AL.getLoc(), diag::err_repeat_attribute) << A;
2113
1
    return;
2114
1
  }
2115
2116
16
  const auto *R = cast<RecordDecl>(D);
2117
16
  int count = 0;
2118
2119
16
  if (!isa<CXXRecordDecl>(R)) {
2120
0
    S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2121
0
    return;
2122
0
  }
2123
2124
16
  if (!cast<CXXRecordDecl>(R)->isPOD()) {
2125
5
    S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
2126
5
    return;
2127
5
  }
2128
2129
16
  
for (const auto *I : R->fields())11
{
2130
16
    if ((count == 1) || 
!I->getType()->isVectorType()11
) {
2131
5
      S.Diag(AL.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
2132
5
      return;
2133
5
    }
2134
11
    count++;
2135
11
  }
2136
2137
6
  D->addAttr(::new (S.Context) VecReturnAttr(S.Context, AL));
2138
6
}
2139
2140
static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D,
2141
27
                                 const ParsedAttr &AL) {
2142
27
  if (isa<ParmVarDecl>(D)) {
2143
    // [[carries_dependency]] can only be applied to a parameter if it is a
2144
    // parameter of a function declaration or lambda.
2145
14
    if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) {
2146
6
      S.Diag(AL.getLoc(),
2147
6
             diag::err_carries_dependency_param_not_function_decl);
2148
6
      return;
2149
6
    }
2150
21
  }
2151
2152
21
  D->addAttr(::new (S.Context) CarriesDependencyAttr(S.Context, AL));
2153
21
}
2154
2155
141k
static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2156
141k
  bool IsCXX17Attr = AL.isCXX11Attribute() && 
!AL.getScopeName()25
;
2157
2158
  // If this is spelled as the standard C++17 attribute, but not in C++17, warn
2159
  // about using it as an extension.
2160
141k
  if (!S.getLangOpts().CPlusPlus17 && 
IsCXX17Attr140k
)
2161
5
    S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2162
2163
141k
  D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
2164
141k
}
2165
2166
25
static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2167
25
  uint32_t priority = ConstructorAttr::DefaultPriority;
2168
25
  if (AL.getNumArgs() &&
2169
7
      !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
2170
4
    return;
2171
2172
21
  D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
2173
21
}
2174
2175
24
static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2176
24
  uint32_t priority = DestructorAttr::DefaultPriority;
2177
24
  if (AL.getNumArgs() &&
2178
11
      !checkUInt32Argument(S, AL, AL.getArgAsExpr(0), priority))
2179
1
    return;
2180
2181
23
  D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
2182
23
}
2183
2184
template <typename AttrTy>
2185
22.8k
static void handleAttrWithMessage(Sema &S, Decl *D, const ParsedAttr &AL) {
2186
  // Handle the case where the attribute has a text message.
2187
22.8k
  StringRef Str;
2188
22.8k
  if (AL.getNumArgs() == 1 && 
!S.checkStringLiteralArgumentAttr(AL, 0, Str)1.61k
)
2189
1
    return;
2190
2191
22.8k
  D->addAttr(::new (S.Context) AttrTy(S.Context, AL, Str));
2192
22.8k
}
2193
2194
static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D,
2195
107
                                          const ParsedAttr &AL) {
2196
107
  if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) {
2197
1
    S.Diag(AL.getLoc(), diag::err_objc_attr_protocol_requires_definition)
2198
1
        << AL << AL.getRange();
2199
1
    return;
2200
1
  }
2201
2202
106
  D->addAttr(::new (S.Context) ObjCExplicitProtocolImplAttr(S.Context, AL));
2203
106
}
2204
2205
static bool checkAvailabilityAttr(Sema &S, SourceRange Range,
2206
                                  IdentifierInfo *Platform,
2207
                                  VersionTuple Introduced,
2208
                                  VersionTuple Deprecated,
2209
8.07M
                                  VersionTuple Obsoleted) {
2210
8.07M
  StringRef PlatformName
2211
8.07M
    = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
2212
8.07M
  if (PlatformName.empty())
2213
67.7k
    PlatformName = Platform->getName();
2214
2215
  // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
2216
  // of these steps are needed).
2217
8.07M
  if (!Introduced.empty() && 
!Deprecated.empty()6.35M
&&
2218
1.37M
      !(Introduced <= Deprecated)) {
2219
7
    S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2220
7
      << 1 << PlatformName << Deprecated.getAsString()
2221
7
      << 0 << Introduced.getAsString();
2222
7
    return true;
2223
7
  }
2224
2225
8.07M
  if (!Introduced.empty() && 
!Obsoleted.empty()6.35M
&&
2226
31
      !(Introduced <= Obsoleted)) {
2227
0
    S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2228
0
      << 2 << PlatformName << Obsoleted.getAsString()
2229
0
      << 0 << Introduced.getAsString();
2230
0
    return true;
2231
0
  }
2232
2233
8.07M
  if (!Deprecated.empty() && 
!Obsoleted.empty()1.43M
&&
2234
28
      !(Deprecated <= Obsoleted)) {
2235
3
    S.Diag(Range.getBegin(), diag::warn_availability_version_ordering)
2236
3
      << 2 << PlatformName << Obsoleted.getAsString()
2237
3
      << 1 << Deprecated.getAsString();
2238
3
    return true;
2239
3
  }
2240
2241
8.07M
  return false;
2242
8.07M
}
2243
2244
/// Check whether the two versions match.
2245
///
2246
/// If either version tuple is empty, then they are assumed to match. If
2247
/// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y.
2248
static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y,
2249
555k
                          bool BeforeIsOkay) {
2250
555k
  if (X.empty() || 
Y.empty()190k
)
2251
374k
    return true;
2252
2253
181k
  if (X == Y)
2254
180k
    return true;
2255
2256
734
  if (BeforeIsOkay && 
X < Y113
)
2257
25
    return true;
2258
2259
709
  return false;
2260
709
}
2261
2262
AvailabilityAttr *Sema::mergeAvailabilityAttr(
2263
    NamedDecl *D, const AttributeCommonInfo &CI, IdentifierInfo *Platform,
2264
    bool Implicit, VersionTuple Introduced, VersionTuple Deprecated,
2265
    VersionTuple Obsoleted, bool IsUnavailable, StringRef Message,
2266
    bool IsStrict, StringRef Replacement, AvailabilityMergeKind AMK,
2267
8.06M
    int Priority) {
2268
8.06M
  VersionTuple MergedIntroduced = Introduced;
2269
8.06M
  VersionTuple MergedDeprecated = Deprecated;
2270
8.06M
  VersionTuple MergedObsoleted = Obsoleted;
2271
8.06M
  bool FoundAny = false;
2272
8.06M
  bool OverrideOrImpl = false;
2273
8.06M
  switch (AMK) {
2274
7.88M
  case AMK_None:
2275
7.88M
  case AMK_Redeclaration:
2276
7.88M
    OverrideOrImpl = false;
2277
7.88M
    break;
2278
2279
179k
  case AMK_Override:
2280
179k
  case AMK_ProtocolImplementation:
2281
179k
    OverrideOrImpl = true;
2282
179k
    break;
2283
8.06M
  }
2284
2285
8.06M
  if (D->hasAttrs()) {
2286
5.46M
    AttrVec &Attrs = D->getAttrs();
2287
16.3M
    for (unsigned i = 0, e = Attrs.size(); i != e;) {
2288
10.9M
      const auto *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]);
2289
10.9M
      if (!OldAA) {
2290
1.34M
        ++i;
2291
1.34M
        continue;
2292
1.34M
      }
2293
2294
9.56M
      IdentifierInfo *OldPlatform = OldAA->getPlatform();
2295
9.56M
      if (OldPlatform != Platform) {
2296
9.37M
        ++i;
2297
9.37M
        continue;
2298
9.37M
      }
2299
2300
      // If there is an existing availability attribute for this platform that
2301
      // has a lower priority use the existing one and discard the new
2302
      // attribute.
2303
186k
      if (OldAA->getPriority() < Priority)
2304
595
        return nullptr;
2305
2306
      // If there is an existing attribute for this platform that has a higher
2307
      // priority than the new attribute then erase the old one and continue
2308
      // processing the attributes.
2309
185k
      if (OldAA->getPriority() > Priority) {
2310
86
        Attrs.erase(Attrs.begin() + i);
2311
86
        --e;
2312
86
        continue;
2313
86
      }
2314
2315
185k
      FoundAny = true;
2316
185k
      VersionTuple OldIntroduced = OldAA->getIntroduced();
2317
185k
      VersionTuple OldDeprecated = OldAA->getDeprecated();
2318
185k
      VersionTuple OldObsoleted = OldAA->getObsoleted();
2319
185k
      bool OldIsUnavailable = OldAA->getUnavailable();
2320
2321
185k
      if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) ||
2322
185k
          !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) ||
2323
185k
          !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) ||
2324
185k
          !(OldIsUnavailable == IsUnavailable ||
2325
670
            
(10
OverrideOrImpl10
&&
!OldIsUnavailable10
&&
IsUnavailable5
))) {
2326
670
        if (OverrideOrImpl) {
2327
49
          int Which = -1;
2328
49
          VersionTuple FirstVersion;
2329
49
          VersionTuple SecondVersion;
2330
49
          if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) {
2331
39
            Which = 0;
2332
39
            FirstVersion = OldIntroduced;
2333
39
            SecondVersion = Introduced;
2334
10
          } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) {
2335
5
            Which = 1;
2336
5
            FirstVersion = Deprecated;
2337
5
            SecondVersion = OldDeprecated;
2338
5
          } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) {
2339
0
            Which = 2;
2340
0
            FirstVersion = Obsoleted;
2341
0
            SecondVersion = OldObsoleted;
2342
0
          }
2343
2344
49
          if (Which == -1) {
2345
5
            Diag(OldAA->getLocation(),
2346
5
                 diag::warn_mismatched_availability_override_unavail)
2347
5
              << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2348
5
              << (AMK == AMK_Override);
2349
44
          } else {
2350
44
            Diag(OldAA->getLocation(),
2351
44
                 diag::warn_mismatched_availability_override)
2352
44
              << Which
2353
44
              << AvailabilityAttr::getPrettyPlatformName(Platform->getName())
2354
44
              << FirstVersion.getAsString() << SecondVersion.getAsString()
2355
44
              << (AMK == AMK_Override);
2356
44
          }
2357
49
          if (AMK == AMK_Override)
2358
15
            Diag(CI.getLoc(), diag::note_overridden_method);
2359
34
          else
2360
34
            Diag(CI.getLoc(), diag::note_protocol_method);
2361
621
        } else {
2362
621
          Diag(OldAA->getLocation(), diag::warn_mismatched_availability);
2363
621
          Diag(CI.getLoc(), diag::note_previous_attribute);
2364
621
        }
2365
2366
670
        Attrs.erase(Attrs.begin() + i);
2367
670
        --e;
2368
670
        continue;
2369
670
      }
2370
2371
185k
      VersionTuple MergedIntroduced2 = MergedIntroduced;
2372
185k
      VersionTuple MergedDeprecated2 = MergedDeprecated;
2373
185k
      VersionTuple MergedObsoleted2 = MergedObsoleted;
2374
2375
185k
      if (MergedIntroduced2.empty())
2376
19.2k
        MergedIntroduced2 = OldIntroduced;
2377
185k
      if (MergedDeprecated2.empty())
2378
152k
        MergedDeprecated2 = OldDeprecated;
2379
185k
      if (MergedObsoleted2.empty())
2380
185k
        MergedObsoleted2 = OldObsoleted;
2381
2382
185k
      if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform,
2383
185k
                                MergedIntroduced2, MergedDeprecated2,
2384
4
                                MergedObsoleted2)) {
2385
4
        Attrs.erase(Attrs.begin() + i);
2386
4
        --e;
2387
4
        continue;
2388
4
      }
2389
2390
185k
      MergedIntroduced = MergedIntroduced2;
2391
185k
      MergedDeprecated = MergedDeprecated2;
2392
185k
      MergedObsoleted = MergedObsoleted2;
2393
185k
      ++i;
2394
185k
    }
2395
5.46M
  }
2396
2397
8.06M
  if (FoundAny &&
2398
185k
      MergedIntroduced == Introduced &&
2399
181k
      MergedDeprecated == Deprecated &&
2400
168k
      MergedObsoleted == Obsoleted)
2401
168k
    return nullptr;
2402
2403
  // Only create a new attribute if !OverrideOrImpl, but we want to do
2404
  // the checking.
2405
7.89M
  if (!checkAvailabilityAttr(*this, CI.getRange(), Platform, MergedIntroduced,
2406
7.89M
                             MergedDeprecated, MergedObsoleted) &&
2407
7.89M
      !OverrideOrImpl) {
2408
7.88M
    auto *Avail = ::new (Context) AvailabilityAttr(
2409
7.88M
        Context, CI, Platform, Introduced, Deprecated, Obsoleted, IsUnavailable,
2410
7.88M
        Message, IsStrict, Replacement, Priority);
2411
7.88M
    Avail->setImplicit(Implicit);
2412
7.88M
    return Avail;
2413
7.88M
  }
2414
14.0k
  return nullptr;
2415
14.0k
}
2416
2417
7.83M
static void handleAvailabilityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2418
7.83M
  if (!checkAttributeNumArgs(S, AL, 1))
2419
0
    return;
2420
7.83M
  IdentifierLoc *Platform = AL.getArgAsIdent(0);
2421
2422
7.83M
  IdentifierInfo *II = Platform->Ident;
2423
7.83M
  if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty())
2424
67.7k
    S.Diag(Platform->Loc, diag::warn_availability_unknown_platform)
2425
67.7k
      << Platform->Ident;
2426
2427
7.83M
  auto *ND = dyn_cast<NamedDecl>(D);
2428
7.83M
  if (!ND) // We warned about this already, so just return.
2429
0
    return;
2430
2431
7.83M
  AvailabilityChange Introduced = AL.getAvailabilityIntroduced();
2432
7.83M
  AvailabilityChange Deprecated = AL.getAvailabilityDeprecated();
2433
7.83M
  AvailabilityChange Obsoleted = AL.getAvailabilityObsoleted();
2434
7.83M
  bool IsUnavailable = AL.getUnavailableLoc().isValid();
2435
7.83M
  bool IsStrict = AL.getStrictLoc().isValid();
2436
7.83M
  StringRef Str;
2437
7.83M
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getMessageExpr()))
2438
678k
    Str = SE->getString();
2439
7.83M
  StringRef Replacement;
2440
7.83M
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getReplacementExpr()))
2441
106k
    Replacement = SE->getString();
2442
2443
7.83M
  if (II->isStr("swift")) {
2444
41.9k
    if (Introduced.isValid() || 
Obsoleted.isValid()41.9k
||
2445
41.9k
        (!IsUnavailable && 
!Deprecated.isValid()3
)) {
2446
2
      S.Diag(AL.getLoc(),
2447
2
             diag::warn_availability_swift_unavailable_deprecated_only);
2448
2
      return;
2449
2
    }
2450
7.83M
  }
2451
2452
7.83M
  int PriorityModifier = AL.isPragmaClangAttribute()
2453
124k
                             ? Sema::AP_PragmaClangAttribute
2454
7.71M
                             : Sema::AP_Explicit;
2455
7.83M
  AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2456
7.83M
      ND, AL, II, false /*Implicit*/, Introduced.Version, Deprecated.Version,
2457
7.83M
      Obsoleted.Version, IsUnavailable, Str, IsStrict, Replacement,
2458
7.83M
      Sema::AMK_None, PriorityModifier);
2459
7.83M
  if (NewAttr)
2460
7.83M
    D->addAttr(NewAttr);
2461
2462
  // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning
2463
  // matches before the start of the watchOS platform.
2464
7.83M
  if (S.Context.getTargetInfo().getTriple().isWatchOS()) {
2465
547
    IdentifierInfo *NewII = nullptr;
2466
547
    if (II->getName() == "ios")
2467
234
      NewII = &S.Context.Idents.get("watchos");
2468
313
    else if (II->getName() == "ios_app_extension")
2469
0
      NewII = &S.Context.Idents.get("watchos_app_extension");
2470
2471
547
    if (NewII) {
2472
702
        auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple {
2473
702
          if (Version.empty())
2474
464
            return Version;
2475
238
          auto Major = Version.getMajor();
2476
200
          auto NewMajor = Major >= 9 ? 
Major - 738
: 0;
2477
238
          if (NewMajor >= 2) {
2478
38
            if (Version.getMinor().hasValue()) {
2479
37
              if (Version.getSubminor().hasValue())
2480
0
                return VersionTuple(NewMajor, Version.getMinor().getValue(),
2481
0
                                    Version.getSubminor().getValue());
2482
37
              else
2483
37
                return VersionTuple(NewMajor, Version.getMinor().getValue());
2484
1
            }
2485
1
            return VersionTuple(NewMajor);
2486
1
          }
2487
2488
200
          return VersionTuple(2, 0);
2489
200
        };
2490
2491
234
        auto NewIntroduced = adjustWatchOSVersion(Introduced.Version);
2492
234
        auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version);
2493
234
        auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version);
2494
2495
234
        AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2496
234
            ND, AL, NewII, true /*Implicit*/, NewIntroduced, NewDeprecated,
2497
234
            NewObsoleted, IsUnavailable, Str, IsStrict, Replacement,
2498
234
            Sema::AMK_None,
2499
234
            PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2500
234
        if (NewAttr)
2501
232
          D->addAttr(NewAttr);
2502
234
      }
2503
7.83M
  } else if (S.Context.getTargetInfo().getTriple().isTvOS()) {
2504
    // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning
2505
    // matches before the start of the tvOS platform.
2506
576
    IdentifierInfo *NewII = nullptr;
2507
576
    if (II->getName() == "ios")
2508
235
      NewII = &S.Context.Idents.get("tvos");
2509
341
    else if (II->getName() == "ios_app_extension")
2510
3
      NewII = &S.Context.Idents.get("tvos_app_extension");
2511
2512
576
    if (NewII) {
2513
238
      AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(
2514
238
          ND, AL, NewII, true /*Implicit*/, Introduced.Version,
2515
238
          Deprecated.Version, Obsoleted.Version, IsUnavailable, Str, IsStrict,
2516
238
          Replacement, Sema::AMK_None,
2517
238
          PriorityModifier + Sema::AP_InferredFromOtherPlatform);
2518
238
      if (NewAttr)
2519
231
        D->addAttr(NewAttr);
2520
238
      }
2521
576
  }
2522
7.83M
}
2523
2524
static void handleExternalSourceSymbolAttr(Sema &S, Decl *D,
2525
71
                                           const ParsedAttr &AL) {
2526
71
  if (!checkAttributeAtLeastNumArgs(S, AL, 1))
2527
3
    return;
2528
68
  assert(checkAttributeAtMostNumArgs(S, AL, 3) &&
2529
68
         "Invalid number of arguments in an external_source_symbol attribute");
2530
2531
68
  StringRef Language;
2532
68
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(0)))
2533
57
    Language = SE->getString();
2534
68
  StringRef DefinedIn;
2535
68
  if (const auto *SE = dyn_cast_or_null<StringLiteral>(AL.getArgAsExpr(1)))
2536
55
    DefinedIn = SE->getString();
2537
68
  bool IsGeneratedDeclaration = AL.getArgAsIdent(2) != nullptr;
2538
2539
68
  D->addAttr(::new (S.Context) ExternalSourceSymbolAttr(
2540
68
      S.Context, AL, Language, DefinedIn, IsGeneratedDeclaration));
2541
68
}
2542
2543
template <class T>
2544
static T *mergeVisibilityAttr(Sema &S, Decl *D, const AttributeCommonInfo &CI,
2545
1.83M
                              typename T::VisibilityType value) {
2546
1.83M
  T *existingAttr = D->getAttr<T>();
2547
1.83M
  if (existingAttr) {
2548
36.4k
    typename T::VisibilityType existingValue = existingAttr->getVisibility();
2549
36.4k
    if (existingValue == value)
2550
36.4k
      return nullptr;
2551
6
    S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2552
6
    S.Diag(CI.getLoc(), diag::note_previous_attribute);
2553
6
    D->dropAttr<T>();
2554
6
  }
2555
1.79M
  return ::new (S.Context) T(S.Context, CI, value);
2556
1.83M
}
SemaDeclAttr.cpp:clang::VisibilityAttr* mergeVisibilityAttr<clang::VisibilityAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, clang::VisibilityAttr::VisibilityType)
Line
Count
Source
2545
1.68M
                              typename T::VisibilityType value) {
2546
1.68M
  T *existingAttr = D->getAttr<T>();
2547
1.68M
  if (existingAttr) {
2548
27.2k
    typename T::VisibilityType existingValue = existingAttr->getVisibility();
2549
27.2k
    if (existingValue == value)
2550
27.2k
      return nullptr;
2551
6
    S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2552
6
    S.Diag(CI.getLoc(), diag::note_previous_attribute);
2553
6
    D->dropAttr<T>();
2554
6
  }
2555
1.65M
  return ::new (S.Context) T(S.Context, CI, value);
2556
1.68M
}
SemaDeclAttr.cpp:clang::TypeVisibilityAttr* mergeVisibilityAttr<clang::TypeVisibilityAttr>(clang::Sema&, clang::Decl*, clang::AttributeCommonInfo const&, clang::TypeVisibilityAttr::VisibilityType)
Line
Count
Source
2545
151k
                              typename T::VisibilityType value) {
2546
151k
  T *existingAttr = D->getAttr<T>();
2547
151k
  if (existingAttr) {
2548
9.21k
    typename T::VisibilityType existingValue = existingAttr->getVisibility();
2549
9.21k
    if (existingValue == value)
2550
9.21k
      return nullptr;
2551
0
    S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility);
2552
0
    S.Diag(CI.getLoc(), diag::note_previous_attribute);
2553
0
    D->dropAttr<T>();
2554
0
  }
2555
141k
  return ::new (S.Context) T(S.Context, CI, value);
2556
151k
}
2557
2558
VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D,
2559
                                          const AttributeCommonInfo &CI,
2560
1.68M
                                          VisibilityAttr::VisibilityType Vis) {
2561
1.68M
  return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, CI, Vis);
2562
1.68M
}
2563
2564
TypeVisibilityAttr *
2565
Sema::mergeTypeVisibilityAttr(Decl *D, const AttributeCommonInfo &CI,
2566
151k
                              TypeVisibilityAttr::VisibilityType Vis) {
2567
151k
  return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, CI, Vis);
2568
151k
}
2569
2570
static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
2571
1.72M
                                 bool isTypeVisibility) {
2572
  // Visibility attributes don't mean anything on a typedef.
2573
1.72M
  if (isa<TypedefNameDecl>(D)) {
2574
1
    S.Diag(AL.getRange().getBegin(), diag::warn_attribute_ignored) << AL;
2575
1
    return;
2576
1
  }
2577
2578
  // 'type_visibility' can only go on a type or namespace.
2579
1.72M
  if (isTypeVisibility &&
2580
140k
      !(isa<TagDecl>(D) ||
2581
1
        isa<ObjCInterfaceDecl>(D) ||
2582
1
        isa<NamespaceDecl>(D))) {
2583
1
    S.Diag(AL.getRange().getBegin(), diag::err_attribute_wrong_decl_type)
2584
1
        << AL << ExpectedTypeOrNamespace;
2585
1
    return;
2586
1
  }
2587
2588
  // Check that the argument is a string literal.
2589
1.72M
  StringRef TypeStr;
2590
1.72M
  SourceLocation LiteralLoc;
2591
1.72M
  if (!S.checkStringLiteralArgumentAttr(AL, 0, TypeStr, &LiteralLoc))
2592
0
    return;
2593
2594
1.72M
  VisibilityAttr::VisibilityType type;
2595
1.72M
  if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) {
2596
0
    S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) << AL
2597
0
                                                                << TypeStr;
2598
0
    return;
2599
0
  }
2600
2601
  // Complain about attempts to use protected visibility on targets
2602
  // (like Darwin) that don't support it.
2603
1.72M
  if (type == VisibilityAttr::Protected &&
2604
59
      !S.Context.getTargetInfo().hasProtectedVisibility()) {
2605
3
    S.Diag(AL.getLoc(), diag::warn_attribute_protected_visibility);
2606
3
    type = VisibilityAttr::Default;
2607
3
  }
2608
2609
1.72M
  Attr *newAttr;
2610
1.72M
  if (isTypeVisibility) {
2611
140k
    newAttr = S.mergeTypeVisibilityAttr(
2612
140k
        D, AL, (TypeVisibilityAttr::VisibilityType)type);
2613
1.58M
  } else {
2614
1.58M
    newAttr = S.mergeVisibilityAttr(D, AL, type);
2615
1.58M
  }
2616
1.72M
  if (newAttr)
2617
1.72M
    D->addAttr(newAttr);
2618
1.72M
}
2619
2620
static void handleObjCNonRuntimeProtocolAttr(Sema &S, Decl *D,
2621
21
                                             const ParsedAttr &AL) {
2622
21
  handleSimpleAttribute<ObjCNonRuntimeProtocolAttr>(S, D, AL);
2623
21
}
2624
2625
110
static void handleObjCDirectAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2626
  // objc_direct cannot be set on methods declared in the context of a protocol
2627
110
  if (isa<ObjCProtocolDecl>(D->getDeclContext())) {
2628
2
    S.Diag(AL.getLoc(), diag::err_objc_direct_on_protocol) << false;
2629
2
    return;
2630
2
  }
2631
2632
108
  if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2633
108
    handleSimpleAttribute<ObjCDirectAttr>(S, D, AL);
2634
0
  } else {
2635
0
    S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2636
0
  }
2637
108
}
2638
2639
static void handleObjCDirectMembersAttr(Sema &S, Decl *D,
2640
8
                                        const ParsedAttr &AL) {
2641
8
  if (S.getLangOpts().ObjCRuntime.allowsDirectDispatch()) {
2642
8
    handleSimpleAttribute<ObjCDirectMembersAttr>(S, D, AL);
2643
0
  } else {
2644
0
    S.Diag(AL.getLoc(), diag::warn_objc_direct_ignored) << AL;
2645
0
  }
2646
8
}
2647
2648
18
static void handleObjCMethodFamilyAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2649
18
  const auto *M = cast<ObjCMethodDecl>(D);
2650
18
  if (!AL.isArgIdent(0)) {
2651
2
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2652
2
        << AL << 1 << AANT_ArgumentIdentifier;
2653
2
    return;
2654
2
  }
2655
2656
16
  IdentifierLoc *IL = AL.getArgAsIdent(0);
2657
16
  ObjCMethodFamilyAttr::FamilyKind F;
2658
16
  if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) {
2659
0
    S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << AL << IL->Ident;
2660
0
    return;
2661
0
  }
2662
2663
16
  if (F == ObjCMethodFamilyAttr::OMF_init &&
2664
14
      !M->getReturnType()->isObjCObjectPointerType()) {
2665
0
    S.Diag(M->getLocation(), diag::err_init_method_bad_return_type)
2666
0
        << M->getReturnType();
2667
    // Ignore the attribute.
2668
0
    return;
2669
0
  }
2670
2671
16
  D->addAttr(new (S.Context) ObjCMethodFamilyAttr(S.Context, AL, F));
2672
16
}
2673
2674
15
static void handleObjCNSObject(Sema &S, Decl *D, const ParsedAttr &AL) {
2675
15
  if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
2676
9
    QualType T = TD->getUnderlyingType();
2677
9
    if (!T->isCARCBridgableType()) {
2678
1
      S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
2679
1
      return;
2680
1
    }
2681
6
  }
2682
6
  else if (const auto *PD = dyn_cast<ObjCPropertyDecl>(D)) {
2683
4
    QualType T = PD->getType();
2684
4
    if (!T->isCARCBridgableType()) {
2685
1
      S.Diag(PD->getLocation(), diag::err_nsobject_attribute);
2686
1
      return;
2687
1
    }
2688
2
  }
2689
2
  else {
2690
    // It is okay to include this attribute on properties, e.g.:
2691
    //
2692
    //  @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
2693
    //
2694
    // In this case it follows tradition and suppresses an error in the above
2695
    // case.
2696
2
    S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
2697
2
  }
2698
13
  D->addAttr(::new (S.Context) ObjCNSObjectAttr(S.Context, AL));
2699
13
}
2700
2701
5.83k
static void handleObjCIndependentClass(Sema &S, Decl *D, const ParsedAttr &AL) {
2702
5.83k
  if (const auto *TD = dyn_cast<TypedefNameDecl>(D)) {
2703
5.83k
    QualType T = TD->getUnderlyingType();
2704
5.83k
    if (!T->isObjCObjectPointerType()) {
2705
1
      S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute);
2706
1
      return;
2707
1
    }
2708
4
  } else {
2709
4
    S.Diag(D->getLocation(), diag::warn_independentclass_attribute);
2710
4
    return;
2711
4
  }
2712
5.83k
  D->addAttr(::new (S.Context) ObjCIndependentClassAttr(S.Context, AL));
2713
5.83k
}
2714
2715
433
static void handleBlocksAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2716
433
  if (!AL.isArgIdent(0)) {
2717
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2718
0
        << AL << 1 << AANT_ArgumentIdentifier;
2719
0
    return;
2720
0
  }
2721
2722
433
  IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
2723
433
  BlocksAttr::BlockType type;
2724
433
  if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) {
2725
0
    S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
2726
0
    return;
2727
0
  }
2728
2729
433
  D->addAttr(::new (S.Context) BlocksAttr(S.Context, AL, type));
2730
433
}
2731
2732
2.55k
static void handleSentinelAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2733
2.55k
  unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel;
2734
2.55k
  if (AL.getNumArgs() > 0) {
2735
2.51k
    Expr *E = AL.getArgAsExpr(0);
2736
2.51k
    Optional<llvm::APSInt> Idx = llvm::APSInt(32);
2737
2.51k
    if (E->isTypeDependent() || E->isValueDependent() ||
2738
2.51k
        !(Idx = E->getIntegerConstantExpr(S.Context))) {
2739
4
      S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2740
4
          << AL << 1 << AANT_ArgumentIntegerConstant << E->getSourceRange();
2741
4
      return;
2742
4
    }
2743
2744
2.51k
    if (Idx->isSigned() && Idx->isNegative()) {
2745
4
      S.Diag(AL.getLoc(), diag::err_attribute_sentinel_less_than_zero)
2746
4
        << E->getSourceRange();
2747
4
      return;
2748
4
    }
2749
2750
2.51k
    sentinel = Idx->getZExtValue();
2751
2.51k
  }
2752
2753
2.54k
  unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos;
2754
2.54k
  if (AL.getNumArgs() > 1) {
2755
2.46k
    Expr *E = AL.getArgAsExpr(1);
2756
2.46k
    Optional<llvm::APSInt> Idx = llvm::APSInt(32);
2757
2.46k
    if (E->isTypeDependent() || E->isValueDependent() ||
2758
2.46k
        !(Idx = E->getIntegerConstantExpr(S.Context))) {
2759
0
      S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
2760
0
          << AL << 2 << AANT_ArgumentIntegerConstant << E->getSourceRange();
2761
0
      return;
2762
0
    }
2763
2.46k
    nullPos = Idx->getZExtValue();
2764
2765
2.46k
    if ((Idx->isSigned() && Idx->isNegative()) || nullPos > 1) {
2766
      // FIXME: This error message could be improved, it would be nice
2767
      // to say what the bounds actually are.
2768
3
      S.Diag(AL.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
2769
3
        << E->getSourceRange();
2770
3
      return;
2771
3
    }
2772
2.54k
  }
2773
2774
2.54k
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2775
61
    const FunctionType *FT = FD->getType()->castAs<FunctionType>();
2776
61
    if (isa<FunctionNoProtoType>(FT)) {
2777
2
      S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_named_arguments);
2778
2
      return;
2779
2
    }
2780
2781
59
    if (!cast<FunctionProtoType>(FT)->isVariadic()) {
2782
4
      S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
2783
4
      return;
2784
4
    }
2785
2.48k
  } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
2786
2.46k
    if (!MD->isVariadic()) {
2787
1
      S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
2788
1
      return;
2789
1
    }
2790
16
  } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
2791
4
    if (!BD->isVariadic()) {
2792
1
      S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
2793
1
      return;
2794
1
    }
2795
12
  } else if (const auto *V = dyn_cast<VarDecl>(D)) {
2796
12
    QualType Ty = V->getType();
2797
12
    if (Ty->isBlockPointerType() || 
Ty->isFunctionPointerType()7
) {
2798
9
      const FunctionType *FT = Ty->isFunctionPointerType()
2799
4
       ? D->getFunctionType()
2800
5
       : Ty->castAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
2801
9
      if (!cast<FunctionProtoType>(FT)->isVariadic()) {
2802
1
        int m = Ty->isFunctionPointerType() ? 
00
: 1;
2803
1
        S.Diag(AL.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
2804
1
        return;
2805
1
      }
2806
3
    } else {
2807
3
      S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2808
3
          << AL << ExpectedFunctionMethodOrBlock;
2809
3
      return;
2810
3
    }
2811
0
  } else {
2812
0
    S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2813
0
        << AL << ExpectedFunctionMethodOrBlock;
2814
0
    return;
2815
0
  }
2816
2.53k
  D->addAttr(::new (S.Context) SentinelAttr(S.Context, AL, sentinel, nullPos));
2817
2.53k
}
2818
2819
48.3k
static void handleWarnUnusedResult(Sema &S, Decl *D, const ParsedAttr &AL) {
2820
48.3k
  if (D->getFunctionType() &&
2821
48.2k
      D->getFunctionType()->getReturnType()->isVoidType() &&
2822
10
      !isa<CXXConstructorDecl>(D)) {
2823
1
    S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 0;
2824
1
    return;
2825
1
  }
2826
48.3k
  if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
2827
79
    if (MD->getReturnType()->isVoidType()) {
2828
1
      S.Diag(AL.getLoc(), diag::warn_attribute_void_function_method) << AL << 1;
2829
1
      return;
2830
1
    }
2831
2832
48.3k
  StringRef Str;
2833
48.3k
  if ((AL.isCXX11Attribute() || 
AL.isC2xAttribute()48.3k
) &&
!AL.getScopeName()100
) {
2834
    // The standard attribute cannot be applied to variable declarations such
2835
    // as a function pointer.
2836
86
    if (isa<VarDecl>(D))
2837
5
      S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type_str)
2838
5
          << AL << "functions, classes, or enumerations";
2839
2840
    // If this is spelled as the standard C++17 attribute, but not in C++17,
2841
    // warn about using it as an extension. If there are attribute arguments,
2842
    // then claim it's a C++2a extension instead.
2843
    // FIXME: If WG14 does not seem likely to adopt the same feature, add an
2844
    // extension warning for C2x mode.
2845
86
    const LangOptions &LO = S.getLangOpts();
2846
86
    if (AL.getNumArgs() == 1) {
2847
29
      if (LO.CPlusPlus && 
!LO.CPlusPlus2028
)
2848
18
        S.Diag(AL.getLoc(), diag::ext_cxx20_attr) << AL;
2849
2850
      // Since this this is spelled [[nodiscard]], get the optional string
2851
      // literal. If in C++ mode, but not in C++2a mode, diagnose as an
2852
      // extension.
2853
      // FIXME: C2x should support this feature as well, even as an extension.
2854
29
      if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, nullptr))
2855
0
        return;
2856
57
    } else if (LO.CPlusPlus && 
!LO.CPlusPlus1746
)
2857
13
      S.Diag(AL.getLoc(), diag::ext_cxx17_attr) << AL;
2858
86
  }
2859
2860
48.3k
  D->addAttr(::new (S.Context) WarnUnusedResultAttr(S.Context, AL, Str));
2861
48.3k
}
2862
2863
968
static void handleWeakImportAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2864
  // weak_import only applies to variable & function declarations.
2865
968
  bool isDef = false;
2866
968
  if (!D->canBeWeakImported(isDef)) {
2867
9
    if (isDef)
2868
3
      S.Diag(AL.getLoc(), diag::warn_attribute_invalid_on_definition)
2869
3
        << "weak_import";
2870
6
    else if (isa<ObjCPropertyDecl>(D) || 
isa<ObjCMethodDecl>(D)5
||
2871
3
             (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
2872
5
              
(3
isa<ObjCInterfaceDecl>(D)3
||
isa<EnumDecl>(D)3
))) {
2873
      // Nothing to warn about here.
2874
5
    } else
2875
1
      S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
2876
1
          << AL << ExpectedVariableOrFunction;
2877
2878
9
    return;
2879
9
  }
2880
2881
959
  D->addAttr(::new (S.Context) WeakImportAttr(S.Context, AL));
2882
959
}
2883
2884
// Handles reqd_work_group_size and work_group_size_hint.
2885
template <typename WorkGroupAttr>
2886
20
static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
2887
20
  uint32_t WGSize[3];
2888
71
  for (unsigned i = 0; i < 3; 
++i51
) {
2889
56
    const Expr *E = AL.getArgAsExpr(i);
2890
56
    if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2891
56
                             /*StrictlyUnsigned=*/true))
2892
2
      return;
2893
54
    if (WGSize[i] == 0) {
2894
3
      S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2895
3
          << AL << E->getSourceRange();
2896
3
      return;
2897
3
    }
2898
54
  }
2899
2900
15
  WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2901
15
  if (Existing && 
!(1
Existing->getXDim() == WGSize[0]1
&&
2902
0
                    Existing->getYDim() == WGSize[1] &&
2903
0
                    Existing->getZDim() == WGSize[2]))
2904
1
    S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
2905
2906
15
  D->addAttr(::new (S.Context)
2907
15
                 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
2908
15
}
SemaDeclAttr.cpp:void handleWorkGroupSize<clang::WorkGroupSizeHintAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
2886
6
static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
2887
6
  uint32_t WGSize[3];
2888
22
  for (unsigned i = 0; i < 3; 
++i16
) {
2889
17
    const Expr *E = AL.getArgAsExpr(i);
2890
17
    if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2891
17
                             /*StrictlyUnsigned=*/true))
2892
1
      return;
2893
16
    if (WGSize[i] == 0) {
2894
0
      S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2895
0
          << AL << E->getSourceRange();
2896
0
      return;
2897
0
    }
2898
16
  }
2899
2900
5
  WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2901
5
  if (Existing && 
!(1
Existing->getXDim() == WGSize[0]1
&&
2902
0
                    Existing->getYDim() == WGSize[1] &&
2903
0
                    Existing->getZDim() == WGSize[2]))
2904
1
    S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
2905
2906
5
  D->addAttr(::new (S.Context)
2907
5
                 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
2908
5
}
SemaDeclAttr.cpp:void handleWorkGroupSize<clang::ReqdWorkGroupSizeAttr>(clang::Sema&, clang::Decl*, clang::ParsedAttr const&)
Line
Count
Source
2886
14
static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
2887
14
  uint32_t WGSize[3];
2888
49
  for (unsigned i = 0; i < 3; 
++i35
) {
2889
39
    const Expr *E = AL.getArgAsExpr(i);
2890
39
    if (!checkUInt32Argument(S, AL, E, WGSize[i], i,
2891
39
                             /*StrictlyUnsigned=*/true))
2892
1
      return;
2893
38
    if (WGSize[i] == 0) {
2894
3
      S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2895
3
          << AL << E->getSourceRange();
2896
3
      return;
2897
3
    }
2898
38
  }
2899
2900
10
  WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>();
2901
10
  if (Existing && 
!(0
Existing->getXDim() == WGSize[0]0
&&
2902
0
                    Existing->getYDim() == WGSize[1] &&
2903
0
                    Existing->getZDim() == WGSize[2]))
2904
0
    S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
2905
2906
10
  D->addAttr(::new (S.Context)
2907
10
                 WorkGroupAttr(S.Context, AL, WGSize[0], WGSize[1], WGSize[2]));
2908
10
}
2909
2910
// Handles intel_reqd_sub_group_size.
2911
6
static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
2912
6
  uint32_t SGSize;
2913
6
  const Expr *E = AL.getArgAsExpr(0);
2914
6
  if (!checkUInt32Argument(S, AL, E, SGSize))
2915
0
    return;
2916
6
  if (SGSize == 0) {
2917
1
    S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2918
1
        << AL << E->getSourceRange();
2919
1
    return;
2920
1
  }
2921
2922
5
  OpenCLIntelReqdSubGroupSizeAttr *Existing =
2923
5
      D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>();
2924
5
  if (Existing && 
Existing->getSubGroupSize() != SGSize1
)
2925
1
    S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
2926
2927
5
  D->addAttr(::new (S.Context)
2928
5
                 OpenCLIntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
2929
5
}
2930
2931
8
static void handleVecTypeHint(Sema &S, Decl *D, const ParsedAttr &AL) {
2932
8
  if (!AL.hasParsedType()) {
2933
0
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
2934
0
    return;
2935
0
  }
2936
2937
8
  TypeSourceInfo *ParmTSI = nullptr;
2938
8
  QualType ParmType = S.GetTypeFromParser(AL.getTypeArg(), &ParmTSI);
2939
8
  assert(ParmTSI && "no type source info for attribute argument");
2940
2941
8
  if (!ParmType->isExtVectorType() && 
!ParmType->isFloatingType()7
&&
2942
6
      (ParmType->isBooleanType() ||
2943
5
       !ParmType->isIntegralType(S.getASTContext()))) {
2944
2
    S.Diag(AL.getLoc(), diag::err_attribute_invalid_argument) << 2 << AL;
2945
2
    return;
2946
2
  }
2947
2948
6
  if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) {
2949
1
    if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) {
2950
1
      S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
2951
1
      return;
2952
1
    }
2953
5
  }
2954
2955
5
  D->addAttr(::new (S.Context) VecTypeHintAttr(S.Context, AL, ParmTSI));
2956
5
}
2957
2958
SectionAttr *Sema::mergeSectionAttr(Decl *D, const AttributeCommonInfo &CI,
2959
1.22k
                                    StringRef Name) {
2960
  // Explicit or partial specializations do not inherit
2961
  // the section attribute from the primary template.
2962
1.22k
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2963
42
    if (CI.getAttributeSpellingListIndex() == SectionAttr::Declspec_allocate &&
2964
3
        FD->isFunctionTemplateSpecialization())
2965
3
      return nullptr;
2966
1.22k
  }
2967
1.22k
  if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) {
2968
4
    if (ExistingAttr->getName() == Name)
2969
0
      return nullptr;
2970
4
    Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
2971
4
         << 1 /*section*/;
2972
4
    Diag(CI.getLoc(), diag::note_previous_attribute);
2973
4
    return nullptr;
2974
4
  }
2975
1.22k
  return ::new (Context) SectionAttr(Context, CI, Name);
2976
1.22k
}
2977
2978
1.25k
bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) {
2979
1.25k
  std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName);
2980
1.25k
  if (!Error.empty()) {
2981
2
    Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error
2982
2
         << 1 /*'section'*/;
2983
2
    return false;
2984
2
  }
2985
1.25k
  return true;
2986
1.25k
}
2987
2988
1.21k
static void handleSectionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2989
  // Make sure that there is a string literal as the sections's single
2990
  // argument.
2991
1.21k
  StringRef Str;
2992
1.21k
  SourceLocation LiteralLoc;
2993
1.21k
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
2994
2
    return;
2995
2996
1.21k
  if (!S.checkSectionName(LiteralLoc, Str))
2997
1
    return;
2998
2999
  // If the target wants to validate the section specifier, make it happen.
3000
1.21k
  std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str);
3001
1.21k
  if (!Error.empty()) {
3002
0
    S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3003
0
    << Error;
3004
0
    return;
3005
0
  }
3006
3007
1.21k
  SectionAttr *NewAttr = S.mergeSectionAttr(D, AL, Str);
3008
1.21k
  if (NewAttr)
3009
1.21k
    D->addAttr(NewAttr);
3010
1.21k
}
3011
3012
// This is used for `__declspec(code_seg("segname"))` on a decl.
3013
// `#pragma code_seg("segname")` uses checkSectionName() instead.
3014
static bool checkCodeSegName(Sema &S, SourceLocation LiteralLoc,
3015
82
                             StringRef CodeSegName) {
3016
82
  std::string Error =
3017
82
      S.Context.getTargetInfo().isValidSectionSpecifier(CodeSegName);
3018
82
  if (!Error.empty()) {
3019
0
    S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target)
3020
0
        << Error << 0 /*'code-seg'*/;
3021
0
    return false;
3022
0
  }
3023
3024
82
  return true;
3025
82
}
3026
3027
CodeSegAttr *Sema::mergeCodeSegAttr(Decl *D, const AttributeCommonInfo &CI,
3028
93
                                    StringRef Name) {
3029
  // Explicit or partial specializations do not inherit
3030
  // the code_seg attribute from the primary template.
3031
93
  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3032
46
    if (FD->isFunctionTemplateSpecialization())
3033
3
      return nullptr;
3034
90
  }
3035
90
  if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3036
14
    if (ExistingAttr->getName() == Name)
3037
12
      return nullptr;
3038
2
    Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section)
3039
2
         << 0 /*codeseg*/;
3040
2
    Diag(CI.getLoc(), diag::note_previous_attribute);
3041
2
    return nullptr;
3042
2
  }
3043
76
  return ::new (Context) CodeSegAttr(Context, CI, Name);
3044
76
}
3045
3046
82
static void handleCodeSegAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3047
82
  StringRef Str;
3048
82
  SourceLocation LiteralLoc;
3049
82
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc))
3050
0
    return;
3051
82
  if (!checkCodeSegName(S, LiteralLoc, Str))
3052
0
    return;
3053
82
  if (const auto *ExistingAttr = D->getAttr<CodeSegAttr>()) {
3054
14
    if (!ExistingAttr->isImplicit()) {
3055
6
      S.Diag(AL.getLoc(),
3056
6
             ExistingAttr->getName() == Str
3057
3
             ? diag::warn_duplicate_codeseg_attribute
3058
3
             : diag::err_conflicting_codeseg_attribute);
3059
6
      return;
3060
6
    }
3061
8
    D->dropAttr<CodeSegAttr>();
3062
8
  }
3063
76
  if (CodeSegAttr *CSA = S.mergeCodeSegAttr(D, AL, Str))
3064
76
    D->addAttr(CSA);
3065
76
}
3066
3067
// Check for things we'd like to warn about. Multiversioning issues are
3068
// handled later in the process, once we know how many exist.
3069
1.08M
bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) {
3070
1.08M
  enum FirstParam { Unsupported, Duplicate, Unknown };
3071
1.08M
  enum SecondParam { None, Architecture, Tune };
3072
1.08M
  if (AttrStr.find("fpmath=") != StringRef::npos)
3073
2
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3074
2
           << Unsupported << None << "fpmath=";
3075
3076
  // Diagnose use of tune if target doesn't support it.
3077
1.08M
  if (!Context.getTargetInfo().supportsTargetAttributeTune() &&
3078
725
      AttrStr.find("tune=") != StringRef::npos)
3079
1
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3080
1
           << Unsupported << None << "tune=";
3081
3082
1.08M
  ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr);
3083
3084
1.08M
  if (!ParsedAttrs.Architecture.empty() &&
3085
156
      !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture))
3086
3
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3087
3
           << Unknown << Architecture << ParsedAttrs.Architecture;
3088
3089
1.08M
  if (!ParsedAttrs.Tune.empty() &&
3090
3
      !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Tune))
3091
1
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3092
1
           << Unknown << Tune << ParsedAttrs.Tune;
3093
3094
1.08M
  if (ParsedAttrs.DuplicateArchitecture)
3095
1
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3096
1
           << Duplicate << None << "arch=";
3097
1.08M
  if (ParsedAttrs.DuplicateTune)
3098
0
    return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3099
0
           << Duplicate << None << "tune=";
3100
3101
1.27M
  
for (const auto &Feature : ParsedAttrs.Features)1.08M
{
3102
1.27M
    auto CurFeature = StringRef(Feature).drop_front(); // remove + or -.
3103
1.27M
    if (!Context.getTargetInfo().isValidFeatureName(CurFeature))
3104
4
      return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3105
4
             << Unsupported << None << CurFeature;
3106
1.27M
  }
3107
3108
1.08M
  TargetInfo::BranchProtectionInfo BPI;
3109
1.08M
  StringRef Error;
3110
1.08M
  if (!ParsedAttrs.BranchProtection.empty() &&
3111
19
      !Context.getTargetInfo().validateBranchProtection(
3112
8
          ParsedAttrs.BranchProtection, BPI, Error)) {
3113
8
    if (Error.empty())
3114
1
      return Diag(LiteralLoc, diag::warn_unsupported_target_attribute)
3115
1
             << Unsupported << None << "branch-protection";
3116
7
    else
3117
7
      return Diag(LiteralLoc, diag::err_invalid_branch_protection_spec)
3118
7
             << Error;
3119
1.08M
  }
3120
3121
1.08M
  return false;
3122
1.08M
}
3123
3124
1.08M
static void handleTargetAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3125
1.08M
  StringRef Str;
3126
1.08M
  SourceLocation LiteralLoc;
3127
1.08M
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str, &LiteralLoc) ||
3128
1.08M
      S.checkTargetAttr(LiteralLoc, Str))
3129
20
    return;
3130
3131
1.08M
  TargetAttr *NewAttr = ::new (S.Context) TargetAttr(S.Context, AL, Str);
3132
1.08M
  D->addAttr(NewAttr);
3133
1.08M
}
3134
3135
1.03M
static void handleMinVectorWidthAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3136
1.03M
  Expr *E = AL.getArgAsExpr(0);
3137
1.03M
  uint32_t VecWidth;
3138
1.03M
  if (!checkUInt32Argument(S, AL, E, VecWidth)) {
3139
1
    AL.setInvalid();
3140
1
    return;
3141
1
  }
3142
3143
1.03M
  MinVectorWidthAttr *Existing = D->getAttr<MinVectorWidthAttr>();
3144
1.03M
  if (Existing && 
Existing->getVectorWidth() != VecWidth1
) {
3145
1
    S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
3146
1
    return;
3147
1
  }
3148
3149
1.03M
  D->addAttr(::new (S.Context) MinVectorWidthAttr(S.Context, AL, VecWidth));
3150
1.03M
}
3151
3152
37
static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3153
37
  Expr *E = AL.getArgAsExpr(0);
3154
37
  SourceLocation Loc = E->getExprLoc();
3155
37
  FunctionDecl *FD = nullptr;
3156
37
  DeclarationNameInfo NI;
3157
3158
  // gcc only allows for simple identifiers. Since we support more than gcc, we
3159
  // will warn the user.
3160
37
  if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
3161
34
    if (DRE->hasQualifier())
3162
4
      S.Diag(Loc, diag::warn_cleanup_ext);
3163
34
    FD = dyn_cast<FunctionDecl>(DRE->getDecl());
3164
34
    NI = DRE->getNameInfo();
3165
34
    if (!FD) {
3166
1
      S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1
3167
1
        << NI.getName();
3168
1
      return;
3169
1
    }
3170
3
  } else if (auto *ULE = dyn_cast<UnresolvedLookupExpr>(E)) {
3171
2
    if (ULE->hasExplicitTemplateArgs())
3172
1
      S.Diag(Loc, diag::warn_cleanup_ext);
3173
2
    FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true);
3174
2
    NI = ULE->getNameInfo();
3175
2
    if (!FD) {
3176
1
      S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2
3177
1
        << NI.getName();
3178
1
      if (ULE->getType() == S.Context.OverloadTy)
3179
1
        S.NoteAllOverloadCandidates(ULE);
3180
1
      return;
3181
1
    }
3182
1
  } else {
3183
1
    S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0;
3184
1
    return;
3185
1
  }
3186
3187
34
  if (FD->getNumParams() != 1) {
3188
1
    S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg)
3189
1
      << NI.getName();
3190
1
    return;
3191
1
  }
3192
3193
  // We're currently more strict than GCC about what function types we accept.
3194
  // If this ever proves to be a problem it should be easy to fix.
3195
33
  QualType Ty = S.Context.getPointerType(cast<VarDecl>(D)->getType());
3196
33
  QualType ParamTy = FD->getParamDecl(0)->getType();
3197
33
  if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
3198
1
                                   ParamTy, Ty) != Sema::Compatible) {
3199
1
    S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type)
3200
1
      << NI.getName() << ParamTy << Ty;
3201
1
    return;
3202
1
  }
3203
3204
32
  D->addAttr(::new (S.Context) CleanupAttr(S.Context, AL, FD));
3205
32
}
3206
3207
static void handleEnumExtensibilityAttr(Sema &S, Decl *D,
3208
129k
                                        const ParsedAttr &AL) {
3209
129k
  if (!AL.isArgIdent(0)) {
3210
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3211
0
        << AL << 0 << AANT_ArgumentIdentifier;
3212
0
    return;
3213
0
  }
3214
3215
129k
  EnumExtensibilityAttr::Kind ExtensibilityKind;
3216
129k
  IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
3217
129k
  if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(),
3218
1
                                               ExtensibilityKind)) {
3219
1
    S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported) << AL << II;
3220
1
    return;
3221
1
  }
3222
3223
129k
  D->addAttr(::new (S.Context)
3224
129k
                 EnumExtensibilityAttr(S.Context, AL, ExtensibilityKind));
3225
129k
}
3226
3227
/// Handle __attribute__((format_arg((idx)))) attribute based on
3228
/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3229
1.00k
static void handleFormatArgAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3230
1.00k
  Expr *IdxExpr = AL.getArgAsExpr(0);
3231
1.00k
  ParamIdx Idx;
3232
1.00k
  if (!checkFunctionOrMethodParameterIndex(S, D, AL, 1, IdxExpr, Idx))
3233
2
    return;
3234
3235
  // Make sure the format string is really a string.
3236
1.00k
  QualType Ty = getFunctionOrMethodParamType(D, Idx.getASTIndex());
3237
3238
1.00k
  bool NotNSStringTy = !isNSStringType(Ty, S.Context);
3239
1.00k
  if (NotNSStringTy &&
3240
703
      !isCFStringType(Ty, S.Context) &&
3241
408
      (!Ty->isPointerType() ||
3242
406
       !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
3243
2
    S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3244
2
        << "a string type" << IdxExpr->getSourceRange()
3245
2
        << getFunctionOrMethodParamRange(D, 0);
3246
2
    return;
3247
2
  }
3248
1.00k
  Ty = getFunctionOrMethodResultType(D);
3249
1.00k
  if (!isNSStringType(Ty, S.Context) &&
3250
702
      !isCFStringType(Ty, S.Context) &&
3251
112
      (!Ty->isPointerType() ||
3252
111
       !Ty->castAs<PointerType>()->getPointeeType()->isCharType())) {
3253
1
    S.Diag(AL.getLoc(), diag::err_format_attribute_result_not)
3254
1
        << (NotNSStringTy ? 
"string type"0
: "NSString")
3255
1
        << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0);
3256
1
    return;
3257
1
  }
3258
3259
1.00k
  D->addAttr(::new (S.Context) FormatArgAttr(S.Context, AL, Idx));
3260
1.00k
}
3261
3262
enum FormatAttrKind {
3263
  CFStringFormat,
3264
  NSStringFormat,
3265
  StrftimeFormat,
3266
  SupportedFormat,
3267
  IgnoredFormat,
3268
  InvalidFormat
3269
};
3270
3271
/// getFormatAttrKind - Map from format attribute names to supported format
3272
/// types.
3273
37.1k
static FormatAttrKind getFormatAttrKind(StringRef Format) {
3274
37.1k
  return llvm::StringSwitch<FormatAttrKind>(Format)
3275
      // Check for formats that get handled specially.
3276
37.1k
      .Case("NSString", NSStringFormat)
3277
37.1k
      .Case("CFString", CFStringFormat)
3278
37.1k
      .Case("strftime", StrftimeFormat)
3279
3280
      // Otherwise, check for supported formats.
3281
37.1k
      .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat)
3282
37.1k
      .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat)
3283
37.1k
      .Case("kprintf", SupportedFormat)         // OpenBSD.
3284
37.1k
      .Case("freebsd_kprintf", SupportedFormat) // FreeBSD.
3285
37.1k
      .Case("os_trace", SupportedFormat)
3286
37.1k
      .Case("os_log", SupportedFormat)
3287
3288
37.1k
      .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat)
3289
37.1k
      .Default(InvalidFormat);
3290
37.1k
}
3291
3292
/// Handle __attribute__((init_priority(priority))) attributes based on
3293
/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
3294
1.13k
static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3295
1.13k
  if (!S.getLangOpts().CPlusPlus) {
3296
0
    S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL;
3297
0
    return;
3298
0
  }
3299
3300
1.13k
  if (S.getCurFunctionOrMethodDecl()) {
3301
2
    S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3302
2
    AL.setInvalid();
3303
2
    return;
3304
2
  }
3305
1.13k
  QualType T = cast<VarDecl>(D)->getType();
3306
1.13k
  if (S.Context.getAsArrayType(T))
3307
6
    T = S.Context.getBaseElementType(T);
3308
1.13k
  if (!T->getAs<RecordType>()) {
3309
2
    S.Diag(AL.getLoc(), diag::err_init_priority_object_attr);
3310
2
    AL.setInvalid();
3311
2
    return;
3312
2
  }
3313
3314
1.13k
  Expr *E = AL.getArgAsExpr(0);
3315
1.13k
  uint32_t prioritynum;
3316
1.13k
  if (!checkUInt32Argument(S, AL, E, prioritynum)) {
3317
2
    AL.setInvalid();
3318
2
    return;
3319
2
  }
3320
3321
  // Only perform the priority check if the attribute is outside of a system
3322
  // header. Values <= 100 are reserved for the implementation, and libc++
3323
  // benefits from being able to specify values in that range.
3324
1.13k
  if ((prioritynum < 101 || 
prioritynum > 655351.12k
) &&
3325
4
      !S.getSourceManager().isInSystemHeader(AL.getLoc())) {
3326
2
    S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
3327
2
        << E->getSourceRange() << AL << 101 << 65535;
3328
2
    AL.setInvalid();
3329
2
    return;
3330
2
  }
3331
1.12k
  D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
3332
1.12k
}
3333
3334
FormatAttr *Sema::mergeFormatAttr(Decl *D, const AttributeCommonInfo &CI,
3335
                                  IdentifierInfo *Format, int FormatIdx,
3336
38.0k
                                  int FirstArg) {
3337
  // Check whether we already have an equivalent format attribute.
3338
394
  for (auto *F : D->specific_attrs<FormatAttr>()) {
3339
394
    if (F->getType() == Format &&
3340
386
        F->getFormatIdx() == FormatIdx &&
3341
386
        F->getFirstArg() == FirstArg) {
3342
      // If we don't have a valid location for this attribute, adopt the
3343
      // location.
3344
386
      if (F->getLocation().isInvalid())
3345
0
        F->setRange(CI.getRange());
3346
386
      return nullptr;
3347
386
    }
3348
394
  }
3349
3350
37.6k
  return ::new (Context) FormatAttr(Context, CI, Format, FormatIdx, FirstArg);
3351
38.0k
}
3352
3353
/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
3354
/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
3355
37.1k
static void handleFormatAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3356
37.1k
  if (!AL.isArgIdent(0)) {
3357
0
    S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type)
3358
0
        << AL << 1 << AANT_ArgumentIdentifier;
3359
0
    return;
3360
0
  }
3361
3362
  // In C++ the implicit 'this' function parameter also counts, and they are
3363
  // counted from one.
3364
37.1k
  bool HasImplicitThisParam = isInstanceMethod(D);
3365
37.1k
  unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam;
3366
3367
37.1k
  IdentifierInfo *II = AL.getArgAsIdent(0)->Ident;
3368
37.1k
  StringRef Format = II->getName();
3369
3370
37.1k
  if (normalizeName(Format)) {
3371
    // If we've modified the string name, we need a new identifier for it.
3372
34.5k
    II = &S.Context.Idents.get(Format);
3373
34.5k
  }
3374
3375
  // Check for supported formats.
3376
37.1k
  FormatAttrKind Kind = getFormatAttrKind(Format);
3377
3378
37.1k
  if (Kind == IgnoredFormat)
3379
4
    return;
3380
3381
37.1k
  if (Kind == InvalidFormat) {
3382
0
    S.Diag(AL.getLoc(), diag::warn_attribute_type_not_supported)
3383
0
        << AL << II->getName();
3384
0
    return;
3385
0
  }
3386
3387
  // checks for the 2nd argument
3388
37.1k
  Expr *IdxExpr = AL.getArgAsExpr(1);
3389
37.1k
  uint32_t Idx;
3390
37.1k
  if (!checkUInt32Argument(S, AL, IdxExpr, Idx, 2))
3391
0
    return;
3392
3393
37.1k
  if (Idx < 1 || 
Idx > NumArgs37.1k
) {
3394
3
    S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3395
3
        << AL << 2 << IdxExpr->getSourceRange();
3396
3
    return;
3397
3
  }
3398
3399
  // FIXME: Do we need to bounds check?
3400
37.1k
  unsigned ArgIdx = Idx - 1;
3401
3402
37.1k
  if (HasImplicitThisParam) {
3403
70
    if (ArgIdx == 0) {
3404
4
      S.Diag(AL.getLoc(),
3405
4
             diag::err_format_attribute_implicit_this_format_string)
3406
4
        << IdxExpr->getSourceRange();
3407
4
      return;
3408
4
    }
3409
66
    ArgIdx--;
3410
66
  }
3411
3412
  // make sure the format string is really a string
3413
37.1k
  QualType Ty = getFunctionOrMethodParamType(D, ArgIdx);
3414
3415
37.1k
  if (Kind == CFStringFormat) {
3416
1.18k
    if (!isCFStringType(Ty, S.Context)) {
3417
1
      S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3418
1
        << "a CFString" << IdxExpr->getSourceRange()
3419
1
        << getFunctionOrMethodParamRange(D, ArgIdx);
3420
1
      return;
3421
1
    }
3422
35.9k
  } else if (Kind == NSStringFormat) {
3423
    // FIXME: do we need to check if the type is NSString*?  What are the
3424
    // semantics?
3425
4.19k
    if (!isNSStringType(Ty, S.Context)) {
3426
1
      S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3427
1
        << "an NSString" << IdxExpr->getSourceRange()
3428
1
        << getFunctionOrMethodParamRange(D, ArgIdx);
3429
1
      return;
3430
1
    }
3431
31.7k
  } else if (!Ty->isPointerType() ||
3432
31.7k
             !Ty->castAs<PointerType>()->getPointeeType()->isCharType()) {
3433
8
    S.Diag(AL.getLoc(), diag::err_format_attribute_not)
3434
8
      << "a string type" << IdxExpr->getSourceRange()
3435
8
      << getFunctionOrMethodParamRange(D, ArgIdx);
3436
8
    return;
3437
8
  }
3438
3439
  // check the 3rd argument
3440
37.1k
  Expr *FirstArgExpr = AL.getArgAsExpr(2);
3441
37.1k
  uint32_t FirstArg;
3442
37.1k
  if (!checkUInt32Argument(S, AL, FirstArgExpr, FirstArg, 3))
3443
0
    return;
3444
3445
  // check if the function is variadic if the 3rd argument non-zero
3446
37.1k
  if (FirstArg != 0) {
3447
18.7k
    if (isFunctionOrMethodVariadic(D)) {
3448
18.7k
      ++NumArgs; // +1 for ...
3449
3
    } else {
3450
3
      S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
3451
3
      return;
3452
3
    }
3453
37.1k
  }
3454
3455
  // strftime requires FirstArg to be 0 because it doesn't read from any
3456
  // variable the input is just the current time + the format string.
3457
37.1k
  if (Kind == StrftimeFormat) {
3458
190
    if (FirstArg != 0) {
3459
1
      S.Diag(AL.getLoc(), diag::err_format_strftime_third_parameter)
3460
1
        << FirstArgExpr->getSourceRange();
3461
1
      return;
3462
1
    }
3463
  // if 0 it disables parameter checking (to use with e.g. va_list)
3464
36.9k
  } else if (FirstArg != 0 && 
FirstArg != NumArgs18.7k
) {
3465
4
    S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3466
4
        << AL << 3 << FirstArgExpr->getSourceRange();
3467
4
    return;
3468
4
  }
3469
3470
37.1k
  FormatAttr *NewAttr = S.mergeFormatAttr(D, AL, II, Idx, FirstArg);
3471
37.1k
  if (NewAttr)
3472
37.1k
    D->addAttr(NewAttr);
3473
37.1k
}
3474
3475
/// Handle __attribute__((callback(CalleeIdx, PayloadIdx0, ...))) attributes.
3476
142
static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3477
  // The index that identifies the callback callee is mandatory.
3478
142
  if (AL.getNumArgs() == 0) {
3479
2
    S.Diag(AL.getLoc(), diag::err_callback_attribute_no_callee)
3480
2
        << AL.getRange();
3481
2
    return;
3482
2
  }
3483
3484
140
  bool HasImplicitThisParam = isInstanceMethod(D);
3485
140
  int32_t NumArgs = getFunctionOrMethodNumParams(D);
3486
3487
140
  FunctionDecl *FD = D->getAsFunction();
3488
140
  assert(FD && "Expected a function declaration!");
3489
3490
140
  llvm::StringMap<int> NameIdxMapping;
3491
140
  NameIdxMapping["__"] = -1;
3492
3493
140
  NameIdxMapping["this"] = 0;
3494
3495
140
  int Idx = 1;
3496
140
  for (const ParmVarDecl *PVD : FD->parameters())
3497
248
    NameIdxMapping[PVD->getName()] = Idx++;
3498
3499
140
  auto UnknownName = NameIdxMapping.end();
3500
3501
140
  SmallVector<int, 8> EncodingIndices;
3502
437
  for (unsigned I = 0, E = AL.getNumArgs(); I < E; 
++I297
) {
3503
317
    SourceRange SR;
3504
317
    int32_t ArgIdx;
3505
3506
317
    if (AL.isArgIdent(I)) {
3507
127
      IdentifierLoc *IdLoc = AL.getArgAsIdent(I);
3508
127
      auto It = NameIdxMapping.find(IdLoc->Ident->getName());
3509
127
      if (It == UnknownName) {
3510
4
        S.Diag(AL.getLoc(), diag::err_callback_attribute_argument_unknown)
3511
4
            << IdLoc->Ident << IdLoc->Loc;
3512
4
        return;
3513
4
      }
3514
3515
123
      SR = SourceRange(IdLoc->Loc);
3516
123
      ArgIdx = It->second;
3517
190
    } else if (AL.isArgExpr(I)) {
3518
190
      Expr *IdxExpr = AL.getArgAsExpr(I);
3519
3520
      // If the expression is not parseable as an int32_t we have a problem.
3521
190
      if (!checkUInt32Argument(S, AL, IdxExpr, (uint32_t &)ArgIdx, I + 1,
3522
0
                               false)) {
3523
0
        S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3524
0
            << AL << (I + 1) << IdxExpr->getSourceRange();
3525
0
        return;
3526
0
      }
3527
3528
      // Check oob, excluding the special values, 0 and -1.
3529
190
      if (ArgIdx < -1 || 
ArgIdx > NumArgs186
) {
3530
10
        S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
3531
10
            << AL << (I + 1) << IdxExpr->getSourceRange();
3532
10
        return;
3533
10
      }
3534
3535
180
      SR = IdxExpr->getSourceRange();
3536
0
    } else {
3537
0
      llvm_unreachable("Unexpected ParsedAttr argument type!");
3538
0
    }
3539
3540
303
    if (ArgIdx == 0 && 
!HasImplicitThisParam14
) {
3541
6
      S.Diag(AL.getLoc(), diag::err_callback_implicit_this_not_available)
3542
6
          << (I + 1) << SR;
3543
6
      return;
3544
6
    }
3545
3546
    // Adjust for the case we do not have an implicit "this" parameter. In this
3547
    // case we decrease all positive values by 1 to get LLVM argument indices.
3548
297
    if (!HasImplicitThisParam && 
ArgIdx > 0152
)
3549
116
      ArgIdx -= 1;
3550
3551
297
    EncodingIndices.push_back(ArgIdx);
3552
297
  }
3553
3554
120
  int CalleeIdx = EncodingIndices.front();
3555
  // Check if the callee index is proper, thus not "this" and not "unknown".
3556
  // This means the "CalleeIdx" has to be non-negative if "HasImplicitThisParam"
3557
  // is false and positive if "HasImplicitThisParam" is true.
3558
120
  if (CalleeIdx < (int)HasImplicitThisParam) {
3559
6
    S.Diag(AL.getLoc(), diag::err_callback_attribute_invalid_callee)
3560
6
        << AL.getRange();
3561
6
    return;
3562
6
  }
3563
3564
  // Get the callee type, note the index adjustment as the AST doesn't contain
3565
  // the this type (which the callee cannot reference anyway!).
3566
114
  const Type *CalleeType =
3567
114
      getFunctionOrMethodParamType(D, CalleeIdx - HasImplicitThisParam)
3568
114
          .getTypePtr();
3569
114
  if (!CalleeType || !CalleeType->isFunctionPointerType()) {
3570
0
    S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3571
0
        << AL.getRange();
3572
0
    return;
3573
0
  }
3574
3575
114
  const Type *CalleeFnType =
3576
114
      CalleeType->getPointeeType()->getUnqualifiedDesugaredType();
3577
3578
  // TODO: Check the type of the callee arguments.
3579
3580
114
  const auto *CalleeFnProtoType = dyn_cast<FunctionProtoType>(CalleeFnType);
3581
114
  if (!CalleeFnProtoType) {
3582
0
    S.Diag(AL.getLoc(), diag::err_callback_callee_no_function_type)
3583
0
        << AL.getRange();
3584
0
    return;
3585
0
  }
3586
3587
114
  if (CalleeFnProtoType->getNumParams() > EncodingIndices.size() - 1) {
3588
12
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3589
12
        << AL << (unsigned)(EncodingIndices.size() - 1);
3590
12
    return;
3591
12
  }
3592
3593
102
  if (CalleeFnProtoType->getNumParams() < EncodingIndices.size() - 1) {
3594
28
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
3595
28
        << AL << (unsigned)(EncodingIndices.size() - 1);
3596
28
    return;
3597
28
  }
3598
3599
74
  if (CalleeFnProtoType->isVariadic()) {
3600
8
    S.Diag(AL.getLoc(), diag::err_callback_callee_is_variadic) << AL.getRange();
3601
8
    return;
3602
8
  }
3603
3604
  // Do not allow multiple callback attributes.
3605
66
  if (D->hasAttr<CallbackAttr>()) {
3606
8
    S.Diag(AL.getLoc(), diag::err_callback_attribute_multiple) << AL.getRange();
3607
8
    return;
3608
8
  }
3609
3610
58
  D->addAttr(::new (S.Context) CallbackAttr(
3611
58
      S.Context, AL, EncodingIndices.data(), EncodingIndices.size()));
3612
58
}
3613
3614
66
static void handleTransparentUnionAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3615
  // Try to find the underlying union declaration.
3616
66
  RecordDecl *RD = nullptr;
3617
66
  const auto *TD = dyn_cast<TypedefNameDecl>(D);
3618
66
  if (TD && 
TD->getUnderlyingType()->isUnionType()38
)
3619
38
    RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
3620
28
  else
3621
28
    RD = dyn_cast<RecordDecl>(D);
3622
3623
66
  if (!RD || !RD->isUnion()) {
3624
0
    S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL
3625
0
                                                              << ExpectedUnion;
3626
0
    return;
3627
0
  }
3628
3629
66
  if (!RD->isCompleteDefinition()) {
3630
8
    if (!RD->isBeingDefined())
3631
0
      S.Diag(AL.getLoc(),
3632
0
             diag::warn_transparent_union_attribute_not_definition);
3633
8
    return;
3634
8
  }
3635
3636
58
  RecordDecl::field_iterator Field = RD->field_begin(),
3637
58
                          FieldEnd = RD->field_end();
3638
58
  if (Field == FieldEnd) {
3639
1
    S.Diag(AL.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
3640
1
    return;
3641
1
  }
3642
3643
57
  FieldDecl *FirstField = *Field;
3644
57
  QualType FirstType = FirstField->getType();
3645
57
  if (FirstType->hasFloatingRepresentation() || 
FirstType->isVectorType()56
) {
3646
2
    S.Diag(FirstField->getLocation(),
3647
2
           diag::warn_transparent_union_attribute_floating)
3648
2
      << FirstType->isVectorType() << FirstType;
3649
2
    return;
3650
2
  }
3651
3652
55
  if (FirstType->isIncompleteType())
3653
3
    return;
3654
52
  uint64_t FirstSize = S.Context.getTypeSize(FirstType);
3655
52
  uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
3656
223
  for (; Field != FieldEnd; 
++Field171
) {
3657
177
    QualType FieldType = Field->getType();
3658
177
    if (FieldType->isIncompleteType())
3659
1
      return;
3660
    // FIXME: this isn't fully correct; we also need to test whether the
3661
    // members of the union would all have the same calling convention as the
3662
    // first member of the union. Checking just the size and alignment isn't
3663
    // sufficient (consider structs passed on the stack instead of in registers
3664
    // as an example).
3665
176
    if (S.Context.getTypeSize(FieldType) != FirstSize ||
3666
174
        S.Context.getTypeAlign(FieldType) > FirstAlign) {
3667
      // Warn if we drop the attribute.
3668
5
      bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
3669
2
      unsigned FieldBits = isSize ? S.Context.getTypeSize(FieldType)
3670
3
                                  : S.Context.getTypeAlign(FieldType);
3671
5
      S.Diag(Field->getLocation(),
3672
5
             diag::warn_transparent_union_attribute_field_size_align)
3673
5
          << isSize << *Field << FieldBits;
3674
3
      unsigned FirstBits = isSize ? 
FirstSize2
: FirstAlign;
3675
5
      S.Diag(FirstField->getLocation(),
3676
5
             diag::note_transparent_union_first_field_size_align)
3677
5
          << isSize << FirstBits;
3678
5
      return;
3679
5
    }
3680
176
  }
3681
3682
46
  RD->addAttr(::new (S.Context) TransparentUnionAttr(S.Context, AL));
3683
46
}
3684
3685
303
static void handleAnnotateAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3686
  // Make sure that there is a string literal as the annotation's single
3687
  // argument.
3688
303
  StringRef Str;
3689
303
  if (!S.checkStringLiteralArgumentAttr(AL, 0, Str))
3690
2
    return;
3691
3692
  // Don't duplicate annotations that are already set.
3693
301
  for (const auto *I : D->specific_attrs<AnnotateAttr>()) {
3694
42
    if (I->getAnnotation() == Str)
3695
7
      return;
3696
42
  }
3697
3698
294
  D->addAttr(::new (S.Context) AnnotateAttr(S.Context, AL, Str));
3699
294
}
3700
3701
270
static void handleAlignValueAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3702
270
  S.AddAlignValueAttr(D, AL, AL.getArgAsExpr(0));
3703
270
}
3704
3705
273
void Sema::AddAlignValueAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E) {
3706
273
  AlignValueAttr TmpAttr(Context, CI, E);
3707
273
  SourceLocation AttrLoc = CI.getLoc();
3708
3709
273
  QualType T;
3710
273
  if (const auto *TD = dyn_cast<TypedefNameDecl>(D))
3711
14
    T = TD->getUnderlyingType();
3712
259
  else if (const auto *VD = dyn_cast<ValueDecl>(D))
3713
259
    T = VD->getType();
3714
259
  else
3715
0
    llvm_unreachable("Unknown decl type for align_value");
3716
3717
273
  if (!T->isDependentType() && 
!T->isAnyPointerType()270
&&
3718
5
      !T->isReferenceType() && 
!T->isMemberPointerType()1
) {
3719
1
    Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only)
3720
1
      << &TmpAttr << T << D->getSourceRange();
3721
1
    return;
3722
1
  }
3723
3724
272
  if (!E->isValueDependent()) {
3725
269
    llvm::APSInt Alignment;
3726
269
    ExprResult ICE = VerifyIntegerConstantExpression(
3727
269
        E, &Alignment, diag::err_align_value_attribute_argument_not_int);
3728
269
    if (ICE.isInvalid())
3729
1
      return;
3730
3731
268
    if (!Alignment.isPowerOf2()) {
3732
3
      Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3733
3
        << E->getSourceRange();
3734
3
      return;
3735
3
    }
3736
3737
265
    D->addAttr(::new (Context) AlignValueAttr(Context, CI, ICE.get()));
3738
265
    return;
3739
265
  }
3740
3741
  // Save dependent expressions in the AST to be instantiated.
3742
3
  D->addAttr(::new (Context) AlignValueAttr(Context, CI, E));
3743
3
}
3744
3745
32.4k
static void handleAlignedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
3746
  // check the attribute arguments.
3747
32.4k
  if (AL.getNumArgs() > 1) {
3748
0
    S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 1;
3749
0
    return;
3750
0
  }
3751
3752
32.4k
  if (AL.getNumArgs() == 0) {
3753
45
    D->addAttr(::new (S.Context) AlignedAttr(S.Context, AL, true, nullptr));
3754
45
    return;
3755
45
  }
3756
3757
32.4k
  Expr *E = AL.getArgAsExpr(0);
3758
32.4k
  if (AL.isPackExpansion() && 
!E->containsUnexpandedParameterPack()9
) {
3759
1
    S.Diag(AL.getEllipsisLoc(),
3760
1
           diag::err_pack_expansion_without_parameter_packs);
3761
1
    return;
3762
1
  }
3763
3764
32.4k
  if (!AL.isPackExpansion() && 
S.DiagnoseUnexpandedParameterPack(E)32.4k
)
3765
3
    return;
3766
3767
32.4k
  S.AddAlignedAttr(D, AL, E, AL.isPackExpansion());
3768
32.4k
}
3769
3770
void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
3771
32.5k
                          bool IsPackExpansion) {
3772
32.5k
  AlignedAttr TmpAttr(Context, CI, true, E);
3773
32.5k
  SourceLocation AttrLoc = CI.getLoc();
3774
3775
  // C++11 alignas(...) and C11 _Alignas(...) have additional requirements.
3776
32.5k
  if (TmpAttr.isAlignas()) {
3777
    // C++11 [dcl.align]p1:
3778
    //   An alignment-specifier may be applied to a variable or to a class
3779
    //   data member, but it shall not be applied to a bit-field, a function
3780
    //   parameter, the formal parameter of a catch clause, or a variable
3781
    //   declared with the register storage class specifier. An
3782
    //   alignment-specifier may also be applied to the declaration of a class
3783
    //   or enumeration type.
3784
    // C11 6.7.5/2:
3785
    //   An alignment attribute shall not be specified in a declaration of
3786
    //   a typedef, or a bit-field, or a function, or a parameter, or an
3787
    //   object declared with the register storage-class specifier.
3788
8.73k
    int DiagKind = -1;
3789
8.73k
    if (isa<ParmVarDecl>(D)) {
3790
3
      DiagKind = 0;
3791
8.72k
    } else if (const auto *VD = dyn_cast<VarDecl>(D)) {
3792
1.27k
      if (VD->getStorageClass() == SC_Register)
3793
3
        DiagKind = 1;
3794
1.27k
      if (VD->isExceptionVariable())
3795
1
        DiagKind = 2;
3796
7.45k
    } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
3797
82
      if (FD->isBitField())
3798
3
        DiagKind = 3;
3799
7.36k
    } else if (!isa<TagDecl>(D)) {
3800
7
      Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr
3801
2
        << (TmpAttr.isC11() ? ExpectedVariableOrField
3802
5
                            : ExpectedVariableFieldOrTag);
3803
7
      return;
3804
7
    }
3805
8.72k
    if (DiagKind != -1) {
3806
10
      Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type)
3807
10
        << &TmpAttr << DiagKind;
3808
10
      return;
3809
10
    }
3810
32.5k
  }
3811
3812
32.5k
  if (E->isValueDependent()) {
3813
    // We can't support a dependent alignment on a non-dependent type,
3814
    // because we have no way to model that a type is "alignment-dependent"
3815
    // but not dependent in any other way.
3816
508
    if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
3817
2
      if (!TND->getUnderlyingType()->isDependentType()) {
3818
2
        Diag(AttrLoc, diag::err_alignment_dependent_typedef_name)
3819
2
            << E->getSourceRange();
3820
2
        return;
3821
2
      }
3822
506
    }
3823
3824
    // Save dependent expressions in the AST to be instantiated.
3825
506
    AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, E);
3826
506
    AA->setPackExpansion(IsPackExpansion);
3827
506
    D->addAttr(AA);
3828
506
    return;
3829
506
  }
3830
3831
  // FIXME: Cache the number on the AL object?
3832
32.0k
  llvm::APSInt Alignment;
3833
32.0k
  ExprResult ICE = VerifyIntegerConstantExpression(
3834
32.0k
      E, &Alignment, diag::err_aligned_attribute_argument_not_int);
3835
32.0k
  if (ICE.isInvalid())
3836
3
    return;
3837
3838
32.0k
  uint64_t AlignVal = Alignment.getZExtValue();
3839
3840
  // C++11 [dcl.align]p2:
3841
  //   -- if the constant expression evaluates to zero, the alignment
3842
  //      specifier shall have no effect
3843
  // C11 6.7.5p6:
3844
  //   An alignment specification of zero has no effect.
3845
32.0k
  if (!(TmpAttr.isAlignas() && 
!Alignment8.21k
)) {
3846
32.0k
    if (!llvm::isPowerOf2_64(AlignVal)) {
3847
6
      Diag(AttrLoc, diag::err_alignment_not_power_of_two)
3848
6
        << E->getSourceRange();
3849
6
      return;
3850
6
    }
3851
31.9k
  }
3852
3853
31.9k
  unsigned MaximumAlignment = Sema::MaximumAlignment;
3854
31.9k
  if (Context.getTargetInfo().getTriple().isOSBinFormatCOFF())
3855
832
    MaximumAlignment = std::min(MaximumAlignment, 8192u);
3856
31.9k
  if (AlignVal > MaximumAlignment) {
3857
15
    Diag(AttrLoc, diag::err_attribute_aligned_too_great)
3858
15
        << MaximumAlignment << E->getSourceRange();
3859
15
    return;
3860
15
  }
3861
3862
31.9k
  if (Context.getTargetInfo().isTLSSupported()) {
3863
28.5k
    unsigned MaxTLSAlign =
3864
28.5k
        Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign())
3865
28.5k
            .getQuantity();
3866
28.5k
    const auto *VD = dyn_cast<VarDecl>(D);
3867
28.5k
    if (MaxTLSAlign && 
AlignVal > MaxTLSAlign879
&&
VD7
&&
3868
2
        VD->getTLSKind() != VarDecl::TLS_None) {
3869
2
      Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum)
3870
2
          << (unsigned)AlignVal << VD << MaxTLSAlign;
3871
2
      return;
3872
2
    }
3873
31.9k
  }
3874
3875
31.9k
  AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, true, ICE.get());
3876
31.9k
  AA->setPackExpansion(IsPackExpansion);
3877
31.9k
  D->addAttr(AA);
3878
31.9k
}
3879
3880
void Sema::AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI,
3881
0
                          TypeSourceInfo *TS, bool IsPackExpansion) {
3882
  // FIXME: Cache the number on the AL object if non-dependent?
3883
  // FIXME: Perform checking of type validity
3884
0
  AlignedAttr *AA = ::new (Context) AlignedAttr(Context, CI, false, TS);
3885
0
  AA->setPackExpansion(IsPackExpansion);
3886
0
  D->addAttr(AA);
3887
0
}
3888
3889
1.75M
void Sema::CheckAlignasUnderalignment(Decl *D) {
3890
1.75M
  assert(D->hasAttrs() && "no attributes on decl");
3891
3892
1.75M
  QualType UnderlyingTy, DiagTy;
3893
1.75M
  if (const auto *VD = dyn_cast<ValueDecl>(D)) {
3894
1.06M
    UnderlyingTy = DiagTy = VD->getType();
3895
693k
  } else {
3896
693k
    UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D));
3897
693k
    if (const auto *ED = dyn_cast<EnumDecl>(D))
3898
133k
      UnderlyingTy = ED->getIntegerType();
3899
693k
  }
3900
1.75M
  if (DiagTy->isDependentType() || 
DiagTy->isIncompleteType()1.64M
)
3901
128k
    return;
3902
3903
  // C++11 [dcl.align]p5, C11 6.7.5/4:
3904
  //   The combined effect of all alignment attributes in a declaration shall
3905
  //   not specify an alignment that is less strict than the alignment that
3906
  //   would otherwise be required for the entity being declared.
3907
1.62M
  AlignedAttr *AlignasAttr = nullptr;
3908
1.62M
  AlignedAttr *LastAlignedAttr = nullptr;
3909
1.62M
  unsigned Align = 0;
3910
14.5k
  for (auto *I : D->specific_attrs<AlignedAttr>()) {
3911
14.5k
    if (I->isAlignmentDependent())
3912
14
      return;
3913
14.5k
    if (I->isAlignas())
3914
2.27k
      AlignasAttr = I;
3915
14.5k
    Align = std::max(Align, I->getAlignment(Context));
3916
14.5k
    LastAlignedAttr = I;
3917
14.5k
  }
3918
3919
1.62M
  if (Align && 
DiagTy->isSizelessType()14.4k
) {
3920
21
    Diag(LastAlignedAttr->getLocation(), diag::err_attribute_sizeless_type)
3921
21
        << LastAlignedAttr << DiagTy;
3922
1.62M
  } else if (AlignasAttr && 
Align2.23k
) {
3923
2.23k
    CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align);
3924
2.23k
    CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy);
3925
2.23k
    if (NaturalAlign > RequestedAlign)
3926
27
      Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned)
3927
27
        << DiagTy << (unsigned)NaturalAlign.getQuantity();
3928
2.23k
  }
3929
1.62M
}
3930
3931
bool Sema::checkMSInheritanceAttrOnDefinition(
3932
    CXXRecordDecl *RD, SourceRange Range, bool BestCase,
3933
83
    MSInheritanceModel ExplicitModel) {
3934
83
  assert(RD->hasDefinition() && "RD has no definition!");
3935
3936
  // We may not have seen base specifiers or any virtual methods yet.  We will
3937
  // have to wait until the record is defined to catch any mismatches.
3938
83
  if (!RD->getDefinition()->isCompleteDefinition())
3939
29
    return false;
3940
3941
  // The unspecified model never matches what a definition could need.
3942
54
  if (ExplicitModel == MSInheritanceModel::Unspecified)
3943
33
    return false;
3944
3945
21
  if (BestCase) {
3946
19
    if (RD->calculateInheritanceModel() == ExplicitModel)
3947
13
      return false;
3948
2
  } else {
3949
2
    if (RD->calculateInheritanceModel() <= ExplicitModel)
3950
2
      return false;
3951
6
  }
3952
3953
6
  Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance)
3954
6
      << 0 /*definition*/;
3955
6
  Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) << RD;
3956
6
  return true;
3957
6
}
3958
3959
/// parseModeAttrArg - Parses attribute mode string and returns parsed type
3960
/// attribute.
3961
static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth,
3962
                             bool &IntegerMode, bool &ComplexMode,
3963
369
                             bool &ExplicitIEEE) {
3964
369
  IntegerMode = true;
3965
369
  ComplexMode = false;
3966
369
  switch (Str.size()) {
3967
334
  case 2:
3968
334
    switch (Str[0]) {
3969
66
    case 'Q':
3970
66
      DestWidth = 8;
3971
66
      break;
3972
67
    case 'H':
3973
67
      DestWidth = 16;
3974
67
      break;
3975
93
    case 'S':
3976
93
      DestWidth = 32;
3977
93
      break;
3978
81
    case 'D':
3979
81
      DestWidth = 64;
3980
81
      break;
3981
5
    case 'X':
3982
5
      DestWidth = 96;
3983
5
      break;
3984
4
    case 'K': // KFmode - IEEE quad precision (__float128)
3985
4
      ExplicitIEEE = true;
3986
3
      DestWidth = Str[1] == 'I' ? 
01
: 128;
3987
4
      break;
3988
9
    case 'T':
3989
9
      ExplicitIEEE = false;
3990
9
      DestWidth = 128;
3991
9
      break;
3992
334
    }
3993
334
    if (Str[1] == 'F') {
3994
52
      IntegerMode = false;
3995
282
    } else if (Str[1] == 'C') {
3996
26
      IntegerMode = false;
3997
26
      ComplexMode = true;
3998
256
    } else if (Str[1] != 'I') {
3999
0
      DestWidth = 0;
4000
0
    }
4001
334
    break;
4002
7
  case 4:
4003
    // FIXME: glibc uses 'word' to define register_t; this is narrower than a
4004
    // pointer on PIC16 and other embedded platforms.
4005
7
    if (Str == "word")
4006
1
      DestWidth = S.Context.getTargetInfo().getRegisterWidth();
4007
6
    else if (Str == "byte")
4008
5
      DestWidth = S.Context.getTargetInfo().getCharWidth();
4009
7
    break;
4010
0
  case 7:
4011
0
    if (Str == "pointer")
4012
0
      DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
4013
0
    break;
4014
28
  case 11:
4015
28
    if (Str == "unwind_word")
4016
28
      DestWidth = S.Context.getTargetInfo().getUnwindWordWidth();
4017
28
    break;
4018
369
  }
4019
369
}
4020
4021
/// handleModeAttr - This attribute modifies the width of a decl with primitive
4022
/// type.
4023
///
4024
/// Despite what would be logical, the mode attribute is a decl attribute, not a
4025
/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
4026
/// HImode, not an intermediate pointer.
4027
294
static void handleModeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
4028
  // This attribute isn't documented, but glibc uses it.  It changes