r191057 - PR17290: Use 'false' macro in fix-it hint for initializing a variable of type

Richard Smith richard-llvm at metafoo.co.uk
Thu Sep 19 17:27:40 PDT 2013


Author: rsmith
Date: Thu Sep 19 19:27:40 2013
New Revision: 191057

URL: http://llvm.org/viewvc/llvm-project?rev=191057&view=rev
Log:
PR17290: Use 'false' macro in fix-it hint for initializing a variable of type
_Bool in C, if the macro is defined. Also teach FixItUtils to look at whether
the macro was defined at the source location for which it is creating a fixit,
rather than looking at whether it's defined *now*. This is especially relevant
for analysis-based warnings which are delayed until end of TU.

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaFixItUtils.cpp
    cfe/trunk/lib/Sema/SemaType.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=191057&r1=191056&r2=191057&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Sep 19 19:27:40 2013
@@ -949,8 +949,9 @@ public:
   bool findMacroSpelling(SourceLocation &loc, StringRef name);
 
   /// \brief Get a string to suggest for zero-initialization of a type.
-  std::string getFixItZeroInitializerForType(QualType T) const;
-  std::string getFixItZeroLiteralForType(QualType T) const;
+  std::string
+  getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const;
+  std::string getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const;
 
   ExprResult Owned(Expr* E) { return E; }
   ExprResult Owned(ExprResult R) { return R; }

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=191057&r1=191056&r2=191057&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Thu Sep 19 19:27:40 2013
@@ -439,22 +439,22 @@ static bool SuggestInitializationFixit(S
     << FixItHint::CreateInsertion(VD->getLocation(), "__block ");
     return true;
   }
-  
+
   // Don't issue a fixit if there is already an initializer.
   if (VD->getInit())
     return false;
-  
-  // Suggest possible initialization (if any).
-  std::string Init = S.getFixItZeroInitializerForType(VariableTy);
-  if (Init.empty())
-    return false;
 
   // Don't suggest a fixit inside macros.
   if (VD->getLocEnd().isMacroID())
     return false;
 
   SourceLocation Loc = S.PP.getLocForEndOfToken(VD->getLocEnd());
-  
+
+  // Suggest possible initialization (if any).
+  std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc);
+  if (Init.empty())
+    return false;
+
   S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
     << FixItHint::CreateInsertion(Loc, Init);
   return true;

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=191057&r1=191056&r2=191057&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Thu Sep 19 19:27:40 2013
@@ -5328,7 +5328,8 @@ void CheckImplicitConversion(Sema &S, Ex
     if (!Loc.isMacroID() || CC.isMacroID())
       S.Diag(Loc, diag::warn_impcast_null_pointer_to_integer)
           << T << clang::SourceRange(CC)
-          << FixItHint::CreateReplacement(Loc, S.getFixItZeroLiteralForType(T));
+          << FixItHint::CreateReplacement(Loc,
+                                          S.getFixItZeroLiteralForType(T, Loc));
   }
 
   if (!Source->isIntegerType() || !Target->isIntegerType())

Modified: cfe/trunk/lib/Sema/SemaFixItUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaFixItUtils.cpp?rev=191057&r1=191056&r2=191057&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaFixItUtils.cpp (original)
+++ cfe/trunk/lib/Sema/SemaFixItUtils.cpp Thu Sep 19 19:27:40 2013
@@ -160,27 +160,33 @@ bool ConversionFixItGenerator::tryToFixC
   return false;
 }
 
-static bool isMacroDefined(const Sema &S, StringRef Name) {
-  return S.PP.getMacroInfo(&S.getASTContext().Idents.get(Name));
+static bool isMacroDefined(const Sema &S, SourceLocation Loc, StringRef Name) {
+  const IdentifierInfo *II = &S.getASTContext().Idents.get(Name);
+  if (!II->hadMacroDefinition()) return false;
+
+  MacroDirective *Macro = S.PP.getMacroDirectiveHistory(II);
+  return Macro && Macro->findDirectiveAtLoc(Loc, S.getSourceManager());
 }
 
-static std::string getScalarZeroExpressionForType(const Type& T, const Sema& S) {
+static std::string getScalarZeroExpressionForType(
+    const Type &T, SourceLocation Loc, const Sema &S) {
   assert(T.isScalarType() && "use scalar types only");
   // Suggest "0" for non-enumeration scalar types, unless we can find a
   // better initializer.
   if (T.isEnumeralType())
     return std::string();
   if ((T.isObjCObjectPointerType() || T.isBlockPointerType()) &&
-      isMacroDefined(S, "nil"))
+      isMacroDefined(S, Loc, "nil"))
     return "nil";
   if (T.isRealFloatingType())
     return "0.0";
-  if (T.isBooleanType() && S.LangOpts.CPlusPlus)
+  if (T.isBooleanType() &&
+      (S.LangOpts.CPlusPlus || isMacroDefined(S, Loc, "false")))
     return "false";
   if (T.isPointerType() || T.isMemberPointerType()) {
     if (S.LangOpts.CPlusPlus11)
       return "nullptr";
-    if (isMacroDefined(S, "NULL"))
+    if (isMacroDefined(S, Loc, "NULL"))
       return "NULL";
   }
   if (T.isCharType())
@@ -194,9 +200,10 @@ static std::string getScalarZeroExpressi
   return "0";
 }
 
-std::string Sema::getFixItZeroInitializerForType(QualType T) const {
+std::string
+Sema::getFixItZeroInitializerForType(QualType T, SourceLocation Loc) const {
   if (T->isScalarType()) {
-    std::string s = getScalarZeroExpressionForType(*T, *this);
+    std::string s = getScalarZeroExpressionForType(*T, Loc, *this);
     if (!s.empty())
       s = " = " + s;
     return s;
@@ -212,6 +219,7 @@ std::string Sema::getFixItZeroInitialize
   return std::string();
 }
 
-std::string Sema::getFixItZeroLiteralForType(QualType T) const {
-  return getScalarZeroExpressionForType(*T, *this);
+std::string
+Sema::getFixItZeroLiteralForType(QualType T, SourceLocation Loc) const {
+  return getScalarZeroExpressionForType(*T, Loc, *this);
 }

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=191057&r1=191056&r2=191057&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Thu Sep 19 19:27:40 2013
@@ -2395,7 +2395,8 @@ static void warnAboutAmbiguousFunction(S
       S.Diag(DeclType.Loc, diag::note_empty_parens_default_ctor)
         << FixItHint::CreateRemoval(ParenRange);
     else {
-      std::string Init = S.getFixItZeroInitializerForType(RT);
+      std::string Init =
+          S.getFixItZeroInitializerForType(RT, ParenRange.getBegin());
       if (Init.empty() && S.LangOpts.CPlusPlus11)
         Init = "{}";
       if (!Init.empty())





More information about the cfe-commits mailing list