diff options
author | John McCall <rjmccall@apple.com> | 2010-03-15 10:54:44 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-03-15 10:54:44 +0000 |
commit | 4b7a834e0fecddd9eaf1f4567867c718e4eebf50 (patch) | |
tree | 471936c6aa4ed9131fd806b34e4d4df42807c62d | |
parent | b6217665c6a987f2d6c8665fd70365d7719ac4df (diff) |
Add support for -Wwrite-strings. Patch by Mike M! Fixes PR 4804.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98541 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/LangOptions.h | 3 | ||||
-rw-r--r-- | include/clang/Driver/CC1Options.td | 2 | ||||
-rw-r--r-- | lib/Frontend/CompilerInvocation.cpp | 3 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 2 | ||||
-rw-r--r-- | lib/Sema/SemaExprObjC.cpp | 2 | ||||
-rw-r--r-- | test/Sema/warn-write-strings.c | 4 | ||||
-rw-r--r-- | test/SemaObjC/warn-write-strings.m | 4 |
7 files changed, 17 insertions, 3 deletions
diff --git a/include/clang/Basic/LangOptions.h b/include/clang/Basic/LangOptions.h index 23e6efe8bd..fdf69d063f 100644 --- a/include/clang/Basic/LangOptions.h +++ b/include/clang/Basic/LangOptions.h @@ -44,6 +44,7 @@ public: 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 @@ public: 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; diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td index 7cd26ef04c..1ecd8d6ada 100644 --- a/include/clang/Driver/CC1Options.td +++ b/include/clang/Driver/CC1Options.td @@ -197,6 +197,8 @@ def fcolor_diagnostics : Flag<"-fcolor-diagnostics">, 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">; diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 64a42bc0ec..5798f2f710 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -474,6 +474,8 @@ static void LangOptsToArgs(const LangOptions &Opts, 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 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, 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)) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index cd049a27c1..a39ba2f7d0 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -370,7 +370,7 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { 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 diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index c60455d845..c98ba435c7 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -106,7 +106,7 @@ Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, // 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); diff --git a/test/Sema/warn-write-strings.c b/test/Sema/warn-write-strings.c new file mode 100644 index 0000000000..938f0be772 --- /dev/null +++ b/test/Sema/warn-write-strings.c @@ -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 *'}} diff --git a/test/SemaObjC/warn-write-strings.m b/test/SemaObjC/warn-write-strings.m new file mode 100644 index 0000000000..938f0be772 --- /dev/null +++ b/test/SemaObjC/warn-write-strings.m @@ -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 *'}} |