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 FieldCollector; - typedef llvm::SmallSetVector NamedDeclSetType; + typedef llvm::SmallSetVector 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 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, + llvm::DenseMap, std::vector> 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 FD; + CanonicalDeclPtr 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, + llvm::DenseMap, /* 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 { using FunctionDeclAndLoc = clang::Sema::FunctionDeclAndLoc; - using FDBaseInfo = DenseMapInfo>; + using FDBaseInfo = + DenseMapInfo>; 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(ND)) +static bool isFunctionOrVarDeclExternC(const NamedDecl *ND) { + if (const auto *FD = dyn_cast(ND)) return FD->isExternC(); return cast(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(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(C) ? cast(C) - : dyn_cast_or_null(D); + const FunctionDecl *FD = isa(C) + ? cast(C) + : dyn_cast_or_null(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(E.IgnoreParens())) { - if (const FunctionDecl *Fun = dyn_cast(DeclRef->getDecl())) { + if (const auto *DeclRef = dyn_cast(E.IgnoreParens())) { + if (const auto *Fun = dyn_cast(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(); - if (const FunctionProtoType *FPT = - dyn_cast_or_null(FunTy)) { + if (const auto *FPT = dyn_cast_if_present(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() && @@ -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(*It); + const auto *OverloadDecl = cast(*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(E) && !isa(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(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()) { + if (const auto *FPT = Ty->getAs()) { 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(D); - if (IndirectFieldDecl *IFD = dyn_cast(D)) + const auto *FD = dyn_cast(D); + if (const auto *IFD = dyn_cast(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(D)) - if (auto *BE = BD->getBinding()) + if (const auto *BD = dyn_cast(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(CurContext); + const auto *CurMethod = dyn_cast(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(Member->getDeclContext()); + const auto *RD = dyn_cast(Member->getDeclContext()); if (!RD) return From; @@ -3034,7 +3034,7 @@ Sema::PerformObjectMemberConversion(Expr *From, DestType = DestRecordType; FromRecordType = FromType; } - } else if (CXXMethodDecl *Method = dyn_cast(Member)) { + } else if (const auto *Method = dyn_cast(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(D)) { - FunctionDecl *FDecl = cast(D); - + if (const auto *FDecl = dyn_cast(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(VD)) - if (!indirectField->isCXXClassMember()) - return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), - indirectField); + if (auto *IndirectField = dyn_cast(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(E); + const auto *ICE = dyn_cast(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(E->IgnoreParens())) { - if (ParmVarDecl *PVD = dyn_cast(DeclRef->getFoundDecl())) { + if (const auto *DeclRef = dyn_cast(E->IgnoreParens())) { + if (const auto *PVD = dyn_cast(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(E->IgnoreParens())) { + if (const auto *BO = dyn_cast(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 - 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(D) && !isa(Existing)) return true; - auto *DUnderlying = D->getUnderlyingDecl(); - auto *EUnderlying = Existing->getUnderlyingDecl(); + const auto *DUnderlying = D->getUnderlyingDecl(); + const auto *EUnderlying = Existing->getUnderlyingDecl(); // If they have different 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(DUnderlying)) { - auto *EFD = cast(EUnderlying); + if (const auto *DFD = dyn_cast(DUnderlying)) { + const auto *EFD = cast(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(DUnderlying)) { - auto *ETD = cast(EUnderlying); + if (const auto *DTD = dyn_cast(DUnderlying)) { + const auto *ETD = cast(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(DUnderlying)) { - VarDecl *EVD = cast(EUnderlying); + if (const auto *DVD = dyn_cast(DUnderlying)) { + const auto *EVD = cast(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(D)) ResultKind = FoundOverloaded; else if (isa(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 Unique; + llvm::SmallDenseMap Unique; llvm::SmallDenseMap UniqueTypes; bool Ambiguous = false; bool HasTag = false, HasFunction = false; bool HasFunctionTemplate = false, HasUnresolved = false; - NamedDecl *HasNonFunction = nullptr; + const NamedDecl *HasNonFunction = nullptr; - llvm::SmallVector EquivalentNonFunctions; + llvm::SmallVector EquivalentNonFunctions; unsigned UniqueTagIndex = 0; unsigned I = 0; while (I < N) { - NamedDecl *D = Decls[I]->getUnderlyingDecl(); + const NamedDecl *D = Decls[I]->getUnderlyingDecl(); D = cast(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(D)) { + if (const auto *TD = dyn_cast(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(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(S->getEntity())) + if (auto *Record = dyn_cast_if_present(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(FD)) { + if (const auto *MD = dyn_cast(FD)) { if (MemberFn || !MD->isStatic()) { - CXXMethodDecl *CurMD = + const auto *CurMD = MemberFn - ? dyn_cast_or_null(MemberFn->getMemberDecl()) - : dyn_cast_or_null(CurContext); - CXXRecordDecl *CurRD = + ? dyn_cast_if_present(MemberFn->getMemberDecl()) + : dyn_cast_if_present(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(D)) +static const NamedDecl *getDefinitionToImport(const NamedDecl *D) { + if (const auto *VD = dyn_cast(D)) return VD->getDefinition(); - if (FunctionDecl *FD = dyn_cast(D)) + if (const auto *FD = dyn_cast(D)) return FD->getDefinition(); - if (TagDecl *TD = dyn_cast(D)) + if (const auto *TD = dyn_cast(D)) return TD->getDefinition(); - if (ObjCInterfaceDecl *ID = dyn_cast(D)) + if (const auto *ID = dyn_cast(D)) return ID->getDefinition(); - if (ObjCProtocolDecl *PD = dyn_cast(D)) + if (const auto *PD = dyn_cast(D)) return PD->getDefinition(); - if (TemplateDecl *TD = dyn_cast(D)) - if (NamedDecl *TTD = TD->getTemplatedDecl()) + if (const auto *TD = dyn_cast(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 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.");