[Sema] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).

llvm-svn: 329346
diff --git a/clang/lib/Sema/CodeCompleteConsumer.cpp b/clang/lib/Sema/CodeCompleteConsumer.cpp
index d012e55..f88ee1a 100644
--- a/clang/lib/Sema/CodeCompleteConsumer.cpp
+++ b/clang/lib/Sema/CodeCompleteConsumer.cpp
@@ -285,11 +285,11 @@
   assert(NumChunks <= 0xffff);
   assert(NumAnnotations <= 0xffff);
 
-  Chunk *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
+  auto *StoredChunks = reinterpret_cast<Chunk *>(this + 1);
   for (unsigned I = 0; I != NumChunks; ++I)
     StoredChunks[I] = Chunks[I];
 
-  const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
+  const auto **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
   for (unsigned I = 0; I != NumAnnotations; ++I)
     StoredAnnotations[I] = Annotations[I];
 }
@@ -340,14 +340,14 @@
   // FIXME: It would be more efficient to teach Twine to tell us its size and
   // then add a routine there to fill in an allocated char* with the contents
   // of the string.
-  char *Mem = (char *)Allocate(Ref.size() + 1, 1);
+  auto *Mem = (char *)Allocate(Ref.size() + 1, 1);
   std::copy(Ref.begin(), Ref.end(), Mem);
   Mem[Ref.size()] = 0;
   return Mem;
 }
 
 StringRef CodeCompletionTUInfo::getParentName(const DeclContext *DC) {
-  const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
+  const auto *ND = dyn_cast<NamedDecl>(DC);
   if (!ND)
     return {};
   
@@ -364,7 +364,7 @@
   // Find the interesting names.
   SmallVector<const DeclContext *, 2> Contexts;
   while (DC && !DC->isFunctionOrMethod()) {
-    if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
+    if (const auto *ND = dyn_cast<NamedDecl>(DC)) {
       if (ND->getIdentifier())
         Contexts.push_back(DC);
     }
@@ -384,10 +384,10 @@
       }
       
       const DeclContext *CurDC = Contexts[I-1];
-      if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
+      if (const auto *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
         CurDC = CatImpl->getCategoryDecl();
       
-      if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
+      if (const auto *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
         const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
         if (!Interface) {
           // Assign an empty StringRef but with non-null data to distinguish
@@ -413,7 +413,7 @@
       sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
           sizeof(const char *) * Annotations.size(),
       alignof(CodeCompletionString));
-  CodeCompletionString *Result 
+  auto *Result 
     = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
                                      Priority, Availability,
                                      Annotations.data(), Annotations.size(),
@@ -463,7 +463,7 @@
   if (DC->isFunctionOrMethod())
     return;
   
-  const NamedDecl *ND = dyn_cast<NamedDecl>(DC);
+  const auto *ND = dyn_cast<NamedDecl>(DC);
   if (!ND)
     return;
   
@@ -655,7 +655,7 @@
       break;
     }
 
-    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
+    if (const auto *Function = dyn_cast<FunctionDecl>(Declaration))
       if (Function->isDeleted())
         Availability = CXAvailability_NotAvailable;
       
diff --git a/clang/lib/Sema/DeclSpec.cpp b/clang/lib/Sema/DeclSpec.cpp
index 2fad5a1..292898c 100644
--- a/clang/lib/Sema/DeclSpec.cpp
+++ b/clang/lib/Sema/DeclSpec.cpp
@@ -1,4 +1,4 @@
-//===--- DeclSpec.cpp - Declaration Specifier Semantic Analysis -----------===//
+//===- DeclSpec.cpp - Declaration Specifier Semantic Analysis -------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -13,20 +13,35 @@
 
 #include "clang/Sema/DeclSpec.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
 #include "clang/AST/LocInfoType.h"
+#include "clang/AST/PrettyPrinter.h"
+#include "clang/AST/Type.h"
 #include "clang/AST/TypeLoc.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/ExceptionSpecificationType.h"
+#include "clang/Basic/LLVM.h"
 #include "clang/Basic/LangOptions.h"
+#include "clang/Basic/OpenCLOptions.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
+#include "clang/Sema/AttributeList.h"
 #include "clang/Sema/ParsedTemplate.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/ErrorHandling.h"
+#include <cassert>
 #include <cstring>
-using namespace clang;
+#include <utility>
 
+using namespace clang;
 
 void UnqualifiedId::setTemplateId(TemplateIdAnnotation *TemplateId) {
   assert(TemplateId && "NULL template-id annotation?");
@@ -135,14 +150,14 @@
 
 SourceLocation CXXScopeSpec::getLastQualifierNameLoc() const {
   if (!Builder.getRepresentation())
-    return SourceLocation();
+    return {};
   return Builder.getTemporary().getLocalBeginLoc();
 }
 
 NestedNameSpecifierLoc 
 CXXScopeSpec::getWithLocInContext(ASTContext &Context) const {
   if (!Builder.getRepresentation())
-    return NestedNameSpecifierLoc();
+    return {};
   
   return Builder.getWithLocInContext(Context);
 }
@@ -289,7 +304,7 @@
   Name.EndLocation = RSquareLoc;
 
   // Allocate storage for bindings and stash them away.
-  if (Bindings.size()) {
+  if (!Bindings.empty()) {
     if (!InlineStorageUsed &&
         Bindings.size() <= llvm::array_lengthof(InlineBindings)) {
       BindingGroup.Bindings = InlineBindings;
@@ -306,8 +321,8 @@
 }
 
 bool Declarator::isDeclarationOfFunction() const {
-  for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
-    switch (DeclTypeInfo[i].Kind) {
+  for (const auto &i : DeclTypeInfo) {
+    switch (i.Kind) {
     case DeclaratorChunk::Function:
       return true;
     case DeclaratorChunk::Paren:
@@ -373,7 +388,7 @@
       if (QT.isNull())
         return false;
       
-      if (const LocInfoType *LIT = dyn_cast<LocInfoType>(QT))
+      if (const auto *LIT = dyn_cast<LocInfoType>(QT))
         QT = LIT->getType();
 
       if (QT.isNull())
@@ -407,7 +422,6 @@
 
 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
 /// declaration specifier includes.
-///
 unsigned DeclSpec::getParsedSpecifiers() const {
   unsigned Res = 0;
   if (StorageClassSpec != SCS_unspecified ||
@@ -482,7 +496,6 @@
   llvm_unreachable("Unknown typespec!");
 }
 
-
 const char *DeclSpec::getSpecifierName(TSS S) {
   switch (S) {
   case TSS_unspecified: return "unspecified";
@@ -777,16 +790,14 @@
 bool DeclSpec::SetTypePipe(bool isPipe, SourceLocation Loc,
                            const char *&PrevSpec, unsigned &DiagID,
                            const PrintingPolicy &Policy) {
-
   if (TypeSpecType != TST_unspecified) {
     PrevSpec = DeclSpec::getSpecifierName((TST)TypeSpecType, Policy);
     DiagID = diag::err_invalid_decl_spec_combination;
     return true;
   }
 
-  if (isPipe) {
+  if (isPipe)
     TypeSpecPipe = TSP_pipe;
-  }
   return false;
 }
 
@@ -975,7 +986,7 @@
   writtenBS.Type = getTypeSpecType();
   // Search the list of attributes for the presence of a mode attribute.
   writtenBS.ModeAttr = false;
-  AttributeList* attrs = getAttributes().getList();
+  AttributeList *attrs = getAttributes().getList();
   while (attrs) {
     if (attrs->getKind() == AttributeList::AT_Mode) {
       writtenBS.ModeAttr = true;
diff --git a/clang/lib/Sema/IdentifierResolver.cpp b/clang/lib/Sema/IdentifierResolver.cpp
index f31c517..1b3487d 100644
--- a/clang/lib/Sema/IdentifierResolver.cpp
+++ b/clang/lib/Sema/IdentifierResolver.cpp
@@ -147,7 +147,7 @@
   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
     updatingIdentifier(*II);
 
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
 
   if (!Ptr) {
     Name.setFETokenInfo(D);
@@ -159,7 +159,7 @@
   if (isDeclPtr(Ptr)) {
     Name.setFETokenInfo(nullptr);
     IDI = &(*IdDeclInfos)[Name];
-    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
+    auto *PrevD = static_cast<NamedDecl *>(Ptr);
     IDI->AddDecl(PrevD);
   } else
     IDI = toIdDeclInfo(Ptr);
@@ -172,7 +172,7 @@
   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
     updatingIdentifier(*II);
   
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
   
   if (!Ptr) {
     AddDecl(D);
@@ -184,7 +184,7 @@
     // as appropriate.
     if (Pos == iterator()) {
       // Add the new declaration before the existing declaration.
-      NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
+      auto *PrevD = static_cast<NamedDecl *>(Ptr);
       RemoveDecl(PrevD);
       AddDecl(D);
       AddDecl(PrevD);
@@ -213,7 +213,7 @@
   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
     updatingIdentifier(*II);
 
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
 
   assert(Ptr && "Didn't find this decl on its identifier's chain!");
 
@@ -232,11 +232,11 @@
   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
     readingIdentifier(*II);
     
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
   if (!Ptr) return end();
 
   if (isDeclPtr(Ptr))
-    return iterator(static_cast<NamedDecl*>(Ptr));
+    return iterator(static_cast<NamedDecl *>(Ptr));
 
   IdDeclInfo *IDI = toIdDeclInfo(Ptr);
 
@@ -304,7 +304,7 @@
   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
     readingIdentifier(*II);
   
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
     
   if (!Ptr) {
     Name.setFETokenInfo(D);
@@ -314,7 +314,7 @@
   IdDeclInfo *IDI;
   
   if (isDeclPtr(Ptr)) {
-    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
+    auto *PrevD = static_cast<NamedDecl *>(Ptr);
     
     switch (compareDeclarations(PrevD, D)) {
     case DMK_Different:
@@ -397,7 +397,7 @@
 /// It creates a new IdDeclInfo if one was not created before for this id.
 IdentifierResolver::IdDeclInfo &
 IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
-  void *Ptr = Name.getFETokenInfo<void>();
+  auto *Ptr = Name.getFETokenInfo<void>();
 
   if (Ptr) return *toIdDeclInfo(Ptr);
 
@@ -415,7 +415,7 @@
 
 void IdentifierResolver::iterator::incrementSlowCase() {
   NamedDecl *D = **this;
-  void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
+  auto *InfoPtr = D->getDeclName().getFETokenInfo<void>();
   assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
   IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
 
diff --git a/clang/lib/Sema/SemaFixItUtils.cpp b/clang/lib/Sema/SemaFixItUtils.cpp
index 714fbed..3cc26c0 100644
--- a/clang/lib/Sema/SemaFixItUtils.cpp
+++ b/clang/lib/Sema/SemaFixItUtils.cpp
@@ -1,4 +1,4 @@
-//===--- SemaFixItUtils.cpp - Sema FixIts ---------------------------------===//
+//===- SemaFixItUtils.cpp - Sema FixIts -----------------------------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -11,12 +11,24 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "clang/Sema/SemaFixItUtils.h"
 #include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclarationName.h"
+#include "clang/AST/Expr.h"
 #include "clang/AST/ExprCXX.h"
 #include "clang/AST/ExprObjC.h"
+#include "clang/AST/Type.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/LangOptions.h"
+#include "clang/Basic/SourceLocation.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/Sema.h"
-#include "clang/Sema/SemaFixItUtils.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Casting.h"
+#include <cassert>
+#include <string>
 
 using namespace clang;
 
@@ -91,7 +103,7 @@
 
   // Check if the argument needs to be dereferenced:
   //   (type * -> type) or (type * -> type &).
-  if (const PointerType *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
+  if (const auto *FromPtrTy = dyn_cast<PointerType>(FromQTy)) {
     OverloadFixItKind FixKind = OFIK_Dereference;
 
     bool CanConvert = CompareTypes(
@@ -103,7 +115,7 @@
           isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull))
         return false;
 
-      if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
+      if (const auto *UO = dyn_cast<UnaryOperator>(Expr)) {
         if (UO->getOpcode() == UO_AddrOf) {
           FixKind = OFIK_RemoveTakeAddress;
           Hints.push_back(FixItHint::CreateRemoval(
@@ -136,8 +148,7 @@
     CanConvert = CompareTypes(S.Context.getPointerType(FromQTy), ToQTy,
                               S, Begin, VK_RValue);
     if (CanConvert) {
-
-      if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(Expr)) {
+      if (const auto *UO = dyn_cast<UnaryOperator>(Expr)) {
         if (UO->getOpcode() == UO_Deref) {
           FixKind = OFIK_RemoveDereference;
           Hints.push_back(FixItHint::CreateRemoval(
@@ -171,7 +182,7 @@
   // Suggest "0" for non-enumeration scalar types, unless we can find a
   // better initializer.
   if (T.isEnumeralType())
-    return std::string();
+    return {};
   if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
       isMacroDefined(S, Loc, "nil"))
     return "nil";
@@ -208,12 +219,12 @@
 
   const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
   if (!RD || !RD->hasDefinition())
-    return std::string();
+    return {};
   if (LangOpts.CPlusPlus11 && !RD->hasUserProvidedDefaultConstructor())
     return "{}";
   if (RD->isAggregate())
     return " = {}";
-  return std::string();
+  return {};
 }
 
 std::string