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 | c33dec36642bb47c2451f3d33f8cf75f3c99f57c (patch) | |
tree | c1e8a4837896c3a700c087c59a52e051ac549cdf /clang | |
parent | 3e11ebebc83643747f15bcafa28b3b7e232f6cc1 (diff) | |
download | bcm5719-llvm-c33dec36642bb47c2451f3d33f8cf75f3c99f57c.tar.gz bcm5719-llvm-c33dec36642bb47c2451f3d33f8cf75f3c99f57c.zip |
Add support for -Wwrite-strings. Patch by Mike M! Fixes PR 4804.
llvm-svn: 98541
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Basic/LangOptions.h | 3 | ||||
-rw-r--r-- | clang/include/clang/Driver/CC1Options.td | 2 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprObjC.cpp | 2 | ||||
-rw-r--r-- | clang/test/Sema/warn-write-strings.c | 4 | ||||
-rw-r--r-- | clang/test/SemaObjC/warn-write-strings.m | 4 |
7 files changed, 17 insertions, 3 deletions
diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 23e6efe8bd7..fdf69d063f4 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/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/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 7cd26ef04cc..1ecd8d6adaf 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/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/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 64a42bc0ec4..5798f2f7100 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/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/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index cd049a27c1b..a39ba2f7d06 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/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/clang/lib/Sema/SemaExprObjC.cpp b/clang/lib/Sema/SemaExprObjC.cpp index c60455d8456..c98ba435c78 100644 --- a/clang/lib/Sema/SemaExprObjC.cpp +++ b/clang/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/clang/test/Sema/warn-write-strings.c b/clang/test/Sema/warn-write-strings.c new file mode 100644 index 00000000000..938f0be7721 --- /dev/null +++ b/clang/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/clang/test/SemaObjC/warn-write-strings.m b/clang/test/SemaObjC/warn-write-strings.m new file mode 100644 index 00000000000..938f0be7721 --- /dev/null +++ b/clang/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 *'}} |