[cfe-commits] r98541 - in /cfe/trunk: include/clang/Basic/LangOptions.h include/clang/Driver/CC1Options.td lib/Frontend/CompilerInvocation.cpp lib/Sema/SemaExpr.cpp lib/Sema/SemaExprObjC.cpp test/Sema/warn-write-strings.c test/SemaObjC/warn-write-strings.m

John McCall rjmccall at apple.com
Mon Mar 15 03:54:44 PDT 2010


Author: rjmccall
Date: Mon Mar 15 05:54:44 2010
New Revision: 98541

URL: http://llvm.org/viewvc/llvm-project?rev=98541&view=rev
Log:
Add support for -Wwrite-strings.  Patch by Mike M!  Fixes PR 4804.


Added:
    cfe/trunk/test/Sema/warn-write-strings.c
    cfe/trunk/test/SemaObjC/warn-write-strings.m
Modified:
    cfe/trunk/include/clang/Basic/LangOptions.h
    cfe/trunk/include/clang/Driver/CC1Options.td
    cfe/trunk/lib/Frontend/CompilerInvocation.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaExprObjC.cpp

Modified: cfe/trunk/include/clang/Basic/LangOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/LangOptions.h?rev=98541&r1=98540&r2=98541&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/LangOptions.h (original)
+++ cfe/trunk/include/clang/Basic/LangOptions.h Mon Mar 15 05:54:44 2010
@@ -44,6 +44,7 @@
 
   unsigned PascalStrings     : 1;  // Allow Pascal strings
   unsigned WritableStrings   : 1;  // Allow writable strings
+  unsigned ConstStrings      : 1;  // Add const qualifier to strings (-Wwrite-strings)
   unsigned LaxVectorConversions : 1;
   unsigned AltiVec           : 1;  // Support AltiVec-style vector initializers.
   unsigned Exceptions        : 1;  // Support exception handling.
@@ -129,7 +130,7 @@
     HexFloats = 0;
     GC = ObjC1 = ObjC2 = ObjCNonFragileABI = ObjCNonFragileABI2 = 0;
     C99 = Microsoft = CPlusPlus = CPlusPlus0x = 0;
-    CXXOperatorNames = PascalStrings = WritableStrings = 0;
+    CXXOperatorNames = PascalStrings = WritableStrings = ConstStrings = 0;
     Exceptions = SjLjExceptions = Freestanding = NoBuiltin = 0;
     NeXTRuntime = 1;
     RTTI = 1;

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=98541&r1=98540&r2=98541&view=diff
==============================================================================
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Mon Mar 15 05:54:44 2010
@@ -197,6 +197,8 @@
   HelpText<"Use colors in diagnostics">;
 def Wno_rewrite_macros : Flag<"-Wno-rewrite-macros">,
   HelpText<"Silence ObjC rewriting warnings">;
+def Wwrite_strings : Flag<"-Wwrite-strings">,
+  HelpText<"Add const qualifier to string literals">;
 def verify : Flag<"-verify">,
   HelpText<"Verify emitted diagnostics and warnings">;
 

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=98541&r1=98540&r2=98541&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Mon Mar 15 05:54:44 2010
@@ -474,6 +474,8 @@
     Res.push_back("-fcatch-undefined-behavior");
   if (Opts.WritableStrings)
     Res.push_back("-fwritable-strings");
+  if (Opts.ConstStrings)
+    Res.push_back("-Wwrite-strings");
   if (!Opts.LaxVectorConversions)
     Res.push_back("-fno-lax-vector-conversions");
   if (Opts.AltiVec)
@@ -1162,6 +1164,7 @@
   Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
   Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
   Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
+  Opts.ConstStrings = Args.hasArg(OPT_Wwrite_strings);
   if (Args.hasArg(OPT_fno_lax_vector_conversions))
     Opts.LaxVectorConversions = 0;
   if (Args.hasArg(OPT_fno_threadsafe_statics))

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=98541&r1=98540&r2=98541&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Mar 15 05:54:44 2010
@@ -370,7 +370,7 @@
   if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
 
   // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
-  if (getLangOptions().CPlusPlus)
+  if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings )
     StrTy.addConst();
 
   // Get an array type for the string, according to C99 6.4.5.  This includes

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=98541&r1=98540&r2=98541&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Mon Mar 15 05:54:44 2010
@@ -106,7 +106,7 @@
     // which is an array type.
     StrTy = Context.CharTy;
     // A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
-    if (getLangOptions().CPlusPlus)
+    if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
       StrTy.addConst();
     StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
                                          ArrayType::Normal, 0);

Added: cfe/trunk/test/Sema/warn-write-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-write-strings.c?rev=98541&view=auto
==============================================================================
--- cfe/trunk/test/Sema/warn-write-strings.c (added)
+++ cfe/trunk/test/Sema/warn-write-strings.c Mon Mar 15 05:54:44 2010
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -Wwrite-strings %s
+
+// PR4804
+char* x = "foo"; // expected-warning {{initializing 'char const [4]' discards qualifiers, expected 'char *'}}

Added: cfe/trunk/test/SemaObjC/warn-write-strings.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-write-strings.m?rev=98541&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/warn-write-strings.m (added)
+++ cfe/trunk/test/SemaObjC/warn-write-strings.m Mon Mar 15 05:54:44 2010
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -verify -fsyntax-only -Wwrite-strings %s
+
+// PR4804
+char* x = "foo"; // expected-warning {{initializing 'char const [4]' discards qualifiers, expected 'char *'}}





More information about the cfe-commits mailing list