Index: clang/include/clang/AST/DeclarationName.h =================================================================== --- clang/include/clang/AST/DeclarationName.h +++ clang/include/clang/AST/DeclarationName.h @@ -118,14 +118,14 @@ 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 @@ /// 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 @@ } /// 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 Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -840,7 +840,7 @@ /// 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; @@ -3242,9 +3242,9 @@ /// 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); @@ -5563,7 +5563,7 @@ DeclarationNameInfo &NameInfo, const TemplateArgumentListInfo *&TemplateArgs); - bool DiagnoseDependentMemberLookup(LookupResult &R); + bool DiagnoseDependentMemberLookup(const LookupResult &R); bool DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, Index: clang/lib/AST/DeclarationName.cpp =================================================================== --- clang/lib/AST/DeclarationName.cpp +++ clang/lib/AST/DeclarationName.cpp @@ -365,7 +365,7 @@ } DeclarationName -DeclarationNameTable::getCXXLiteralOperatorName(IdentifierInfo *II) { +DeclarationNameTable::getCXXLiteralOperatorName(const IdentifierInfo *II) { llvm::FoldingSetNodeID ID; ID.AddPointer(II); Index: clang/lib/Sema/Sema.cpp =================================================================== --- clang/lib/Sema/Sema.cpp +++ clang/lib/Sema/Sema.cpp @@ -1403,9 +1403,7 @@ // 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)) { Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -2107,9 +2107,9 @@ // 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 @@ !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 FieldDecl *FD = dyn_cast(D); + if (const IndirectFieldDecl *IFD = dyn_cast(D)) FD = IFD->getAnonField(); if (FD) { UnusedPrivateFields.remove(FD); @@ -2131,8 +2131,8 @@ // 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 @@ /// /// 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 @@ !CodeSynthesisContexts.empty() && CodeSynthesisContexts.back().Kind == CodeSynthesisContext::DefaultFunctionArgumentInstantiation; - CXXMethodDecl *CurMethod = dyn_cast(CurContext); + const CXXMethodDecl *CurMethod = dyn_cast(CurContext); bool isInstance = CurMethod && CurMethod->isInstance() && R.getNamingClass() == CurMethod->getParent() && !isDefaultArgument; @@ -2251,7 +2251,7 @@ 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 @@ NestedNameSpecifier *Qualifier, NamedDecl *FoundDecl, NamedDecl *Member) { - CXXRecordDecl *RD = dyn_cast(Member->getDeclContext()); + const CXXRecordDecl *RD = dyn_cast(Member->getDeclContext()); if (!RD) return From; @@ -3034,7 +3034,7 @@ DestType = DestRecordType; FromRecordType = FromType; } - } else if (CXXMethodDecl *Method = dyn_cast(Member)) { + } else if (const CXXMethodDecl *Method = dyn_cast(Member)) { if (Method->isStatic()) return From; @@ -3154,7 +3154,7 @@ // 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 @@ // -- 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 @@ // 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 @@ 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 @@ /// 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 @@ 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 @@ // 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(), Index: clang/lib/Sema/SemaLookup.cpp =================================================================== --- clang/lib/Sema/SemaLookup.cpp +++ clang/lib/Sema/SemaLookup.cpp @@ -199,7 +199,7 @@ 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 @@ /// 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 @@ /// 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 @@ // 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 @@ } // 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 @@ // 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 @@ } // 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 @@ } /// 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 @@ // 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 @@ // 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 @@ // 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 @@ // 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 @@ } // 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 @@ 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 @@ // 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 @@ /// 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 @@ 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 @@ 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 += "[...]";