Skip to content

Commit

Permalink
Make -Wstring-plus-int warns even if when the result is not out of bo…
Browse files Browse the repository at this point in the history
…unds

Summary: Patch by Arnaud Bienner

Reviewers: sylvestre.ledru, thakis, serge-sans-paille

Reviewed By: thakis

Subscribers: arphaman, dyung, anemet, llvm-commits, cfe-commits

Differential Revision: https://reviews.llvm.org/D55382

llvm-svn: 350335
  • Loading branch information
ArnaudBienner committed Jan 3, 2019
1 parent 0785e73 commit 57fc958
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
4 changes: 2 additions & 2 deletions clang/bindings/python/tests/cindex/test_diagnostics.py
Expand Up @@ -51,7 +51,7 @@ def test_diagnostic_fixit(self):
self.assertEqual(tu.diagnostics[0].fixits[0].value, '.f0 = ')

def test_diagnostic_range(self):
tu = get_tu('void f() { int i = "a" + 1; }')
tu = get_tu('void f() { int i = "a"; }')
self.assertEqual(len(tu.diagnostics), 1)
self.assertEqual(tu.diagnostics[0].severity, Diagnostic.Warning)
self.assertEqual(tu.diagnostics[0].location.line, 1)
Expand All @@ -63,7 +63,7 @@ def test_diagnostic_range(self):
self.assertEqual(tu.diagnostics[0].ranges[0].start.line, 1)
self.assertEqual(tu.diagnostics[0].ranges[0].start.column, 20)
self.assertEqual(tu.diagnostics[0].ranges[0].end.line, 1)
self.assertEqual(tu.diagnostics[0].ranges[0].end.column, 27)
self.assertEqual(tu.diagnostics[0].ranges[0].end.column, 23)
with self.assertRaises(IndexError):
tu.diagnostics[0].ranges[1].start.line

Expand Down
10 changes: 0 additions & 10 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -9143,16 +9143,6 @@ static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc,
if (!IsStringPlusInt || IndexExpr->isValueDependent())
return;

Expr::EvalResult Result;
if (IndexExpr->EvaluateAsInt(Result, Self.getASTContext())) {
llvm::APSInt index = Result.Val.getInt();
unsigned StrLenWithNull = StrExpr->getLength() + 1;
if (index.isNonNegative() &&
index <= llvm::APSInt(llvm::APInt(index.getBitWidth(), StrLenWithNull),
index.isUnsigned()))
return;
}

SourceRange DiagRange(LHSExpr->getBeginLoc(), RHSExpr->getEndLoc());
Self.Diag(OpLoc, diag::warn_string_plus_int)
<< DiagRange << IndexExpr->IgnoreImpCasts()->getType();
Expand Down
23 changes: 11 additions & 12 deletions clang/test/SemaCXX/string-plus-int.cpp
Expand Up @@ -31,37 +31,36 @@ void f(int index) {
consume("foo" + 5); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume("foo" + index); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume("foo" + kMyEnum); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume("foo" + kMySmallEnum); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

consume(5 + "foo"); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume(index + "foo"); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume(kMyEnum + "foo"); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume(kMySmallEnum + "foo"); // expected-warning {{adding 'MyEnum' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

// FIXME: suggest replacing with "foo"[5]
consumeChar(*("foo" + 5)); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consumeChar(*(5 + "foo")); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

consume(L"foo" + 5); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume(L"foo" + 2); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

consume("foo" + 3); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume("foo" + 4); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}
consume("\pfoo" + 4); // expected-warning {{adding 'int' to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

#define A "foo"
#define B "bar"
consume(A B + sizeof(A) - 1); // expected-warning {{to a string does not append to the string}} expected-note {{use array indexing to silence this warning}}

// Should not warn.
consume(&("foo"[3]));
consume(&("foo"[index]));
consume(&("foo"[kMyEnum]));
consume("foo" + kMySmallEnum);
consume(kMySmallEnum + "foo");

consume(L"foo" + 2);

consume("foo" + 3); // Points at the \0
consume("foo" + 4); // Points 1 past the \0, which is legal too.
consume("\pfoo" + 4); // Pascal strings don't have a trailing \0, but they
// have a leading length byte, so this is fine too.

consume("foo" + kMyOperatorOverloadedEnum);
consume(kMyOperatorOverloadedEnum + "foo");

#define A "foo"
#define B "bar"
consume(A B + sizeof(A) - 1);
}

template <typename T>
Expand Down

0 comments on commit 57fc958

Please sign in to comment.