[clang] 80fda7a - [clang][Sema][NFC] Make a bunch of things const if possible
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 20 03:24:13 PDT 2023
Author: Timm Bäder
Date: 2023-04-20T12:23:51+02:00
New Revision: 80fda7a346630e490e8bc3a9cc0d5e0d35526ecb
URL: https://github.com/llvm/llvm-project/commit/80fda7a346630e490e8bc3a9cc0d5e0d35526ecb
DIFF: https://github.com/llvm/llvm-project/commit/80fda7a346630e490e8bc3a9cc0d5e0d35526ecb.diff
LOG: [clang][Sema][NFC] Make a bunch of things const if possible
And some general code style cleanup.
Differential Revision: https://reviews.llvm.org/D148696
Added:
Modified:
clang/include/clang/AST/DeclarationName.h
clang/include/clang/Sema/Sema.h
clang/lib/AST/DeclarationName.cpp
clang/lib/Sema/Sema.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaExpr.cpp
clang/lib/Sema/SemaLookup.cpp
clang/lib/Sema/SemaOpenMP.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/AST/DeclarationName.h b/clang/include/clang/AST/DeclarationName.h
index b682a75e65764..b06931ea3e418 100644
--- a/clang/include/clang/AST/DeclarationName.h
+++ b/clang/include/clang/AST/DeclarationName.h
@@ -118,14 +118,14 @@ class alignas(IdentifierInfoAlignment) CXXLiteralOperatorIdName
friend class clang::DeclarationName;
friend class clang::DeclarationNameTable;
- IdentifierInfo *ID;
+ const IdentifierInfo *ID;
/// Extra information associated with this operator name that
/// can be used by the front end. All bits are really needed
/// so it is not possible to stash something in the low order bits.
void *FETokenInfo;
- CXXLiteralOperatorIdName(IdentifierInfo *II)
+ CXXLiteralOperatorIdName(const IdentifierInfo *II)
: DeclarationNameExtra(CXXLiteralOperatorName), ID(II),
FETokenInfo(nullptr) {}
@@ -478,7 +478,7 @@ class DeclarationName {
/// If this name is the name of a literal operator,
/// retrieve the identifier associated with it.
- IdentifierInfo *getCXXLiteralIdentifier() const {
+ const IdentifierInfo *getCXXLiteralIdentifier() const {
if (getNameKind() == CXXLiteralOperatorName) {
assert(getPtr() && "getCXXLiteralIdentifier on a null DeclarationName!");
return castAsCXXLiteralOperatorIdName()->ID;
@@ -650,7 +650,7 @@ class DeclarationNameTable {
}
/// Get the name of the literal operator function with II as the identifier.
- DeclarationName getCXXLiteralOperatorName(IdentifierInfo *II);
+ DeclarationName getCXXLiteralOperatorName(const IdentifierInfo *II);
};
/// DeclarationNameLoc - Additional source/type location info
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index b05238d0352e6..fd0b89eaec87c 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -840,7 +840,7 @@ class Sema final {
/// FieldCollector - Collects CXXFieldDecls during parsing of C++ classes.
std::unique_ptr<CXXFieldCollector> FieldCollector;
- typedef llvm::SmallSetVector<NamedDecl *, 16> NamedDeclSetType;
+ typedef llvm::SmallSetVector<const NamedDecl *, 16> NamedDeclSetType;
/// Set containing all declared private fields that are not used.
NamedDeclSetType UnusedPrivateFields;
@@ -1489,7 +1489,7 @@ class Sema final {
/// Determine if VD, which must be a variable or function, is an external
/// symbol that nonetheless can't be referenced from outside this translation
/// unit because its type has no linkage and it's not extern "C".
- bool isExternalWithNoLinkageType(ValueDecl *VD) const;
+ bool isExternalWithNoLinkageType(const ValueDecl *VD) const;
/// Obtain a sorted list of functions that are undefined but ODR-used.
void getUndefinedButUsed(
@@ -1778,7 +1778,7 @@ class Sema final {
};
SemaDiagnosticBuilder(Kind K, SourceLocation Loc, unsigned DiagID,
- FunctionDecl *Fn, Sema &S);
+ const FunctionDecl *Fn, Sema &S);
SemaDiagnosticBuilder(SemaDiagnosticBuilder &&D);
SemaDiagnosticBuilder(const SemaDiagnosticBuilder &) = default;
~SemaDiagnosticBuilder();
@@ -1853,7 +1853,7 @@ class Sema final {
Sema &S;
SourceLocation Loc;
unsigned DiagID;
- FunctionDecl *Fn;
+ const FunctionDecl *Fn;
bool ShowCallStack;
// Invariant: At most one of these Optionals has a value.
@@ -3242,9 +3242,9 @@ class Sema final {
/// Diagnose that the specified declaration needs to be visible but
/// isn't, and suggest a module import that would resolve the problem.
- void diagnoseMissingImport(SourceLocation Loc, NamedDecl *Decl,
+ void diagnoseMissingImport(SourceLocation Loc, const NamedDecl *Decl,
MissingImportKind MIK, bool Recover = true);
- void diagnoseMissingImport(SourceLocation Loc, NamedDecl *Decl,
+ void diagnoseMissingImport(SourceLocation Loc, const NamedDecl *Decl,
SourceLocation DeclLoc, ArrayRef<Module *> Modules,
MissingImportKind MIK, bool Recover);
@@ -4510,7 +4510,7 @@ class Sema final {
TemplateDiscarded, // Discarded due to uninstantiated templates
Unknown,
};
- FunctionEmissionStatus getEmissionStatus(FunctionDecl *Decl,
+ FunctionEmissionStatus getEmissionStatus(const FunctionDecl *Decl,
bool Final = false);
// Whether the callee should be ignored in CUDA/HIP/OpenMP host/device check.
@@ -5563,7 +5563,7 @@ class Sema final {
DeclarationNameInfo &NameInfo,
const TemplateArgumentListInfo *&TemplateArgs);
- bool DiagnoseDependentMemberLookup(LookupResult &R);
+ bool DiagnoseDependentMemberLookup(const LookupResult &R);
bool
DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
@@ -12997,14 +12997,14 @@ class Sema final {
/// Diagnostics that are emitted only if we discover that the given function
/// must be codegen'ed. Because handling these correctly adds overhead to
/// compilation, this is currently only enabled for CUDA compilations.
- llvm::DenseMap<CanonicalDeclPtr<FunctionDecl>,
+ llvm::DenseMap<CanonicalDeclPtr<const FunctionDecl>,
std::vector<PartialDiagnosticAt>>
DeviceDeferredDiags;
/// A pair of a canonical FunctionDecl and a SourceLocation. When used as the
/// key in a hashtable, both the FD and location are hashed.
struct FunctionDeclAndLoc {
- CanonicalDeclPtr<FunctionDecl> FD;
+ CanonicalDeclPtr<const FunctionDecl> FD;
SourceLocation Loc;
};
@@ -13018,7 +13018,7 @@ class Sema final {
///
/// Functions that we can tell a priori must be emitted aren't added to this
/// map.
- llvm::DenseMap</* Callee = */ CanonicalDeclPtr<FunctionDecl>,
+ llvm::DenseMap</* Callee = */ CanonicalDeclPtr<const FunctionDecl>,
/* Caller = */ FunctionDeclAndLoc>
DeviceKnownEmittedFns;
@@ -13063,8 +13063,9 @@ class Sema final {
/// if (diagIfOpenMPDeviceCode(Loc, diag::err_vla_unsupported))
/// return ExprError();
/// // Otherwise, continue parsing as normal.
- SemaDiagnosticBuilder
- diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID, FunctionDecl *FD);
+ SemaDiagnosticBuilder diagIfOpenMPDeviceCode(SourceLocation Loc,
+ unsigned DiagID,
+ const FunctionDecl *FD);
/// Creates a SemaDiagnosticBuilder that emits the diagnostic if the current
/// context is "used as host code".
@@ -13080,13 +13081,14 @@ class Sema final {
/// return ExprError();
/// // Otherwise, continue parsing as normal.
SemaDiagnosticBuilder diagIfOpenMPHostCode(SourceLocation Loc,
- unsigned DiagID, FunctionDecl *FD);
+ unsigned DiagID,
+ const FunctionDecl *FD);
SemaDiagnosticBuilder targetDiag(SourceLocation Loc, unsigned DiagID,
- FunctionDecl *FD = nullptr);
+ const FunctionDecl *FD = nullptr);
SemaDiagnosticBuilder targetDiag(SourceLocation Loc,
const PartialDiagnostic &PD,
- FunctionDecl *FD = nullptr) {
+ const FunctionDecl *FD = nullptr) {
return targetDiag(Loc, PD.getDiagID(), FD) << PD;
}
@@ -14045,7 +14047,8 @@ namespace llvm {
// SourceLocation.
template <> struct DenseMapInfo<clang::Sema::FunctionDeclAndLoc> {
using FunctionDeclAndLoc = clang::Sema::FunctionDeclAndLoc;
- using FDBaseInfo = DenseMapInfo<clang::CanonicalDeclPtr<clang::FunctionDecl>>;
+ using FDBaseInfo =
+ DenseMapInfo<clang::CanonicalDeclPtr<const clang::FunctionDecl>>;
static FunctionDeclAndLoc getEmptyKey() {
return {FDBaseInfo::getEmptyKey(), clang::SourceLocation()};
diff --git a/clang/lib/AST/DeclarationName.cpp b/clang/lib/AST/DeclarationName.cpp
index c1219041a466b..73c56e6610ee0 100644
--- a/clang/lib/AST/DeclarationName.cpp
+++ b/clang/lib/AST/DeclarationName.cpp
@@ -365,7 +365,7 @@ DeclarationNameTable::getCXXSpecialName(DeclarationName::NameKind Kind,
}
DeclarationName
-DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) {
+DeclarationNameTable::getCXXLiteralOperatorName(const IdentifierInfo *II) {
llvm::FoldingSetNodeID ID;
ID.AddPointer(II);
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index b709f920f236c..50d48be532ab9 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -785,15 +785,15 @@ static bool ShouldRemoveFromUnused(Sema *SemaRef, const DeclaratorDecl *D) {
return false;
}
-static bool isFunctionOrVarDeclExternC(NamedDecl *ND) {
- if (auto *FD = dyn_cast<FunctionDecl>(ND))
+static bool isFunctionOrVarDeclExternC(const NamedDecl *ND) {
+ if (const auto *FD = dyn_cast<FunctionDecl>(ND))
return FD->isExternC();
return cast<VarDecl>(ND)->isExternC();
}
/// Determine whether ND is an external-linkage function or variable whose
/// type has no linkage.
-bool Sema::isExternalWithNoLinkageType(ValueDecl *VD) const {
+bool Sema::isExternalWithNoLinkageType(const ValueDecl *VD) const {
// Note: it's not quite enough to check whether VD has UniqueExternalLinkage,
// because we also want to catch the case where its type has VisibleNoLinkage,
// which does not affect the linkage of VD.
@@ -1403,9 +1403,7 @@ void Sema::ActOnEndOfTranslationUnit() {
// source.
RecordCompleteMap RecordsComplete;
RecordCompleteMap MNCComplete;
- for (NamedDeclSetType::iterator I = UnusedPrivateFields.begin(),
- E = UnusedPrivateFields.end(); I != E; ++I) {
- const NamedDecl *D = *I;
+ for (const NamedDecl *D : UnusedPrivateFields) {
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D->getDeclContext());
if (RD && !RD->isUnion() &&
IsRecordFullyDefined(RD, RecordsComplete, MNCComplete)) {
@@ -1607,7 +1605,7 @@ bool Sema::hasUncompilableErrorOccurred() const {
// Print notes showing how we can reach FD starting from an a priori
// known-callable function.
-static void emitCallStackNotes(Sema &S, FunctionDecl *FD) {
+static void emitCallStackNotes(Sema &S, const FunctionDecl *FD) {
auto FnIt = S.DeviceKnownEmittedFns.find(FD);
while (FnIt != S.DeviceKnownEmittedFns.end()) {
// Respect error limit.
@@ -1819,7 +1817,8 @@ void Sema::emitDeferredDiags() {
Sema::SemaDiagnosticBuilder::SemaDiagnosticBuilder(Kind K, SourceLocation Loc,
unsigned DiagID,
- FunctionDecl *Fn, Sema &S)
+ const FunctionDecl *Fn,
+ Sema &S)
: S(S), Loc(Loc), DiagID(DiagID), Fn(Fn),
ShowCallStack(K == K_ImmediateWithCallStack || K == K_Deferred) {
switch (K) {
@@ -1865,7 +1864,7 @@ Sema::SemaDiagnosticBuilder::~SemaDiagnosticBuilder() {
}
Sema::SemaDiagnosticBuilder
-Sema::targetDiag(SourceLocation Loc, unsigned DiagID, FunctionDecl *FD) {
+Sema::targetDiag(SourceLocation Loc, unsigned DiagID, const FunctionDecl *FD) {
FD = FD ? FD : getCurFunctionDecl();
if (LangOpts.OpenMP)
return LangOpts.OpenMPIsDevice ? diagIfOpenMPDeviceCode(Loc, DiagID, FD)
@@ -1933,8 +1932,9 @@ void Sema::checkTypeSupport(QualType Ty, SourceLocation Loc, ValueDecl *D) {
// Try to associate errors with the lexical context, if that is a function, or
// the value declaration otherwise.
- FunctionDecl *FD = isa<FunctionDecl>(C) ? cast<FunctionDecl>(C)
- : dyn_cast_or_null<FunctionDecl>(D);
+ const FunctionDecl *FD = isa<FunctionDecl>(C)
+ ? cast<FunctionDecl>(C)
+ : dyn_cast_or_null<FunctionDecl>(D);
auto CheckDeviceType = [&](QualType Ty) {
if (Ty->isDependentType())
@@ -2512,8 +2512,8 @@ bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
return false;
}
- if (const DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
- if (const FunctionDecl *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
+ if (const auto *DeclRef = dyn_cast<DeclRefExpr>(E.IgnoreParens())) {
+ if (const auto *Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) {
if (Fun->getMinRequiredArguments() == 0)
ZeroArgCallReturnTy = Fun->getReturnType();
return true;
@@ -2530,8 +2530,7 @@ bool Sema::tryExprAsCall(Expr &E, QualType &ZeroArgCallReturnTy,
if (!FunTy)
FunTy = ExprTy->getAs<FunctionType>();
- if (const FunctionProtoType *FPT =
- dyn_cast_or_null<FunctionProtoType>(FunTy)) {
+ if (const auto *FPT = dyn_cast_if_present<FunctionProtoType>(FunTy)) {
if (FPT->getNumParams() == 0)
ZeroArgCallReturnTy = FunTy->getReturnType();
return true;
@@ -2562,7 +2561,7 @@ static void noteOverloads(Sema &S, const UnresolvedSetImpl &Overloads,
continue;
}
- NamedDecl *Fn = (*It)->getUnderlyingDecl();
+ const NamedDecl *Fn = (*It)->getUnderlyingDecl();
// Don't print overloads for non-default multiversioned functions.
if (const auto *FD = Fn->getAsFunction()) {
if (FD->isMultiVersion() && FD->hasAttr<TargetAttr>() &&
@@ -2592,7 +2591,7 @@ static void notePlausibleOverloads(Sema &S, SourceLocation Loc,
UnresolvedSet<2> PlausibleOverloads;
for (OverloadExpr::decls_iterator It = Overloads.begin(),
DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) {
- const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It);
+ const auto *OverloadDecl = cast<FunctionDecl>(*It);
QualType OverloadResultTy = OverloadDecl->getReturnType();
if (IsPlausibleResult(OverloadResultTy))
PlausibleOverloads.addDecl(It.getDecl());
@@ -2604,7 +2603,7 @@ static void notePlausibleOverloads(Sema &S, SourceLocation Loc,
/// putting parentheses after it. Notably, expressions with unary
/// operators can't be because the unary operator will start parsing
/// outside the call.
-static bool IsCallableWithAppend(Expr *E) {
+static bool IsCallableWithAppend(const Expr *E) {
E = E->IgnoreImplicit();
return (!isa<CStyleCastExpr>(E) &&
!isa<UnaryOperator>(E) &&
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index cc5edef7e2f67..575459c3c6920 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19915,7 +19915,7 @@ ObjCContainerDecl *Sema::getObjCDeclContext() const {
return (dyn_cast_or_null<ObjCContainerDecl>(CurContext));
}
-Sema::FunctionEmissionStatus Sema::getEmissionStatus(FunctionDecl *FD,
+Sema::FunctionEmissionStatus Sema::getEmissionStatus(const FunctionDecl *FD,
bool Final) {
assert(FD && "Expected non-null FunctionDecl");
@@ -19933,7 +19933,7 @@ Sema::FunctionEmissionStatus Sema::getEmissionStatus(FunctionDecl *FD,
// We have to check the GVA linkage of the function's *definition* -- if we
// only have a declaration, we don't know whether or not the function will
// be emitted, because (say) the definition could include "inline".
- FunctionDecl *Def = FD->getDefinition();
+ const FunctionDecl *Def = FD->getDefinition();
return Def && !isDiscardableGVALinkage(
getASTContext().GetGVALinkageForFunction(Def));
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 86a4cbf23b765..87ea08748a9f2 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2107,9 +2107,9 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
// b) if the function is a defaulted comparison, we can use the body we
// build when defining it as input to the exception specification
// computation rather than computing a new body.
- if (auto *FPT = Ty->getAs<FunctionProtoType>()) {
+ if (const auto *FPT = Ty->getAs<FunctionProtoType>()) {
if (isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) {
- if (auto *NewFPT = ResolveExceptionSpec(NameInfo.getLoc(), FPT))
+ if (const auto *NewFPT = ResolveExceptionSpec(NameInfo.getLoc(), FPT))
E->setType(Context.getQualifiedType(NewFPT, Ty.getQualifiers()));
}
}
@@ -2119,8 +2119,8 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
!Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, E->getBeginLoc()))
getCurFunction()->recordUseOfWeak(E);
- FieldDecl *FD = dyn_cast<FieldDecl>(D);
- if (IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(D))
+ const auto *FD = dyn_cast<FieldDecl>(D);
+ if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D))
FD = IFD->getAnonField();
if (FD) {
UnusedPrivateFields.remove(FD);
@@ -2131,8 +2131,8 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK,
// C++ [expr.prim]/8: The expression [...] is a bit-field if the identifier
// designates a bit-field.
- if (auto *BD = dyn_cast<BindingDecl>(D))
- if (auto *BE = BD->getBinding())
+ if (const auto *BD = dyn_cast<BindingDecl>(D))
+ if (const auto *BE = BD->getBinding())
E->setObjectKind(BE->getObjectKind());
return E;
@@ -2211,7 +2211,7 @@ static void emitEmptyLookupTypoDiagnostic(
///
/// Return \c true if the error is unrecoverable, or \c false if the caller
/// should attempt to recover using these lookup results.
-bool Sema::DiagnoseDependentMemberLookup(LookupResult &R) {
+bool Sema::DiagnoseDependentMemberLookup(const LookupResult &R) {
// During a default argument instantiation the CurContext points
// to a CXXMethodDecl; but we can't apply a this-> fixit inside a
// function parameter list, hence add an explicit check.
@@ -2219,7 +2219,7 @@ bool Sema::DiagnoseDependentMemberLookup(LookupResult &R) {
!CodeSynthesisContexts.empty() &&
CodeSynthesisContexts.back().Kind ==
CodeSynthesisContext::DefaultFunctionArgumentInstantiation;
- CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
+ const auto *CurMethod = dyn_cast<CXXMethodDecl>(CurContext);
bool isInstance = CurMethod && CurMethod->isInstance() &&
R.getNamingClass() == CurMethod->getParent() &&
!isDefaultArgument;
@@ -2251,7 +2251,7 @@ bool Sema::DiagnoseDependentMemberLookup(LookupResult &R) {
Diag(R.getNameLoc(), DiagID) << R.getLookupName();
}
- for (NamedDecl *D : R)
+ for (const NamedDecl *D : R)
Diag(D->getLocation(), NoteID);
// Return true if we are inside a default argument instantiation
@@ -3009,7 +3009,7 @@ Sema::PerformObjectMemberConversion(Expr *From,
NestedNameSpecifier *Qualifier,
NamedDecl *FoundDecl,
NamedDecl *Member) {
- CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext());
+ const auto *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext());
if (!RD)
return From;
@@ -3034,7 +3034,7 @@ Sema::PerformObjectMemberConversion(Expr *From,
DestType = DestRecordType;
FromRecordType = FromType;
}
- } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) {
+ } else if (const auto *Method = dyn_cast<CXXMethodDecl>(Member)) {
if (Method->isStatic())
return From;
@@ -3154,7 +3154,7 @@ bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
// Turn off ADL when we find certain kinds of declarations during
// normal lookup:
- for (NamedDecl *D : R) {
+ for (const NamedDecl *D : R) {
// C++0x [basic.lookup.argdep]p3:
// -- a declaration of a class member
// Since using decls preserve this property, we check this on the
@@ -3177,9 +3177,7 @@ bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
// -- a declaration that is neither a function or a function
// template
// And also for builtin functions.
- if (isa<FunctionDecl>(D)) {
- FunctionDecl *FDecl = cast<FunctionDecl>(D);
-
+ if (const auto *FDecl = dyn_cast<FunctionDecl>(D)) {
// But also builtin functions.
if (FDecl->getBuiltinID() && FDecl->isImplicit())
return false;
@@ -3313,10 +3311,10 @@ ExprResult Sema::BuildDeclarationNameExpr(
// Handle members of anonymous structs and unions. If we got here,
// and the reference is to a class member indirect field, then this
// must be the subject of a pointer-to-member expression.
- if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD))
- if (!indirectField->isCXXClassMember())
- return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(),
- indirectField);
+ if (auto *IndirectField = dyn_cast<IndirectFieldDecl>(VD);
+ IndirectField && !IndirectField->isCXXClassMember())
+ return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(),
+ IndirectField);
QualType type = VD->getType();
if (type.isNull())
@@ -3779,7 +3777,7 @@ ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) {
if (Literal.hasUDSuffix()) {
// We're building a user-defined literal.
- IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix());
+ const IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix());
SourceLocation UDSuffixLoc =
getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset());
@@ -4242,13 +4240,13 @@ static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T,
/// Check whether E is a pointer from a decayed array type (the decayed
/// pointer type is equal to T) and emit a warning if it is.
static void warnOnSizeofOnArrayDecay(Sema &S, SourceLocation Loc, QualType T,
- Expr *E) {
+ const Expr *E) {
// Don't warn if the operation changed the type.
if (T != E->getType())
return;
// Now look for array decays.
- ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E);
+ const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
if (!ICE || ICE->getCastKind() != CK_ArrayToPointerDecay)
return;
@@ -4331,8 +4329,8 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E,
return true;
if (ExprKind == UETT_SizeOf) {
- if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
- if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) {
+ if (const auto *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) {
+ if (const auto *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) {
QualType OType = PVD->getOriginalType();
QualType Type = PVD->getType();
if (Type->isPointerType() && OType->isArrayType()) {
@@ -4346,7 +4344,7 @@ bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E,
// Warn on "sizeof(array op x)" and "sizeof(x op array)", where the array
// decays into a pointer and returns an unintended result. This is most
// likely a typo for "sizeof(array) op x".
- if (BinaryOperator *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) {
+ if (const auto *BO = dyn_cast<BinaryOperator>(E->IgnoreParens())) {
warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(),
BO->getLHS());
warnOnSizeofOnArrayDecay(*this, BO->getOperatorLoc(), BO->getType(),
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index a495d3ca19895..1fda7269b6459 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -199,7 +199,7 @@ namespace {
const_iterator end() const { return list.end(); }
llvm::iterator_range<const_iterator>
- getNamespacesFor(DeclContext *DC) const {
+ getNamespacesFor(const DeclContext *DC) const {
return llvm::make_range(std::equal_range(begin(), end(),
DC->getPrimaryContext(),
UnqualUsingEntry::Comparator()));
@@ -351,12 +351,12 @@ void LookupResult::deletePaths(CXXBasePaths *Paths) {
/// Get a representative context for a declaration such that two declarations
/// will have the same context if they were found within the same scope.
-static DeclContext *getContextForScopeMatching(Decl *D) {
+static const DeclContext *getContextForScopeMatching(const Decl *D) {
// For function-local declarations, use that function as the context. This
// doesn't account for scopes within the function; the caller must deal with
// those.
- DeclContext *DC = D->getLexicalDeclContext();
- if (DC->isFunctionOrMethod())
+ if (const DeclContext *DC = D->getLexicalDeclContext();
+ DC->isFunctionOrMethod())
return DC;
// Otherwise, look at the semantic context of the declaration. The
@@ -367,15 +367,16 @@ static DeclContext *getContextForScopeMatching(Decl *D) {
/// Determine whether \p D is a better lookup result than \p Existing,
/// given that they declare the same entity.
static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
- NamedDecl *D, NamedDecl *Existing) {
+ const NamedDecl *D,
+ const NamedDecl *Existing) {
// When looking up redeclarations of a using declaration, prefer a using
// shadow declaration over any other declaration of the same entity.
if (Kind == Sema::LookupUsingDeclName && isa<UsingShadowDecl>(D) &&
!isa<UsingShadowDecl>(Existing))
return true;
- auto *DUnderlying = D->getUnderlyingDecl();
- auto *EUnderlying = Existing->getUnderlyingDecl();
+ const auto *DUnderlying = D->getUnderlyingDecl();
+ const auto *EUnderlying = Existing->getUnderlyingDecl();
// If they have
diff erent underlying declarations, prefer a typedef over the
// original type (this happens when two type declarations denote the same
@@ -397,8 +398,8 @@ static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
// FIXME: In the presence of ambiguous default arguments, we should keep both,
// so we can diagnose the ambiguity if the default argument is needed.
// See C++ [over.match.best]p3.
- if (auto *DFD = dyn_cast<FunctionDecl>(DUnderlying)) {
- auto *EFD = cast<FunctionDecl>(EUnderlying);
+ if (const auto *DFD = dyn_cast<FunctionDecl>(DUnderlying)) {
+ const auto *EFD = cast<FunctionDecl>(EUnderlying);
unsigned DMin = DFD->getMinRequiredArguments();
unsigned EMin = EFD->getMinRequiredArguments();
// If D has more default arguments, it is preferred.
@@ -409,8 +410,8 @@ static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
}
// Pick the template with more default template arguments.
- if (auto *DTD = dyn_cast<TemplateDecl>(DUnderlying)) {
- auto *ETD = cast<TemplateDecl>(EUnderlying);
+ if (const auto *DTD = dyn_cast<TemplateDecl>(DUnderlying)) {
+ const auto *ETD = cast<TemplateDecl>(EUnderlying);
unsigned DMin = DTD->getTemplateParameters()->getMinRequiredArguments();
unsigned EMin = ETD->getTemplateParameters()->getMinRequiredArguments();
// If D has more default arguments, it is preferred. Note that default
@@ -433,8 +434,8 @@ static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
// VarDecl can have incomplete array types, prefer the one with more complete
// array type.
- if (VarDecl *DVD = dyn_cast<VarDecl>(DUnderlying)) {
- VarDecl *EVD = cast<VarDecl>(EUnderlying);
+ if (const auto *DVD = dyn_cast<VarDecl>(DUnderlying)) {
+ const auto *EVD = cast<VarDecl>(EUnderlying);
if (EVD->getType()->isIncompleteType() &&
!DVD->getType()->isIncompleteType()) {
// Prefer the decl with a more complete type if visible.
@@ -451,7 +452,7 @@ static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
}
// Pick the newer declaration; it might have a more precise type.
- for (Decl *Prev = DUnderlying->getPreviousDecl(); Prev;
+ for (const Decl *Prev = DUnderlying->getPreviousDecl(); Prev;
Prev = Prev->getPreviousDecl())
if (Prev == EUnderlying)
return true;
@@ -459,7 +460,7 @@ static bool isPreferredLookupResult(Sema &S, Sema::LookupNameKind Kind,
}
/// Determine whether \p D can hide a tag declaration.
-static bool canHideTag(NamedDecl *D) {
+static bool canHideTag(const NamedDecl *D) {
// C++ [basic.scope.declarative]p4:
// Given a set of declarations in a single declarative region [...]
// exactly one declaration shall declare a class name or enumeration name
@@ -492,7 +493,7 @@ void LookupResult::resolveKind() {
// If there's a single decl, we need to examine it to decide what
// kind of lookup this is.
if (N == 1) {
- NamedDecl *D = (*Decls.begin())->getUnderlyingDecl();
+ const NamedDecl *D = (*Decls.begin())->getUnderlyingDecl();
if (isa<FunctionTemplateDecl>(D))
ResultKind = FoundOverloaded;
else if (isa<UnresolvedUsingValueDecl>(D))
@@ -503,21 +504,21 @@ void LookupResult::resolveKind() {
// Don't do any extra resolution if we've already resolved as ambiguous.
if (ResultKind == Ambiguous) return;
- llvm::SmallDenseMap<NamedDecl*, unsigned, 16> Unique;
+ llvm::SmallDenseMap<const NamedDecl *, unsigned, 16> Unique;
llvm::SmallDenseMap<QualType, unsigned, 16> UniqueTypes;
bool Ambiguous = false;
bool HasTag = false, HasFunction = false;
bool HasFunctionTemplate = false, HasUnresolved = false;
- NamedDecl *HasNonFunction = nullptr;
+ const NamedDecl *HasNonFunction = nullptr;
- llvm::SmallVector<NamedDecl*, 4> EquivalentNonFunctions;
+ llvm::SmallVector<const NamedDecl *, 4> EquivalentNonFunctions;
unsigned UniqueTagIndex = 0;
unsigned I = 0;
while (I < N) {
- NamedDecl *D = Decls[I]->getUnderlyingDecl();
+ const NamedDecl *D = Decls[I]->getUnderlyingDecl();
D = cast<NamedDecl>(D->getCanonicalDecl());
// Ignore an invalid declaration unless it's the only one left.
@@ -533,7 +534,7 @@ void LookupResult::resolveKind() {
// and, through using declarations and directives, across scopes. There is
// no ambiguity if they all refer to the same type, so unique based on the
// canonical type.
- if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
+ if (const auto *TD = dyn_cast<TypeDecl>(D)) {
QualType T = getSema().Context.getTypeDeclType(TD);
auto UniqueResult = UniqueTypes.insert(
std::make_pair(getSema().Context.getCanonicalType(T), I));
@@ -608,7 +609,7 @@ void LookupResult::resolveKind() {
// even if they're not visible. (ref?)
if (N > 1 && HideTags && HasTag && !Ambiguous &&
(HasFunction || HasNonFunction || HasUnresolved)) {
- NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0 : N - 1];
+ const NamedDecl *OtherDecl = Decls[UniqueTagIndex ? 0 : N - 1];
if (isa<TagDecl>(Decls[UniqueTagIndex]->getUnderlyingDecl()) &&
getContextForScopeMatching(Decls[UniqueTagIndex])->Equals(
getContextForScopeMatching(OtherDecl)) &&
@@ -1197,9 +1198,9 @@ static bool LookupDirect(Sema &S, LookupResult &R, const DeclContext *DC) {
}
// Performs C++ unqualified lookup into the given file context.
-static bool
-CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context,
- DeclContext *NS, UnqualUsingDirectiveSet &UDirs) {
+static bool CppNamespaceLookup(Sema &S, LookupResult &R, ASTContext &Context,
+ const DeclContext *NS,
+ UnqualUsingDirectiveSet &UDirs) {
assert(NS && NS->isFileContext() && "CppNamespaceLookup() requires namespace!");
@@ -1333,8 +1334,7 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) {
if (!SearchNamespaceScope) {
R.resolveKind();
if (S->isClassScope())
- if (CXXRecordDecl *Record =
- dyn_cast_or_null<CXXRecordDecl>(S->getEntity()))
+ if (auto *Record = dyn_cast_if_present<CXXRecordDecl>(S->getEntity()))
R.setNamingClass(Record);
return true;
}
@@ -5613,15 +5613,15 @@ bool FunctionCallFilterCCC::ValidateCandidate(const TypoCorrection &candidate) {
// unless the method being corrected--or the current DeclContext, if the
// function being corrected is not a method--is a method in the same class
// or a descendent class of the candidate's parent class.
- if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
if (MemberFn || !MD->isStatic()) {
- CXXMethodDecl *CurMD =
+ const auto *CurMD =
MemberFn
- ? dyn_cast_or_null<CXXMethodDecl>(MemberFn->getMemberDecl())
- : dyn_cast_or_null<CXXMethodDecl>(CurContext);
- CXXRecordDecl *CurRD =
+ ? dyn_cast_if_present<CXXMethodDecl>(MemberFn->getMemberDecl())
+ : dyn_cast_if_present<CXXMethodDecl>(CurContext);
+ const CXXRecordDecl *CurRD =
CurMD ? CurMD->getParent()->getCanonicalDecl() : nullptr;
- CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl();
+ const CXXRecordDecl *RD = MD->getParent()->getCanonicalDecl();
if (!CurRD || (CurRD != RD && !CurRD->isDerivedFrom(RD)))
continue;
}
@@ -5640,28 +5640,28 @@ void Sema::diagnoseTypo(const TypoCorrection &Correction,
/// Find which declaration we should import to provide the definition of
/// the given declaration.
-static NamedDecl *getDefinitionToImport(NamedDecl *D) {
- if (VarDecl *VD = dyn_cast<VarDecl>(D))
+static const NamedDecl *getDefinitionToImport(const NamedDecl *D) {
+ if (const auto *VD = dyn_cast<VarDecl>(D))
return VD->getDefinition();
- if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
+ if (const auto *FD = dyn_cast<FunctionDecl>(D))
return FD->getDefinition();
- if (TagDecl *TD = dyn_cast<TagDecl>(D))
+ if (const auto *TD = dyn_cast<TagDecl>(D))
return TD->getDefinition();
- if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
+ if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(D))
return ID->getDefinition();
- if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D))
+ if (const auto *PD = dyn_cast<ObjCProtocolDecl>(D))
return PD->getDefinition();
- if (TemplateDecl *TD = dyn_cast<TemplateDecl>(D))
- if (NamedDecl *TTD = TD->getTemplatedDecl())
+ if (const auto *TD = dyn_cast<TemplateDecl>(D))
+ if (const NamedDecl *TTD = TD->getTemplatedDecl())
return getDefinitionToImport(TTD);
return nullptr;
}
-void Sema::diagnoseMissingImport(SourceLocation Loc, NamedDecl *Decl,
+void Sema::diagnoseMissingImport(SourceLocation Loc, const NamedDecl *Decl,
MissingImportKind MIK, bool Recover) {
// Suggest importing a module providing the definition of this entity, if
// possible.
- NamedDecl *Def = getDefinitionToImport(Decl);
+ const NamedDecl *Def = getDefinitionToImport(Decl);
if (!Def)
Def = Decl;
@@ -5687,7 +5687,7 @@ static std::string getHeaderNameForHeader(Preprocessor &PP, const FileEntry *E,
return (IsSystem ? '<' : '"') + Path + (IsSystem ? '>' : '"');
}
-void Sema::diagnoseMissingImport(SourceLocation UseLoc, NamedDecl *Decl,
+void Sema::diagnoseMissingImport(SourceLocation UseLoc, const NamedDecl *Decl,
SourceLocation DeclLoc,
ArrayRef<Module *> Modules,
MissingImportKind MIK, bool Recover) {
@@ -5738,7 +5738,7 @@ void Sema::diagnoseMissingImport(SourceLocation UseLoc, NamedDecl *Decl,
if (Modules.size() > 1) {
std::string ModuleList;
unsigned N = 0;
- for (Module *M : Modules) {
+ for (const auto *M : Modules) {
ModuleList += "\n ";
if (++N == 5 && N != Modules.size()) {
ModuleList += "[...]";
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index ff5c3cc408f72..be80e933ab1ac 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -2025,9 +2025,9 @@ enum class FunctionEmissionStatus {
};
} // anonymous namespace
-Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
- unsigned DiagID,
- FunctionDecl *FD) {
+Sema::SemaDiagnosticBuilder
+Sema::diagIfOpenMPDeviceCode(SourceLocation Loc, unsigned DiagID,
+ const FunctionDecl *FD) {
assert(LangOpts.OpenMP && LangOpts.OpenMPIsDevice &&
"Expected OpenMP device compilation.");
@@ -2065,7 +2065,7 @@ Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPDeviceCode(SourceLocation Loc,
Sema::SemaDiagnosticBuilder Sema::diagIfOpenMPHostCode(SourceLocation Loc,
unsigned DiagID,
- FunctionDecl *FD) {
+ const FunctionDecl *FD) {
assert(LangOpts.OpenMP && !LangOpts.OpenMPIsDevice &&
"Expected OpenMP host compilation.");
More information about the cfe-commits
mailing list