[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-22 Thread Yingchi Long via Phabricator via cfe-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2ec79afd8993: [Sema] check InitListExpr format strings like 
{foo} (authored by inclyc).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137839/new/

https://reviews.llvm.org/D137839

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/format-strings.cpp


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,12 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format 
specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is 
not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 
'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
+
+  constexpr const char *fmt2 {"%d"}; // expected-note{{format string is 
defined here}}
+  printf(fmt2, "oops"); // expected-warning{{format specifies type 'int' but 
the argument has type}}
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8585,6 +8585,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,12 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
+
+  constexpr const char *fmt2 {"%d"}; // expected-note{{format string is defined here}}
+  printf(fmt2, "oops"); // expected-warning{{format specifies type 'int' but the argument has type}}
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8585,6 +8585,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-22 Thread Yingchi Long via Phabricator via cfe-commits
inclyc updated this revision to Diff 477279.
inclyc added a comment.

Address comments

- add a test to coverage warnings if appropriate


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137839/new/

https://reviews.llvm.org/D137839

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/format-strings.cpp


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,12 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format 
specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is 
not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 
'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
+
+  constexpr const char *fmt2 {"%d"}; // expected-note{{format string is 
defined here}}
+  printf(fmt2, "oops"); // expected-warning{{format specifies type 'int' but 
the argument has type}}
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8585,6 +8585,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,12 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
+
+  constexpr const char *fmt2 {"%d"}; // expected-note{{format string is defined here}}
+  printf(fmt2, "oops"); // expected-warning{{format specifies type 'int' but the argument has type}}
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8585,6 +8585,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-22 Thread Aaron Ballman via Phabricator via cfe-commits
aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

In D137839#3944574 , @inclyc wrote:

> Do we need release notes here? This patch is just an improvement to 
> https://reviews.llvm.org/D130906

I don't think we need release notes in this case, the existing ones work well 
enough.

LGTM with a request for an additional test case (you can add it when you land 
the changes, I don't expect surprises from the test, it's just for regression 
coverage).




Comment at: clang/test/SemaCXX/format-strings.cpp:208
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
 }

Cam you also add a test case for:
```
printf(fmt, "oops", 1.0f);
```
to show that we still issue diagnostics when they're appropriate?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137839/new/

https://reviews.llvm.org/D137839

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-22 Thread Yingchi Long via Phabricator via cfe-commits
inclyc added a comment.

Do we need release notes here? This patch is just an improvement to 
https://reviews.llvm.org/D130906


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137839/new/

https://reviews.llvm.org/D137839

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-17 Thread Yingchi Long via Phabricator via cfe-commits
inclyc added a comment.

ping :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137839/new/

https://reviews.llvm.org/D137839

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D137839: [Sema] check InitListExpr format strings like {"foo"}

2022-11-11 Thread Yingchi Long via Phabricator via cfe-commits
inclyc created this revision.
Herald added a project: All.
inclyc added a reviewer: aaron.ballman.
inclyc published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Adds InitListExpr case in format string checks.

e.g.

  int sprintf(char *__restrict, const char * __restrict, ...);
  
  int foo()
  {
  char data[100];
  constexpr const char* fmt2{"%d"};  // no-warning
  sprintf(data, fmt2, 123);
  }

Fixes: https://github.com/llvm/llvm-project/issues/58900


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137839

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/format-strings.cpp


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,9 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format 
specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is 
not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 
'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8554,6 +8554,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was


Index: clang/test/SemaCXX/format-strings.cpp
===
--- clang/test/SemaCXX/format-strings.cpp
+++ clang/test/SemaCXX/format-strings.cpp
@@ -203,6 +203,9 @@
   printf(string_linebreak(), 1, 2, 3, 4); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
   printf(not_literal(), 1, 2, 3, 4); // expected-warning {{format string is not a string literal}}
   printf(wrap_constexpr(), 1, 2); // expected-warning {{format specifies type 'char *' but the argument has type 'int'}}
+
+  constexpr const char *fmt {"%d%d"};
+  printf(fmt, 1, 1); // no-warning
 }
 
 
Index: clang/lib/Sema/SemaChecking.cpp
===
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -8554,6 +8554,15 @@
 return SLCT_UncheckedLiteral;
 
   switch (E->getStmtClass()) {
+  case Stmt::InitListExprClass:
+// Handle expressions like {"foobar"}.
+if (const clang::Expr *SLE = maybeConstEvalStringLiteral(S.Context, E)) {
+  return checkFormatStringExpr(S, SLE, Args, APK, format_idx, firstDataArg,
+   Type, CallType, /*InFunctionCall*/ false,
+   CheckedVarArgs, UncoveredArg, Offset,
+   IgnoreStringsWithoutSpecifiers);
+}
+return SLCT_NotALiteral;
   case Stmt::BinaryConditionalOperatorClass:
   case Stmt::ConditionalOperatorClass: {
 // The expression is a literal if both sub-expressions were, and it was
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits