[PATCH] D158112: [-Wunsafe-buffer-usage] Fix assertion failure in case of BindingDecl

2023-08-17 Thread Rashmi Mudduluru 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 rG2afcda693acb: [-Wunsafe-buffer-usage] Fix assertion failure 
in case of BindingDecl (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D158112

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -78,3 +78,23 @@
   auto [x, y] = a;
   x = 9;
 }
+
+void test_claim_use_multiple() {
+  int *a = new int[8];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[6] = 9;  // expected-note{{used in buffer access here}}
+  a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
+}
+
+struct S
+{
+int *x;
+};
+ 
+S f() { return S{new int[4]}; }
+
+void test_struct_claim_use() {
+  auto [x] = f();
+  x[6] = 8;  // expected-warning{{unsafe buffer access}}
+  x++;  // expected-warning{{unsafe pointer arithmetic}}
+}
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -380,6 +380,10 @@
   }
 };
 
+static auto toSupportedVariable() {
+  return to(varDecl());
+}
+
 using FixableGadgetList = std::vector>;
 using WarningGadgetList = std::vector>;
 
@@ -566,7 +570,8 @@
   static Matcher matcher() {
 auto PtrInitStmt = declStmt(hasSingleDecl(varDecl(
  hasInitializer(ignoringImpCasts(declRefExpr(
-  hasPointerType()).
+  hasPointerType(),
+toSupportedVariable()).
   bind(PointerInitRHSTag.
   bind(PointerInitLHSTag)));
 
@@ -616,10 +621,10 @@
   static Matcher matcher() {
 auto PtrAssignExpr = binaryOperator(allOf(hasOperatorName("="),
   hasRHS(ignoringParenImpCasts(declRefExpr(hasPointerType(),
-   to(varDecl())).
+   toSupportedVariable()).
bind(PointerAssignRHSTag))),
hasLHS(declRefExpr(hasPointerType(),
-  to(varDecl())).
+  toSupportedVariable()).
   bind(PointerAssignLHSTag;
 
 return stmt(isInUnspecifiedUntypedContext(PtrAssignExpr));
@@ -691,7 +696,8 @@
   static Matcher matcher() {
 auto ArrayOrPtr = anyOf(hasPointerType(), hasArrayType());
 auto BaseIsArrayOrPtrDRE =
-hasBase(ignoringParenImpCasts(declRefExpr(ArrayOrPtr)));
+hasBase(ignoringParenImpCasts(declRefExpr(ArrayOrPtr,
+  toSupportedVariable(;
 auto Target =
 arraySubscriptExpr(BaseIsArrayOrPtrDRE).bind(ULCArraySubscriptTag);
 
@@ -733,7 +739,8 @@
   static Matcher matcher() {
 auto ArrayOrPtr = anyOf(hasPointerType(), hasArrayType());
 auto target = expr(
-ignoringParenImpCasts(declRefExpr(allOf(ArrayOrPtr, to(varDecl(.bind(DeclRefExprTag)));
+ignoringParenImpCasts(declRefExpr(allOf(ArrayOrPtr, 
+  toSupportedVariable())).bind(DeclRefExprTag)));
 return stmt(isInUnspecifiedPointerContext(target));
   }
 
@@ -769,7 +776,7 @@
 unaryOperator(
 hasOperatorName("*"),
 has(expr(ignoringParenImpCasts(
-declRefExpr(to(varDecl())).bind(BaseDeclRefExprTag)
+declRefExpr(toSupportedVariable()).bind(BaseDeclRefExprTag)
 .bind(OperatorTag);
 
 return expr(isInUnspecifiedLvalueContext(Target));
@@ -809,7 +816,8 @@
 return expr(isInUnspecifiedPointerContext(expr(ignoringImpCasts(
 unaryOperator(hasOperatorName("&"),
   hasUnaryOperand(arraySubscriptExpr(
-  hasBase(ignoringParenImpCasts(declRefExpr())
+  hasBase(ignoringParenImpCasts(declRefExpr(
+  toSupportedVariable()))
 .bind(UPCAddressofArraySubscriptTag);
   }
 
@@ -961,7 +969,8 @@
 // things right.
 return 

[PATCH] D157441: [-Wunsafe-buffer-usage] Use `Strategy` to determine whether to fix a parameter

2023-08-17 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2719
+  Strategy NaiveStrategy = getNaiveStrategy(
+  llvm::make_range(VisitedVars.begin(), VisitedVars.end()));
   VariableGroupsManagerImpl VarGrpMgr(Groups, VarGrpMap, GrpsUnionForParms);

ziqingluo-90 wrote:
> `VisitedVars` are the set of variables on the computed graph.  The previous 
> `UnsafeVars` is a superset of it, including safe variables that have 
> `FixableGadget`s discovered.  We do not want to assign strategies other than 
> `Won't fix` to those safe variables.
Would it make more sense (make the code more readable) if we renamed 
`UnsafeVars` to say, `FixableVars`?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D157441

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


[PATCH] D157018: [-Wunsafe-buffer-usage] Replace assert that declarations are always found

2023-08-15 Thread Rashmi Mudduluru 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 rGcf1c64b94d94: [-Wunsafe-buffer-usage] Replace assert that 
declarations are always found (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D157018

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -66,3 +66,15 @@
   b++;  // expected-note{{used in pointer arithmetic here}} \
 // debug-note{{safe buffers debug: failed to produce fixit for 'b' : 
has an unclaimed use}}
 }
+
+int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for 
buffer access}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'a' : neither 
local nor a parameter}}
+void test_globals() {
+  a[7] = 4;  // expected-note{{used in buffer access here}}
+}
+
+void test_decomp_decl() {
+  int a[2] = {1, 2};
+  auto [x, y] = a;
+  x = 9;
+}
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -888,7 +888,8 @@
 
   const DeclStmt *lookupDecl(const VarDecl *VD) const {
 auto It = Defs.find(VD);
-assert(It != Defs.end() && "Definition never discovered!");
+if (It == Defs.end())
+  return nullptr;
 return It->second;
   }
 };
@@ -2053,7 +2054,10 @@
  ASTContext ,
  UnsafeBufferUsageHandler ) {
   const DeclStmt *DS = Tracker.lookupDecl(VD);
-  assert(DS && "Fixing non-local variables not implemented yet!");
+  if (!DS) {
+DEBUG_NOTE_DECL_FAIL(VD, " : variables declared this way not implemented 
yet");
+return {};
+  }
   if (!DS->isSingleDecl()) {
 // FIXME: to support handling multiple `VarDecl`s in a single `DeclStmt`
 DEBUG_NOTE_DECL_FAIL(VD, " : multiple VarDecls");


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -66,3 +66,15 @@
   b++;  // expected-note{{used in pointer arithmetic here}} \
 // debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
 }
+
+int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'a' : neither local nor a parameter}}
+void test_globals() {
+  a[7] = 4;  // expected-note{{used in buffer access here}}
+}
+
+void test_decomp_decl() {
+  int a[2] = {1, 2};
+  auto [x, y] = a;
+  x = 9;
+}
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -888,7 +888,8 @@
 
   const DeclStmt *lookupDecl(const VarDecl *VD) const {
 auto It = Defs.find(VD);
-assert(It != Defs.end() && "Definition never discovered!");
+if (It == Defs.end())
+  return nullptr;
 return It->second;
   }
 };
@@ -2053,7 +2054,10 @@
  ASTContext ,
  UnsafeBufferUsageHandler ) {
   const DeclStmt *DS = Tracker.lookupDecl(VD);
-  assert(DS && "Fixing non-local variables not implemented yet!");
+  if (!DS) {
+DEBUG_NOTE_DECL_FAIL(VD, " : variables declared this way not implemented yet");
+return {};
+  }
   if (!DS->isSingleDecl()) {
 // FIXME: to support handling multiple `VarDecl`s in a single `DeclStmt`
 DEBUG_NOTE_DECL_FAIL(VD, " : multiple VarDecls");
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156762: [-Wunsafe-buffer-usage][NFC] Refactor `getFixIts`---where fix-its are generated

2023-08-03 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2145
 
-static bool impossibleToFixForVar(const FixableGadgetSets ,
-  const Strategy ,
-  const VarDecl * Var) {
-  for (const auto  : FixablesForAllVars.byVar.find(Var)->second) {
-std::optional Fixits = F->getFixits(S);
-if (!Fixits) {
-  return true;
+// Erasing variables in `FixItsForVariable`, if such a variable has an 
unfixable
+// group mate.  A variable `v` is unfixable iff `FixItsForVariable` does not

// Erases variables in `FixItsForVariable`, if such a variable has an unfixable


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156762

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


[PATCH] D156474: [-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code

2023-08-02 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud accepted this revision.
t-rasmud added a comment.
This revision is now accepted and ready to land.

LGTM!




Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2179
+  if (FixItsForVariable.count(GrpMate))
+FinalFixItsForVariable[Var].insert(FinalFixItsForVariable[Var].end(),
+   FixItsForVariable[GrpMate].begin(),

ziqingluo-90 wrote:
> t-rasmud wrote:
> > ziqingluo-90 wrote:
> > > Instead of calling `fixVariable` to generate fix-its for group mates, 
> > > directly copying those fix-its from the `FixItsForVariable` container.
> > > 
> > > At this point,  `FixItsForVariable` is complete. That is, it maps every 
> > > variable to its own fix-its (excluding fix-its of group mates).
> > Do you mean "including fix-its of group mates"? If not, I might have 
> > misunderstood the changes and will take a look again.
> No. 
> `FixItsForVariable` is a map from variables to their own fix-its, excluding 
> fix-its of group mates.
> `FinalFixItsForVariable` is a map from variables to the set of fix-its that 
> fix the whole group.
> 
> The construction of  `FinalFixItsForVariable` reads `FixItsForVariable`.
> 
> Maybe I should have better names for these two variables.
> 
> 
> 
Ah! That makes sense, I misread the `FixItsForVariable` as 
`FinalFixItsForVariable` in you comment. Thank you for explaining.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156474

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


[PATCH] D156474: [-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code

2023-08-01 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:1143
+   // order is deterministic:
+   CompareNode>
+  byVar;

NoQ wrote:
> ziqingluo-90 wrote:
> > To make the order of variables in every group deterministic.
> Would it make sense to immediately clean up the regexps in tests then?
@NoQ I had an offline discussion with @ziqingluo-90 about this and we agreed 
(IIRC) that I would clean up the tests since I added those regexps. I plan to 
create a patch as soon as this lands. Does that make sense?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156474

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


[PATCH] D156474: [-Wunsafe-buffer-usage][NFC] Slightly refactor and optimize the code

2023-08-01 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2179
+  if (FixItsForVariable.count(GrpMate))
+FinalFixItsForVariable[Var].insert(FinalFixItsForVariable[Var].end(),
+   FixItsForVariable[GrpMate].begin(),

ziqingluo-90 wrote:
> Instead of calling `fixVariable` to generate fix-its for group mates, 
> directly copying those fix-its from the `FixItsForVariable` container.
> 
> At this point,  `FixItsForVariable` is complete. That is, it maps every 
> variable to its own fix-its (excluding fix-its of group mates).
Do you mean "including fix-its of group mates"? If not, I might have 
misunderstood the changes and will take a look again.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2394
+  for (const VarDecl *V : VarGroup) {
+VarGrpMap[V] = GrpIdx;
   }

ziqingluo-90 wrote:
> Mapping variables to group indexes instead of groups themselves to avoid 
> copies.
I think this is a really clever optimization and believe it would save a bunch 
of time and space in code having significant variable grouping. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156474

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


[PATCH] D154880: [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage.

2023-07-26 Thread Rashmi Mudduluru 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 rGa6ae740e743a: [-Wunsafe-buffer-usage] Add a facility for 
debugging low fixit coverage (authored by t-rasmud).
Herald added a project: clang.

Changed prior to commit:
  https://reviews.llvm.org/D154880?vs=544547=544561#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154880

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -verify=expected %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -verify=expected,debug %s
+
+// A generic -debug would also enable our notes. This is probably fine.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -mllvm -debug \
+// RUN:-verify=expected,debug %s
+
+// This test file checks the behavior under the assumption that no fixits
+// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
+// to support these cases (thus failing the test), the test should be changed
+// to showcase a different unsupported example.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -fdiagnostics-parseable-fixits %s \
+// RUN:2>&1 | FileCheck %s
+// CHECK-NOT: fix-it:
+
+// This debugging facility is only available in debug builds.
+//
+// REQUIRES: asserts
+
+void foo() {
+  int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+  x[5] = 10;// expected-note{{used in buffer access here}}
+  int z = x[-1];// expected-note{{used in buffer access here}} \
+// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
+}
+
+void failed_decl() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
+  
+  for (int i = 0; i < 10; i++) {
+a[i] = i;  // expected-note{{used in buffer access here}}
+  }
+}
+
+void failed_multiple_decl() {
+  int *a = new int[4], b;  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
+  a[4] = 3;  // expected-note{{used in buffer access here}}
+}
+
+void failed_param_var_decl(int *a =new int[3]) {  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
+  a[4] = 6;  // expected-note{{used in buffer access here}}
+}
+
+void unclaimed_use() {
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[2] = 9;  // expected-note{{used in buffer access here}}
+  int *b = a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
+}
+
+void implied_unclaimed_var(int *b) {  // expected-warning{{'b' is an unsafe pointer used for buffer access}}
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[4] = 7;  // expected-note{{used in buffer access here}}
+  a = b;  // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
+  b++;  // expected-note{{used in pointer arithmetic here}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
+}
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2276,6 +2276,12 @@
   for (const auto  : Fixes)
 FD << F;
 }
+
+#ifndef NDEBUG
+if (areDebugNotesRequested())
+  for (const DebugNote : DebugNotesByVar[Variable])
+S.Diag(Note.first, diag::note_safe_buffer_debug_mode) << Note.second;
+#endif
   }
 
   bool isSafeBufferOptOut(const SourceLocation ) const override {
Index: 

[PATCH] D154880: [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage.

2023-07-26 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 544547.

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

https://reviews.llvm.org/D154880

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -verify=expected %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -verify=expected,debug %s
+
+// A generic -debug would also enable our notes. This is probably fine.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -mllvm -debug \
+// RUN:-verify=expected,debug %s
+
+// This test file checks the behavior under the assumption that no fixits
+// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
+// to support these cases (thus failing the test), the test should be changed
+// to showcase a different unsupported example.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -fdiagnostics-parseable-fixits %s \
+// RUN:2>&1 | FileCheck %s
+// CHECK-NOT: fix-it:
+
+// This debugging facility is only available in debug builds.
+//
+// REQUIRES: asserts
+
+void foo() {
+  int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+  x[5] = 10;// expected-note{{used in buffer access here}}
+  int z = x[-1];// expected-note{{used in buffer access here}} \
+// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
+}
+
+void failed_decl() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
+  
+  for (int i = 0; i < 10; i++) {
+a[i] = i;  // expected-note{{used in buffer access here}}
+  }
+}
+
+void failed_multiple_decl() {
+  int *a = new int[4], b;  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
+  a[4] = 3;  // expected-note{{used in buffer access here}}
+}
+
+void failed_param_var_decl(int *a =new int[3]) {  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
+  a[4] = 6;  // expected-note{{used in buffer access here}}
+}
+
+void unclaimed_use() {
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[2] = 9;  // expected-note{{used in buffer access here}}
+  int *b = a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
+}
+
+void implied_unclaimed_var(int *b) {  // expected-warning{{'b' is an unsafe pointer used for buffer access}}
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[4] = 7;  // expected-note{{used in buffer access here}}
+  a = b;  // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
+  b++;  // expected-note{{used in pointer arithmetic here}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
+}
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2276,6 +2276,12 @@
   for (const auto  : Fixes)
 FD << F;
 }
+
+#ifndef NDEBUG
+if (areDebugNotesRequested())
+  for (const DebugNote : DebugNotesByVar[Variable])
+S.Diag(Note.first, diag::note_safe_buffer_debug_mode) << Note.second;
+#endif
   }
 
   bool isSafeBufferOptOut(const SourceLocation ) const override {
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -319,6 +319,15 @@
 
   Kind getKind() const { return K; }
 
+#ifndef NDEBUG
+  StringRef getDebugName() const {
+switch (K) {
+#define GADGET(x) case Kind::x: return #x;

[PATCH] D154880: [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage.

2023-07-26 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 544521.

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

https://reviews.llvm.org/D154880

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -verify=expected %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -verify=expected,debug %s
+
+// A generic -debug would also enable our notes. This is probably fine.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -mllvm -debug \
+// RUN:-verify=expected,debug %s
+
+// This test file checks the behavior under the assumption that no fixits
+// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
+// to support these cases (thus failing the test), the test should be changed
+// to showcase a different unsupported example.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -fdiagnostics-parseable-fixits %s \
+// RUN:2>&1 | FileCheck %s
+// CHECK-NOT: fix-it:
+
+// This debugging facility is only available in debug builds.
+//
+// REQUIRES: asserts
+
+void foo() {
+  int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+  x[5] = 10;// expected-note{{used in buffer access here}}
+  int z = x[-1];// expected-note{{used in buffer access here}} \
+// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
+}
+
+void failed_decl() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
+  
+  for (int i = 0; i < 10; i++) {
+a[i] = i;  // expected-note{{used in buffer access here}}
+  }
+}
+
+void failed_multiple_decl() {
+  int *a = new int[4], b;  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
+  a[4] = 3;  // expected-note{{used in buffer access here}}
+}
+
+void failed_param_var_decl(int *a =new int[3]) {  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
+  a[4] = 6;  // expected-note{{used in buffer access here}}
+}
+
+void unclaimed_use() {
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[2] = 9;  // expected-note{{used in buffer access here}}
+  int *b = a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
+}
+
+void implied_unclaimed_var(int *b) {  // expected-warning{{'b' is an unsafe pointer used for buffer access}}
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[4] = 7;  // expected-note{{used in buffer access here}}
+  a = b;  // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
+  b++;  // expected-note{{used in pointer arithmetic here}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
+}
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2276,6 +2276,12 @@
   for (const auto  : Fixes)
 FD << F;
 }
+
+#ifndef NDEBUG
+if (areDebugNotesRequested())
+  for (const DebugNote : DebugNotesByVar[Variable])
+S.Diag(Note.first, diag::note_safe_buffer_debug_mode) << Note.second;
+#endif
   }
 
   bool isSafeBufferOptOut(const SourceLocation ) const override {
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -319,6 +319,15 @@
 
   Kind getKind() const { return K; }
 
+#ifndef NDEBUG
+  StringRef getDebugName() const {
+switch (K) {
+#define GADGET(x) case Kind::x: return #x;

[PATCH] D154880: [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage.

2023-07-26 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

In D154880#4533035 , @ziqingluo-90 
wrote:

> This is a lot of work, thank you @t-rasmud & @NoQ !
>
> I have a minor suggestion: can we use some macros to make the debug stub even 
> shorter?
> The prefix `"failed to produce fixit for declaration"`  is used in many 
> places so probably we do not have to repeat it everywhere.  And, maybe some 
> prefixes could be a bit more blurry so that they can be shared.  For example, 
> we can just replace `"failed to produce fixit for parm var decl"` with 
> `"failed to produce fixit for declaration"`.   We have source location and 
> more specific message attached to the note so we are not losing information I 
> think.
>
> I'm imagining something like this:
>
>   #define DEBUG_NOTE_DECL_FAIL(D, Msg)  \
>   Handler.addDebugNoteForVar((D), (D)->getBeginLoc(),  "failed to produce 
> fixit for declaration "##Msg)
>   
>   #define DEBUG_NOTE_GADGET_FAIL(Gadget, Msg)  ...
>
> Does it make sense to you?

I like this suggestion. I've made changes to replace 
`Handler.addDebugNoteForVar` for declarations. The Gadget case appears just 
once as of now, so I've left it as is.


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

https://reviews.llvm.org/D154880

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


[PATCH] D154880: [-Wunsafe-buffer-usage] Add a facility for debugging low fixit coverage.

2023-07-26 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 544510.
t-rasmud retitled this revision from "[-Wunsafe-buffer-usage][WIP] Add a 
facility for debugging low fixit coverage." to "[-Wunsafe-buffer-usage] Add a 
facility for debugging low fixit coverage.".

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

https://reviews.llvm.org/D154880

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -verify=expected %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -verify=expected,debug %s
+
+// A generic -debug would also enable our notes. This is probably fine.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -mllvm -debug \
+// RUN:-verify=expected,debug %s
+
+// This test file checks the behavior under the assumption that no fixits
+// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
+// to support these cases (thus failing the test), the test should be changed
+// to showcase a different unsupported example.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -fdiagnostics-parseable-fixits %s \
+// RUN:2>&1 | FileCheck %s
+// CHECK-NOT: fix-it:
+
+// This debugging facility is only available in debug builds.
+//
+// REQUIRES: asserts
+
+void foo() {
+  int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+  x[5] = 10;// expected-note{{used in buffer access here}}
+  int z = x[-1];// expected-note{{used in buffer access here}} \
+// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
+}
+
+void failed_decl() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
+  
+  for (int i = 0; i < 10; i++) {
+a[i] = i;  // expected-note{{used in buffer access here}}
+  }
+}
+
+void failed_multiple_decl() {
+  int *a = new int[4], b;  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
+  a[4] = 3;  // expected-note{{used in buffer access here}}
+}
+
+void failed_param_var_decl(int *a =new int[3]) {  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
+  a[4] = 6;  // expected-note{{used in buffer access here}}
+}
+
+void unclaimed_use() {
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[2] = 9;  // expected-note{{used in buffer access here}}
+  int *b = a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
+}
+
+void implied_unclaimed_var(int *b) {  // expected-warning{{'b' is an unsafe pointer used for buffer access}}
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[4] = 7;  // expected-note{{used in buffer access here}}
+  a = b;  // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
+  b++;  // expected-note{{used in pointer arithmetic here}} \
+// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
+}
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2276,6 +2276,12 @@
   for (const auto  : Fixes)
 FD << F;
 }
+
+#ifndef NDEBUG
+if (areDebugNotesRequested())
+  for (const DebugNote : DebugNotesByVar[Variable])
+S.Diag(Note.first, diag::note_safe_buffer_debug_mode) << Note.second;
+#endif
   }
 
   bool isSafeBufferOptOut(const SourceLocation ) const override {
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ 

[PATCH] D155526: [-Wunsafe-buffer-usage] Fix a fallthrough case in UPCStandalonePointer getFixits

2023-07-25 Thread Rashmi Mudduluru via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG070358ec9235: [-Wunsafe-buffer-usage] Fix a fallthrough case 
in UPCStandalonePointer getFixits (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Changed prior to commit:
  https://reviews.llvm.org/D155526?vs=543700=544087#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155526

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage 
-fsafe-buffer-usage-suggestions -verify %s
+
+// expected-no-diagnostics
+
+typedef unsigned __darwin_size_t;
+typedef __darwin_size_t size_t;
+ #define bzero(s, n) __builtin_bzero(s, n)
+void __nosan_bzero(void *dst, size_t sz) { bzero(dst, sz); }
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1512,8 +1512,8 @@
 FixItHint::CreateInsertion(
 (*EndOfOperand).getLocWithOffset(1), "[0]")}};
 }
+break;
   }
-[[fallthrough]];
   case Strategy::Kind::Iterator:
   case Strategy::Kind::Array:
   case Strategy::Kind::Vector:
@@ -1541,8 +1541,9 @@
   if (EndOfOperand)
 return FixItList{{FixItHint::CreateInsertion(
 *EndOfOperand, ".data()")}};
+  // FIXME: Points inside a macro expansion.
+  break;
 }
-  [[fallthrough]];
 case Strategy::Kind::Wontfix:
 case Strategy::Kind::Iterator:
 case Strategy::Kind::Array:


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-test-unreachable.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
+
+// expected-no-diagnostics
+
+typedef unsigned __darwin_size_t;
+typedef __darwin_size_t size_t;
+ #define bzero(s, n) __builtin_bzero(s, n)
+void __nosan_bzero(void *dst, size_t sz) { bzero(dst, sz); }
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -1512,8 +1512,8 @@
 FixItHint::CreateInsertion(
 (*EndOfOperand).getLocWithOffset(1), "[0]")}};
 }
+break;
   }
-[[fallthrough]];
   case Strategy::Kind::Iterator:
   case Strategy::Kind::Array:
   case Strategy::Kind::Vector:
@@ -1541,8 +1541,9 @@
   if (EndOfOperand)
 return FixItList{{FixItHint::CreateInsertion(
 *EndOfOperand, ".data()")}};
+  // FIXME: Points inside a macro expansion.
+  break;
 }
-  [[fallthrough]];
 case Strategy::Kind::Wontfix:
 case Strategy::Kind::Iterator:
 case Strategy::Kind::Array:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D156189: [-Wunsafe-buffer-usage] Refactor to let local variable fix-its and parameter fix-its share common code

2023-07-25 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:1824
+
+  if (!IdentText)
+return {};

When will this condition be satisfied? I just want to understand if there are 
code examples where there is no identifier text or is it that 
`getVarDeclIdentifierText` fails.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156189

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


[PATCH] D150386: [-Wunsafe-buffer-usage] Handle lambda expressions within a method.

2023-07-20 Thread Rashmi Mudduluru 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 rG27c10337831c: [WIP][-Wunsafe-buffer-usage] Handle lambda 
expressions within a method. (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150386

Files:
  clang/docs/LibASTMatchersReference.html
  clang/docs/ReleaseNotes.rst
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
@@ -166,7 +166,6 @@
 int * gp = garray;  // expected-warning{{'gp' is an unsafe pointer used for buffer access}}
 int gvar = gp[1];   // FIXME: file scope unsafe buffer access is not warned
 
-// FIXME: Add test for lambda capture with initializer. E. g. auto Lam = [new_p = p]() {...
 void testLambdaCaptureAndGlobal(int * p) {
   // expected-warning@-1{{'p' is an unsafe pointer used for buffer access}}
   int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}}
@@ -175,6 +174,44 @@
 return p[1]   // expected-note{{used in buffer access here}}
   + a[1] + garray[1]  // expected-note2{{used in buffer access here}}
   + gp[1];// expected-note{{used in buffer access here}}
+
+  };
+}
+
+auto file_scope_lambda = [](int *ptr) {
+  // expected-warning@-1{{'ptr' is an unsafe pointer used for buffer access}}
+  
+  ptr[5] = 10;  // expected-note{{used in buffer access here}}
+};
+
+void testLambdaCapture() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}}
+  int b[10];  // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}}
+  int c[10];
+
+  auto Lam1 = [a]() {
+return a[1];   // expected-note{{used in buffer access here}}
+  };
+
+  auto Lam2 = [x = b[3]]() { // expected-note{{used in buffer access here}}
+return x;
+  };
+
+  auto Lam = [x = c]() { // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+return x[3]; // expected-note{{used in buffer access here}}
+  };
+}
+
+void testLambdaImplicitCapture() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}}
+  int b[10];  // expected-warning{{'b' is an unsafe buffer that does not perform bounds checks}}
+  
+  auto Lam1 = [=]() {
+return a[1];   // expected-note{{used in buffer access here}}
+  };
+  
+  auto Lam2 = [&]() {
+return b[1];   // expected-note{{used in buffer access here}}
   };
 }
 
Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp
@@ -110,3 +110,70 @@
   m1(q, q, 8);
 }
 
+void unsafe_access_in_lamda() {
+  auto p = new int[10];
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:11}:"std::span p"
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
+
+  auto my_lambda = [&](){
+p[5] = 10;
+  };
+
+  foo(p);
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:8-[[@LINE-1]]:8}:".data()"
+}
+
+void fixits_in_lamda() {
+  auto p = new int[10];
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:11}:"std::span p"
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
+
+  auto my_lambda = [&](){
+foo(p);
+// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:10-[[@LINE-1]]:10}:".data()"
+  };
+
+  p[5] = 10;
+}
+
+// FIXME: Emit fixits for lambda captured variables
+void fixits_in_lambda_capture() {
+  auto p = new int[10];
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:11}:"std::span p"
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-3]]:23-[[@LINE-3]]:23}:", 10}"
+  
+  auto my_lambda = [p](){ // No fixits emitted here.
+foo(p);
+  };
+  
+  p[5] = 10;
+}
+
+void fixits_in_lambda_capture_reference() {
+  auto p = new int[10];
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:11}:"std::span p"
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-2]]:12-[[@LINE-2]]:12}:"{"
+  

[PATCH] D153059: [-Wunsafe-buffer-usage] Group parameter fix-its

2023-07-20 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

> I'm not sure it makes sense split it up now, probably depends on whether Jan 
> and Rashmi have the same troubles as me ^.^

I have to admit it is a difficult patch to follow. It also has a lot of clever 
ideas that deserve attention but might be lost because of the complexity of the 
patch. I would really appreciate if this I can be refactored and split up!


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

https://reviews.llvm.org/D153059

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


[PATCH] D153059: [-Wunsafe-buffer-usage] Group parameter fix-its

2023-07-18 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: 
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-multi-parm-span.cpp:306
+  b[5] = 5; // expected-note{{used in buffer access here}}
+}

Can we have a test case with qualifiers on parameters and maybe another with a 
qualifier on the function itself?


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

https://reviews.llvm.org/D153059

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


[PATCH] D155134: [clang][docs] Defensively turn off exception behavior in dump_ast_matchers.py

2023-07-18 Thread Rashmi Mudduluru 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 rG1e62587a48a3: [clang][docs] Defensively turn off exception 
behavior in dump_ast_matchers.py (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155134

Files:
  clang/docs/tools/dump_ast_matchers.py


Index: clang/docs/tools/dump_ast_matchers.py
===
--- clang/docs/tools/dump_ast_matchers.py
+++ clang/docs/tools/dump_ast_matchers.py
@@ -15,7 +15,8 @@
 try:
 CLASS_INDEX_PAGE = urlopen(CLASS_INDEX_PAGE_URL).read().decode("utf-8")
 except Exception as e:
-raise Exception("Unable to get %s: %s" % (CLASS_INDEX_PAGE_URL, e))
+CLASS_INDEX_PAGE = None
+print("Unable to get %s: %s" % (CLASS_INDEX_PAGE_URL, e))
 
 MATCHERS_FILE = "../../include/clang/ASTMatchers/ASTMatchers.h"
 
@@ -58,7 +59,10 @@
 url = "https://clang.llvm.org/doxygen/classclang_1_1%s.html; % name
 if url not in doxygen_probes:
 search_str = 'href="classclang_1_1%s.html"' % name
-doxygen_probes[url] = search_str in CLASS_INDEX_PAGE
+if CLASS_INDEX_PAGE is not None:
+doxygen_probes[url] = search_str in CLASS_INDEX_PAGE
+else:
+doxygen_probes[url] = True
 if not doxygen_probes[url]:
 print("Did not find %s in class index page" % name)
 if doxygen_probes[url]:
@@ -186,7 +190,7 @@
 """
 if declaration.strip():
 
-if re.match(r"^\s?(#|namespace|using)", declaration):
+if re.match(r"^\s?(#|namespace|using|template  
using|})", declaration):
 return
 
 # Node matchers are defined by writing:


Index: clang/docs/tools/dump_ast_matchers.py
===
--- clang/docs/tools/dump_ast_matchers.py
+++ clang/docs/tools/dump_ast_matchers.py
@@ -15,7 +15,8 @@
 try:
 CLASS_INDEX_PAGE = urlopen(CLASS_INDEX_PAGE_URL).read().decode("utf-8")
 except Exception as e:
-raise Exception("Unable to get %s: %s" % (CLASS_INDEX_PAGE_URL, e))
+CLASS_INDEX_PAGE = None
+print("Unable to get %s: %s" % (CLASS_INDEX_PAGE_URL, e))
 
 MATCHERS_FILE = "../../include/clang/ASTMatchers/ASTMatchers.h"
 
@@ -58,7 +59,10 @@
 url = "https://clang.llvm.org/doxygen/classclang_1_1%s.html; % name
 if url not in doxygen_probes:
 search_str = 'href="classclang_1_1%s.html"' % name
-doxygen_probes[url] = search_str in CLASS_INDEX_PAGE
+if CLASS_INDEX_PAGE is not None:
+doxygen_probes[url] = search_str in CLASS_INDEX_PAGE
+else:
+doxygen_probes[url] = True
 if not doxygen_probes[url]:
 print("Did not find %s in class index page" % name)
 if doxygen_probes[url]:
@@ -186,7 +190,7 @@
 """
 if declaration.strip():
 
-if re.match(r"^\s?(#|namespace|using)", declaration):
+if re.match(r"^\s?(#|namespace|using|template  using|})", declaration):
 return
 
 # Node matchers are defined by writing:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D155304: [clang][docs] Update LibASTMatchersReference.html

2023-07-18 Thread Rashmi Mudduluru 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 rG3ea673a97b05: [clang][docs] Update 
LibASTMatchersReference.html (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155304

Files:
  clang/docs/LibASTMatchersReference.html

Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -1455,6 +1455,16 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtcoroutineBodyStmtMatcherhttps://clang.llvm.org/doxygen/classclang_1_1CoroutineBodyStmt.html;>CoroutineBodyStmt...
+Matches coroutine body statements.
+
+coroutineBodyStmt() matches the coroutine below
+  generatorint gen() {
+co_return;
+  }
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtcoyieldExprMatcherhttps://clang.llvm.org/doxygen/classclang_1_1CoyieldExpr.html;>CoyieldExpr...
 Matches co_yield expressions.
 
@@ -3037,10 +3047,11 @@
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXConstructExpr.html;>CXXConstructExprargumentCountAtLeastunsigned N
-Checks that a call expression or a constructor call expression has
-at least the specified number of arguments (including absent default arguments).
+Checks that a call expression or a constructor call expression has at least
+the specified number of arguments (including absent default arguments).
 
-Example matches f(0, 0) and g(0, 0, 0) (matcher = callExpr(argumentCountAtLeast(2)))
+Example matches f(0, 0) and g(0, 0, 0)
+(matcher = callExpr(argumentCountAtLeast(2)))
   void f(int x, int y);
   void g(int x, int y, int z);
   f(0, 0);
@@ -3706,10 +3717,11 @@
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXUnresolvedConstructExpr.html;>CXXUnresolvedConstructExprargumentCountAtLeastunsigned N
-Checks that a call expression or a constructor call expression has
-at least the specified number of arguments (including absent default arguments).
+Checks that a call expression or a constructor call expression has at least
+the specified number of arguments (including absent default arguments).
 
-Example matches f(0, 0) and g(0, 0, 0) (matcher = callExpr(argumentCountAtLeast(2)))
+Example matches f(0, 0) and g(0, 0, 0)
+(matcher = callExpr(argumentCountAtLeast(2)))
   void f(int x, int y);
   void g(int x, int y, int z);
   f(0, 0);
@@ -3728,10 +3740,11 @@
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CallExpr.html;>CallExprargumentCountAtLeastunsigned N
-Checks that a call expression or a constructor call expression has
-at least the specified number of arguments (including absent default arguments).
+Checks that a call expression or a constructor call expression has at least
+the specified number of arguments (including absent default arguments).
 
-Example matches f(0, 0) and g(0, 0, 0) (matcher = callExpr(argumentCountAtLeast(2)))
+Example matches f(0, 0) and g(0, 0, 0)
+(matcher = callExpr(argumentCountAtLeast(2)))
   void f(int x, int y);
   void g(int x, int y, int z);
   f(0, 0);
@@ -4897,10 +4910,11 @@
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCMessageExpr.html;>ObjCMessageExprargumentCountAtLeastunsigned N
-Checks that a call expression or a constructor call expression has
-at least the specified number of arguments (including absent default arguments).
+Checks that a call expression or a constructor call expression has at least
+the specified number of arguments (including absent default arguments).
 
-Example matches f(0, 0) and g(0, 0, 0) (matcher = callExpr(argumentCountAtLeast(2)))
+Example matches f(0, 0) and g(0, 0, 0)
+(matcher = callExpr(argumentCountAtLeast(2)))
   void f(int x, int y);
   void g(int x, int y, int z);
   f(0, 0);
@@ -6795,9 +6809,10 @@
 
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CXXForRangeStmt.html;>CXXForRangeStmthasBodyMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>Stmt InnerMatcher
-Matches a 'for', 'while', 'do' statement or a function definition that has
-a given body. Note that in case of functions this matcher only matches the
-definition itself and not the other declarations of the same function.
+Matches a 'for', 'while', 'while' statement or a function or coroutine
+definition that has a given body. Note that in case of functions or
+coroutines this matcher only matches the definition itself and not the
+other declarations of the same function or coroutine.
 
 Given
   for (;;) {}
@@ -7682,6 +7697,30 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1CoroutineBodyStmt.html;>CoroutineBodyStmthasBodyMatcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>Stmt InnerMatcher
+Matches a 'for', 'while', 'while' 

[PATCH] D154880: [-Wunsafe-buffer-usage][WIP] Add a facility for debugging low fixit coverage.

2023-07-18 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 541745.
t-rasmud added a comment.

This patch addresses cases 2, 3, and 4 described in the summary (i.e) adds 
debug notes for unclaimed uses of variables and for failed fixit generation of 
variable declarations.


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

https://reviews.llvm.org/D154880

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -verify=expected %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -verify=expected,debug %s
+
+// A generic -debug would also enable our notes. This is probably fine.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-std=c++20 -mllvm -debug \
+// RUN:-verify=expected,debug %s
+
+// This test file checks the behavior under the assumption that no fixits
+// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
+// to support these cases (thus failing the test), the test should be changed
+// to showcase a different unsupported example.
+//
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
+// RUN:-mllvm -debug-only=SafeBuffers \
+// RUN:-std=c++20 -fdiagnostics-parseable-fixits %s \
+// RUN:2>&1 | FileCheck %s
+// CHECK-NOT: fix-it:
+
+// This debugging facility is only available in debug builds.
+//
+// REQUIRES: asserts
+
+void foo() {
+  int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
+  x[5] = 10;// expected-note{{used in buffer access here}}
+  int z = x[-1];// expected-note{{used in buffer access here}} \
+// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
+}
+
+void failed_decl() {
+  int a[10];  // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
+  
+  for (int i = 0; i < 10; i++) {
+a[i] = i;  // expected-note{{used in buffer access here}}
+  }
+}
+
+void failed_multiple_decl() {
+  int *a = new int[4], b;  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
+  a[4] = 3;  // expected-note{{used in buffer access here}}
+}
+
+void failed_param_var_decl(int *a =new int[3]) {  // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
+  // debug-note{{safe buffers debug: failed to produce fixit for parm var decl 'a' : has default arg}}
+  a[4] = 6;  // expected-note{{used in buffer access here}}
+}
+
+void unclaimed_use() {
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[2] = 9;  // expected-note{{used in buffer access here}}
+  int *b = a++;  // expected-note{{used in pointer arithmetic here}} \
+  // debug-note{{safe buffers debug: 'a' has an unclaimed use}}
+}
+
+void implied_unclaimed_var(int *b) {  // expected-warning{{'b' is an unsafe pointer used for buffer access}}
+  int *a = new int[3];  // expected-warning{{'a' is an unsafe pointer used for buffer access}}
+  a[4] = 7;  // expected-note{{used in buffer access here}}
+  a = b;  // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
+  b++;  // expected-note{{used in pointer arithmetic here}} \
+// debug-note{{safe buffers debug: 'b' has an unclaimed use}}
+}
Index: clang/lib/Sema/AnalysisBasedWarnings.cpp
===
--- clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2276,6 +2276,12 @@
   for (const auto  : Fixes)
 FD << F;
 }
+
+#ifndef NDEBUG
+if (areDebugNotesRequested())
+  for (const DebugNote : DebugNotesByVar[Variable])
+S.Diag(Note.first, diag::note_safe_buffer_debug_mode) << Note.second;
+#endif
   }
 
   bool isSafeBufferOptOut(const SourceLocation ) const override {
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -319,6 +319,15 @@
 
   Kind 

[PATCH] D155524: [-Wunsafe-buffer-usage] Ignore the FixableGadgets that will not be fixed at an earlier stage

2023-07-17 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud accepted this revision.
t-rasmud added a comment.
This revision is now accepted and ready to land.

LGTM!




Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2346
+  // computed above.  We do not want to generate fix-its for such variables,
+  // since they are neither warned nor reachable from a warned one.
+  for (auto I = FixablesForAllVars.byVar.begin();

Nit: Maybe also mention when a variable is neither warned nor is reachable? Are 
there scenarios besides variables inside pragmas where this constraint is 
satisfied? 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155524

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


[PATCH] D154880: [-Wunsafe-buffer-usage][WIP] Add a facility for debugging low fixit coverage.

2023-07-17 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

I will add debug notes for the rest of the cases not addressed in this patch.


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

https://reviews.llvm.org/D154880

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


[PATCH] D153059: [-Wunsafe-buffer-usage] Group parameter fix-its

2023-06-29 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: 
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-multi-parm-span.cpp:251
+}
+// CHECK-NOT: fix-it:{{.*}}:{[[@LINE-1]]

Does this patch handle virtual methods? Ideally we'd like the fixed methods to 
have the same subtyping relations as the original method. Does it make sense to 
have test cases with virtual methods as part of this patch?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153059

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


[PATCH] D153059: [-Wunsafe-buffer-usage] Group parameter fix-its

2023-06-29 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: 
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-multi-parm-span.cpp:5
+
+// FIXME: what about possible diagnostic message non-determinism?
+

I have used a workaround for non-determinism by using regular expressions to 
match on the `expected-note`.  See 
https://github.com/llvm/llvm-project/commit/db3dcedb9cedcec4a9570fda7406490c642df8ae#diff-8486f0b4ae37871fc0a186f40a41adbc70e5a0ca0134dc9bba762e1f17994460R176.
This is definitely a temporary fix (but helps with passing llvm builedbot tests 
upon landing on `llvm-project\main`) and we should probably discuss and put in 
a more reliable fix (say, using ordered sets) in the future.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153059

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


[PATCH] D150489: [-Wunsafe-buffer-usage] Handle pointer initializations for grouping related variables

2023-06-21 Thread Rashmi Mudduluru 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 rGdb3dcedb9ced: [-Wunsafe-buffer-usage] Handle pointer 
initializations for grouping related… (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Changed prior to commit:
  https://reviews.llvm.org/D150489?vs=531912=533425#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D150489

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-ptr-init-fixits.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-ptr-init.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc-fixits.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp
@@ -39,13 +39,11 @@
   p = q;
 }
 
-
-// FIXME: Support initializations at declarations.
 void lhs_span_multi_assign() {
   int *a = new int[2];
   int *b = a;
   int *c = b;
-  int *d = c;  // expected-warning{{'d' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'd' to 'std::span' to preserve bounds information$
+  int *d = c;  // expected-warning{{'d' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'd' to 'std::span' to preserve bounds information, and change ('a', 'b', and 'c'|'a', 'c', and 'b'|'b', 'a', and 'c'|'b', 'c', and 'a'|'c', 'a', and 'b'|'c', 'b', and 'a') to 'std::span' to propagate bounds information between them$
   int tmp = d[2];  // expected-note{{used in buffer access here}}
 }
 
@@ -59,15 +57,15 @@
 
 void rhs_span1() {
   int *q = new int[12];
-  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information$
+  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change ('q' and 'r'|'r' and 'q') to 'std::span' to propagate bounds information between them$
   p[5] = 10;  // expected-note{{used in buffer access here}}
-  int *r = q;  // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'r' to 'std::span' to preserve bounds information$
+  int *r = q;  // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'r' to 'std::span' to preserve bounds information, and change ('p' and 'q'|'q' and 'p') to 'std::span' to propagate bounds information between them$
   r[10] = 5;  // expected-note{{used in buffer access here}}
 }
 
 void rhs_span2() {
   int *q = new int[6];
-  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information$
+  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}}
   p[5] = 10;  // expected-note{{used in buffer access here}}
   int *r = q;
 }
@@ -175,7 +173,7 @@
 void test_crash() {
   int *r = new int[8];
   int *q = r;
-  int *p;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change 'q' to 'std::span' to propagate bounds information between them$
+  int *p;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change ('r' and 'q'|'q' and 'r') to 'std::span' to propagate bounds information between them$
   p = q;
   int tmp = p[9];  // expected-note{{used in buffer access here}}
 }
Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp
===
--- clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp
@@ -38,6 +38,15 @@
   p[5] = 4;  // expected-note{{used in buffer access here}}
 }
 
+void uuc_if_body2_ptr_init(bool flag) {
+  int *r = new int[7];
+  if (flag) {
+  } else {
+int* p = r;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} // expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change 'r' to 'std::span' to propagate bounds information between them$
+p[5] 

[PATCH] D153064: [-Wunsafe-buffer-usage] Do not emit fixits for C++ interfaces with C linkage

2023-06-16 Thread Rashmi Mudduluru 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 rGd1ae844dc2cc: [-Wunsafe-buffer-usage] Do not emit fixits for 
C++ interfaces with C linkage (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153064

Files:
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage 
-fsafe-buffer-usage-suggestions -verify %s
+
+extern "C" {
+void foo(int *ptr) {
+  ptr[5] = 10;  // expected-warning{{unsafe buffer access}}
+}
+
+void bar(int *ptr);
+
+struct c_struct {
+  char *name;
+};
+}
+
+void bar(int *ptr) {
+  ptr[5] = 10;  // expected-warning{{unsafe buffer access}}
+}
+
+void call_foo(int *p) {
+  foo(p);
+  struct c_struct str;
+  str.name[7] = 9;  // expected-warning{{unsafe buffer access}}
+  bar(p);
+}
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -2139,6 +2139,18 @@
UnsafeBufferUsageHandler ,
bool EmitSuggestions) {
   assert(D && D->getBody());
+  
+  // Do not emit fixit suggestions for functions declared in an
+  // extern "C" block.
+  if (const auto *FD = dyn_cast(D)) {
+  for (FunctionDecl *FReDecl : FD->redecls()) {
+  if (FReDecl->isExternC()) {
+EmitSuggestions = false;
+break;
+  }
+}
+  }
+  
   WarningGadgetSets UnsafeOps;
   FixableGadgetSets FixablesForAllVars;
 


Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-c-linkage.cpp
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
+
+extern "C" {
+void foo(int *ptr) {
+  ptr[5] = 10;  // expected-warning{{unsafe buffer access}}
+}
+
+void bar(int *ptr);
+
+struct c_struct {
+  char *name;
+};
+}
+
+void bar(int *ptr) {
+  ptr[5] = 10;  // expected-warning{{unsafe buffer access}}
+}
+
+void call_foo(int *p) {
+  foo(p);
+  struct c_struct str;
+  str.name[7] = 9;  // expected-warning{{unsafe buffer access}}
+  bar(p);
+}
Index: clang/lib/Analysis/UnsafeBufferUsage.cpp
===
--- clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -2139,6 +2139,18 @@
UnsafeBufferUsageHandler ,
bool EmitSuggestions) {
   assert(D && D->getBody());
+  
+  // Do not emit fixit suggestions for functions declared in an
+  // extern "C" block.
+  if (const auto *FD = dyn_cast(D)) {
+  for (FunctionDecl *FReDecl : FD->redecls()) {
+  if (FReDecl->isExternC()) {
+EmitSuggestions = false;
+break;
+  }
+}
+  }
+  
   WarningGadgetSets UnsafeOps;
   FixableGadgetSets FixablesForAllVars;
 
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D153059: [-Wunsafe-buffer-usage][WIP] Group parameter fix-its

2023-06-15 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:2264
+  // variable `x` implicates fixing `y`:
   DepMapTy PtrAssignmentGraph{};
+

ziqingluo-90 wrote:
> ziqingluo-90 wrote:
> > How about changing the variable name to `PtrImplicationGraph`?  For two 
> > variables `V` and `W`,  if `W` is in `PtrImplicationGraph[V]`, it means 
> > fixing `V` implicates fixing `W`, right?
> @t-rasmud what do you think?
Sounds good! `PtrImplicationGraph ` conveys the same meaning as 
`PtrAssignmentGraph ` and is more generic.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153059

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


[PATCH] D145739: [-Wunsafe-buffer-usage] Group variables associated by pointer assignments

2023-05-25 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

Hi Douglas,

No worries, I know the root cause for the issue. I will make the necessary 
changes and re-commit the patch.

Thanks,
Rashmi


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145739

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


[PATCH] D145739: [-Wunsafe-buffer-usage] Group variables associated by pointer assignments

2023-05-24 Thread Rashmi Mudduluru 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 rGee6b08e99375: [-Wunsafe-buffer-usage] Group variables 
associated by pointer assignments (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Changed prior to commit:
  https://reviews.llvm.org/D145739?vs=523596=525364#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D145739

Files:
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-fixits-test.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc-fixits.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-uuc.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-multi-decl-warnings.cpp
@@ -0,0 +1,347 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
+
+namespace std {
+  class type_info { };
+}
+
+void local_assign_both_span() {
+  int tmp;
+  int* p = new int[10]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change 'q' to 'std::span' to propagate bounds information between them$
+  tmp = p[4];  // expected-note{{used in buffer access here}}
+
+  int* q = new int[10];  // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'q' to 'std::span' to preserve bounds information, and change 'p' to 'std::span' to propagate bounds information between them$
+  tmp = q[4];  // expected-note{{used in buffer access here}}
+
+  q = p;
+}
+
+void local_assign_rhs_span() {
+  int tmp;
+  int* p = new int[10];
+  int* q = new int[10];  // expected-warning{{'q' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'q' to 'std::span' to preserve bounds information$
+  tmp = q[4];  // expected-note{{used in buffer access here}}
+  p = q;
+}
+
+void local_assign_no_span() {
+  int tmp;
+  int* p = new int[10];
+  int* q = new int[10];
+  p = q;
+}
+
+void local_assign_lhs_span() {
+  int tmp;
+  int* p = new int[10];  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information, and change 'q' to 'std::span' to propagate bounds information between them$
+  tmp = p[4];  // expected-note{{used in buffer access here}}
+  int* q = new int[10];
+
+  p = q;
+}
+
+
+// FIXME: Support initializations at declarations.
+void lhs_span_multi_assign() {
+  int *a = new int[2];
+  int *b = a;
+  int *c = b;
+  int *d = c;  // expected-warning{{'d' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'd' to 'std::span' to preserve bounds information$
+  int tmp = d[2];  // expected-note{{used in buffer access here}}
+}
+
+void rhs_span() {
+  int *x = new int[3];
+  int *y;  // expected-warning{{'y' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'y' to 'std::span' to preserve bounds information$
+  y[5] = 10;  // expected-note{{used in buffer access here}}
+
+  x = y;
+}
+
+void rhs_span1() {
+  int *q = new int[12];
+  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information$
+  p[5] = 10;  // expected-note{{used in buffer access here}}
+  int *r = q;  // expected-warning{{'r' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'r' to 'std::span' to preserve bounds information$
+  r[10] = 5;  // expected-note{{used in buffer access here}}
+}
+
+void rhs_span2() {
+  int *q = new int[6];
+  int *p = q;  // expected-warning{{'p' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'p' to 'std::span' to preserve bounds information$
+  p[5] = 10;  // expected-note{{used in buffer access here}}
+  int *r = q;
+}
+
+void test_grouping() {
+  int *z = new int[8];
+  int tmp;
+  int *y = new int[10];  // expected-warning{{'y' is an unsafe pointer used for buffer access}} expected-note-re^change type of 'y' to 'std::span' to preserve bounds information$
+  tmp = y[5]; // expected-note{{used in buffer access here}}
+
+  int *x = new int[10];
+  x = y;
+
+  int *w = z;
+}
+
+void test_grouping1() {
+  int 

[PATCH] D146669: [-Wunsafe-buffer-usage] Hide fixits/suggestions behind an extra flag, -fsafe-buffer-usage-suggestions.

2023-03-23 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Sema/AnalysisBasedWarnings.cpp:2159
   Sema 
+  bool SuggestSuggestions;
 

Was there a reason for naming this variable SuggestSuggestions? Can this be 
called EmitSuggestions? I think that would make it uniform and easy to follow 
the code.


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

https://reviews.llvm.org/D146669

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


[PATCH] D143128: [-Wunsafe-buffer-usage] Fix-Its transforming `[any]` to `(DRE.data() + any)`

2023-03-06 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added inline comments.



Comment at: clang/lib/Analysis/UnsafeBufferUsage.cpp:1079
+dyn_cast(ArraySub->getBase()->IgnoreImpCasts())) {
+  // FIXME: this `getASTContext` call is costy, we should pass the
+  // ASTContext in:

Nit: "costly"



Comment at: 
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-addressof-arraysubscript.cpp:74
+  // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
+}

Can we have a test case for `[0]`? 
IIUC, this would generate a fixit `p.data() + 0` which is correct but we might 
want to optimize it to `p.data()` sometime in the future.


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

https://reviews.llvm.org/D143128

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


[PATCH] D138940: [-Wunsafe-buffer-usage] Introduce the `unsafe_buffer_usage` attribute

2023-01-31 Thread Rashmi Mudduluru 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 rGacc3cc69e4d1: [-Wunsafe-buffer-usage] Introduce the 
unsafe_buffer_usage attribute (authored by t-rasmud).
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D138940

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Analysis/Analyses/UnsafeBufferUsageGadgets.def
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Analysis/UnsafeBufferUsage.cpp
  clang/lib/Sema/AnalysisBasedWarnings.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
  clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp

Index: clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/warn-unsafe-buffer-usage-function-attr.cpp
@@ -0,0 +1,86 @@
+// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -verify %s
+
+[[clang::unsafe_buffer_usage]]
+void deprecatedFunction3();
+
+void deprecatedFunction4(int z);
+
+void someFunction();
+
+[[clang::unsafe_buffer_usage]]
+void overloading(int* x);
+
+void overloading(char c[]);
+
+void overloading(int* x, int size);
+
+[[clang::unsafe_buffer_usage]]
+void deprecatedFunction4(int z);
+
+void caller(int z, int* x, int size, char c[]) {
+deprecatedFunction3(); // expected-warning{{function introduces unsafe buffer manipulation}}
+deprecatedFunction4(z); // expected-warning{{function introduces unsafe buffer manipulation}}
+someFunction();
+
+overloading(x); // expected-warning{{function introduces unsafe buffer manipulation}}
+overloading(x, size);
+overloading(c);
+}
+
+[[clang::unsafe_buffer_usage]]
+void overloading(char c[]);
+
+// Test variadics
+[[clang::unsafe_buffer_usage]]
+void testVariadics(int *ptr, ...);
+
+template
+[[clang::unsafe_buffer_usage]]
+T adder(T first, Args... args);
+
+template 
+void foo(T t) {}
+
+template<>
+[[clang::unsafe_buffer_usage]]
+void foo(int *t) {}
+
+void caller1(int *p, int *q) {
+testVariadics(p, q);  // expected-warning{{function introduces unsafe buffer manipulation}}
+adder(p, q);  // expected-warning{{function introduces unsafe buffer manipulation}}
+
+int x;
+foo(x);
+foo();  // expected-warning{{function introduces unsafe buffer manipulation}}
+}
+
+// Test virtual functions
+class BaseClass {
+public:
+[[clang::unsafe_buffer_usage]]
+virtual void func() {}
+
+virtual void func1() {}
+};
+
+class DerivedClass : public BaseClass {
+public:
+void func() {}
+
+[[clang::unsafe_buffer_usage]]
+void func1() {}
+};
+
+void testInheritance() {
+DerivedClass DC;
+DC.func();
+DC.func1();  // expected-warning{{function introduces unsafe buffer manipulation}}
+
+BaseClass *BC;
+BC->func();  // expected-warning{{function introduces unsafe buffer manipulation}}
+BC->func1();
+
+BC = 
+BC->func();  // expected-warning{{function introduces unsafe buffer manipulation}}
+BC->func1();
+}
Index: clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-unsafe-buffer-usage.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1  -fsyntax-only -verify %s
+
+// Function annotations.
+[[clang::unsafe_buffer_usage]]
+void f(int *buf, int size);
+void g(int *buffer [[clang::unsafe_buffer_usage("buffer")]], int size); // expected-warning {{'unsafe_buffer_usage' attribute only applies to functions}}
Index: clang/test/Misc/pragma-attribute-supported-attributes-list.test
===
--- clang/test/Misc/pragma-attribute-supported-attributes-list.test
+++ clang/test/Misc/pragma-attribute-supported-attributes-list.test
@@ -187,6 +187,7 @@
 // CHECK-NEXT: TestTypestate (SubjectMatchRule_function_is_member)
 // CHECK-NEXT: TrivialABI (SubjectMatchRule_record)
 // CHECK-NEXT: Uninitialized (SubjectMatchRule_variable_is_local)
+// CHECK-NEXT: UnsafeBufferUsage (SubjectMatchRule_function)
 // CHECK-NEXT: UseHandle (SubjectMatchRule_variable_is_parameter)
 // CHECK-NEXT: VecReturn (SubjectMatchRule_record)
 // CHECK-NEXT: VecTypeHint (SubjectMatchRule_function)
Index: clang/lib/Sema/SemaDeclAttr.cpp
===
--- clang/lib/Sema/SemaDeclAttr.cpp
+++ clang/lib/Sema/SemaDeclAttr.cpp
@@ -8446,6 +8446,11 @@
   D->addAttr(Attr::Create(S.Context, Argument, AL));
 }
 
+template
+static void handleUnsafeBufferUsage(Sema , Decl *D, const ParsedAttr ) {
+  

[PATCH] D126097: [clang-tidy] Adds the NSDateFormatter checker to clang-tidy

2022-08-02 Thread Rashmi Mudduluru via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG95a92995d45f: Adds the NSDateFormatter checker to clang-tidy 
(authored by t-rasmud).

Changed prior to commit:
  https://reviews.llvm.org/D126097?vs=449144=449425#toc

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126097

Files:
  clang-tools-extra/clang-tidy/objc/CMakeLists.txt
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.cpp
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.h
  clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/objc/nsdate-formatter.rst
  clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m

Index: clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
@@ -0,0 +1,313 @@
+// RUN: %check_clang_tidy %s objc-nsdate-formatter %t
+@interface NSObject
++ (instancetype)alloc;
+- (instancetype)init;
+@end
+
+@interface TestClass : NSObject
++ (void)testMethod1;
++ (void)testMethod2;
++ (void)testMethod3;
++ (void)testAnotherClass;
+@end
+
+@interface NSString : NSObject
+@end
+
+void NSLog(NSString *format, ...);
+
+@interface NSDate : NSObject
+@end
+
+@interface NSDateFormatter : NSObject
+@property(copy) NSString *dateFormat;
+- (NSString *)stringFromDate:(NSDate *)date;
+@end
+
+@interface AnotherClass : NSObject
+@property(copy) NSString *dateFormat;
+@end
+
+@interface NSDateComponents : NSObject
+@property long year;
+@property long month;
+@property long day;
+@end
+
+@interface NSCalendar : NSObject
+@property(class, readonly, copy) NSCalendar *currentCalendar;
+- (nullable NSDate *)dateFromComponents:(NSDateComponents *)Comps;
+@end
+
+@implementation TestClass
++ (void)testMethod1 {
+  // Reproducing incorrect behavior from Radar
+  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year (Y) with month (M); did you mean to use calendar year (y) instead? [objc-nsdate-formatter]
+  NSDateComponents *comps = [[NSDateComponents alloc] init];
+  [comps setDay:29];
+  [comps setMonth:12];
+  [comps setYear:2014];
+  NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior - week year
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar incorrect behavior
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM"];
+  NSLog(@"_MM %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_dd"];
+  NSLog(@"_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_DD"];
+  NSLog(@"_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_WW"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: Week of Month (W) used without the month (M); did you forget M in the format string? [objc-nsdate-formatter]
+  NSLog(@"_WW %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM_dd"];
+  NSLog(@"_MM_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Potentially Incorrect
+  [formatter setDateFormat:@"_MM_DD"];
+  NSLog(@"_MM_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_MM_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year (y) with week of the year (w); did you mean to use week-year (Y) instead? 

[PATCH] D126097: [clang-tidy] Adds the NSDateFormatter checker to clang-tidy

2022-08-01 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 449144.

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

https://reviews.llvm.org/D126097

Files:
  clang-tools-extra/clang-tidy/objc/CMakeLists.txt
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.cpp
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.h
  clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/objc/nsdate-formatter.rst
  clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m

Index: clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
@@ -0,0 +1,313 @@
+// RUN: %check_clang_tidy %s objc-nsdate-formatter %t
+@interface NSObject
++ (instancetype)alloc;
+- (instancetype)init;
+@end
+
+@interface TestClass : NSObject
++ (void)testMethod1;
++ (void)testMethod2;
++ (void)testMethod3;
++ (void)testAnotherClass;
+@end
+
+@interface NSString : NSObject
+@end
+
+void NSLog(NSString *format, ...);
+
+@interface NSDate : NSObject
+@end
+
+@interface NSDateFormatter : NSObject
+@property(copy) NSString *dateFormat;
+- (NSString *)stringFromDate:(NSDate *)date;
+@end
+
+@interface AnotherClass : NSObject
+@property(copy) NSString *dateFormat;
+@end
+
+@interface NSDateComponents : NSObject
+@property long year;
+@property long month;
+@property long day;
+@end
+
+@interface NSCalendar : NSObject
+@property(class, readonly, copy) NSCalendar *currentCalendar;
+- (nullable NSDate *)dateFromComponents:(NSDateComponents *)Comps;
+@end
+
+@implementation TestClass
++ (void)testMethod1 {
+  // Reproducing incorrect behavior from Radar
+  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-nsdate-formatter]
+  NSDateComponents *comps = [[NSDateComponents alloc] init];
+  [comps setDay:29];
+  [comps setMonth:12];
+  [comps setYear:2014];
+  NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior - week year
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar incorrect behavior
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM"];
+  NSLog(@"_MM %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_dd"];
+  NSLog(@"_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_DD"];
+  NSLog(@"_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_WW"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: Week of Month(W) used without the month(M); did you forget M in the format string? [objc-nsdate-formatter]
+  NSLog(@"_WW %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM_dd"];
+  NSLog(@"_MM_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Potentially Incorrect
+  [formatter setDateFormat:@"_MM_DD"];
+  NSLog(@"_MM_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_MM_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_MM_ww %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"===WEEK YEAR==");
+  // Incorrect
+  [formatter setDateFormat:@"_MM"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-nsdate-formatter]
+  NSLog(@"_MM 

[PATCH] D126097: [clang-tidy] Adds the NSDateFormatter checker to clang-tidy

2022-08-01 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

Ping


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

https://reviews.llvm.org/D126097

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


[PATCH] D126097: [clang-tidy] Adds the NSDateFormatter checker to clang-tidy

2022-07-14 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 444527.
t-rasmud added a reviewer: ziqingluo-90.
t-rasmud added a comment.

Addresses review comments from the previously uploaded diff.


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

https://reviews.llvm.org/D126097

Files:
  clang-tools-extra/clang-tidy/objc/CMakeLists.txt
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.cpp
  clang-tools-extra/clang-tidy/objc/NSDateFormatterCheck.h
  clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/objc/nsdate-formatter.rst
  clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m

Index: clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/objc/nsdate-formatter.m
@@ -0,0 +1,302 @@
+// RUN: %check_clang_tidy %s objc-nsdate-formatter %t
+@interface NSObject
++ (instancetype)alloc;
+- (instancetype)init;
+@end
+
+@interface TestClass : NSObject
++ (void)testMethod1;
++ (void)testMethod2;
++ (void)testMethod3;
+@end
+
+@interface NSString : NSObject
+@end
+
+void NSLog(NSString *format, ...);
+
+@interface NSDate : NSObject
+@end
+
+@interface NSDateFormatter : NSObject
+@property(copy) NSString *dateFormat;
+- (NSString *)stringFromDate:(NSDate *)date;
+@end
+
+@interface NSDateComponents : NSObject
+@property long year;
+@property long month;
+@property long day;
+@end
+
+@interface NSCalendar : NSObject
+@property(class, readonly, copy) NSCalendar *currentCalendar;
+- (nullable NSDate *)dateFromComponents:(NSDateComponents *)Comps;
+@end
+
+@implementation TestClass
++ (void)testMethod1 {
+  // Reproducing incorrect behavior from Radar
+  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-nsdate-formatter]
+  NSDateComponents *comps = [[NSDateComponents alloc] init];
+  [comps setDay:29];
+  [comps setMonth:12];
+  [comps setYear:2014];
+  NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior
+  [formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar correct behavior - week year
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  // Radar incorrect behavior
+  [formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM"];
+  NSLog(@"_MM %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_dd"];
+  NSLog(@"_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Correct
+  [formatter setDateFormat:@"_DD"];
+  NSLog(@"_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_ww %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_WW"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: Week of Month(W) used without the month(M); did you forget M in the format string? [objc-nsdate-formatter]
+  NSLog(@"_WW %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"==");
+  // Correct
+  [formatter setDateFormat:@"_MM_dd"];
+  NSLog(@"_MM_dd %@", [formatter stringFromDate:date_radar]);
+
+  // Potentially Incorrect
+  [formatter setDateFormat:@"_MM_DD"];
+  NSLog(@"_MM_DD %@", [formatter stringFromDate:date_radar]);
+
+  // Incorrect
+  [formatter setDateFormat:@"_MM_ww"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-nsdate-formatter]
+  NSLog(@"_MM_ww %@", [formatter stringFromDate:date_radar]);
+
+  NSLog(@"===WEEK YEAR==");
+  // Incorrect
+  [formatter setDateFormat:@"_MM"];
+  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-nsdate-formatter]

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-30 Thread Rashmi Mudduluru 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 rGeb1d908e5cf7: Adds AST matcher for ObjCStringLiteral 
(authored by t-rasmud).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D128103

Files:
  clang/docs/LibASTMatchersReference.html
  clang/docs/ReleaseNotes.rst
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcStringLiteral);
   REGISTER_MATCHER(objcThrowStmt);
   REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(ofClass);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher objcStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,15 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+/// Matches ObjectiveC String literal expressions.
+///
+/// Example matches @"abcd"
+/// \code
+///   NSString *s = @"abcd";
+/// \endcode
+extern const internal::VariadicDynCastAllOfMatcher
+objcStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -565,6 +565,9 @@
 
 - Added ``forEachTemplateArgument`` matcher which creates a match every
   time a ``templateArgument`` matches the matcher supplied to it.
+  
+- Added ``objcStringLiteral`` matcher which matches ObjectiveC String
+  literal expressions.
 
 clang-format
 
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -1236,7 +1236,7 @@
   #pragma omp parallel
 
 ``ompDefaultClause()`` matches ``default(none)``, ``default(shared)``,
-``default(private)`` and ``default(firstprivate)``
+`` default(private)`` and ``default(firstprivate)``
 
 
 
@@ -2036,6 +2036,14 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcStringLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCStringLiteral.html;>ObjCStringLiteral...
+Matches ObjectiveC String literal expressions.
+
+Example matches @"abcd"
+  NSString *s = @"abcd";
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcThrowStmtMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCAtThrowStmt.html;>ObjCAtThrowStmt...
 Matches 

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-30 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.



In D128103#363 , @aaron.ballman 
wrote:

> LGTM! Do you need me to commit on your behalf? If so, what name and email 
> address would you like me to use for patch attribution?

Thank you @aaron.ballman. I believe I have the permissions to commit, so I can 
land the patch.


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

https://reviews.llvm.org/D128103

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


[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-29 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 441179.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

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

https://reviews.llvm.org/D128103

Files:
  clang/docs/LibASTMatchersReference.html
  clang/docs/ReleaseNotes.rst
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp

Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcStringLiteral);
   REGISTER_MATCHER(objcThrowStmt);
   REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(ofClass);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher objcStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,15 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+/// Matches ObjectiveC String literal expressions.
+///
+/// Example matches @"abcd"
+/// \code
+///   NSString *s = @"abcd";
+/// \endcode
+extern const internal::VariadicDynCastAllOfMatcher
+objcStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang/docs/ReleaseNotes.rst
===
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -536,6 +536,9 @@
 
 - Added ``forEachTemplateArgument`` matcher which creates a match every
   time a ``templateArgument`` matches the matcher supplied to it.
+  
+- Added ``objcStringLiteral`` matcher which matches ObjectiveC String
+  literal expressions.
 
 clang-format
 
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -1236,7 +1236,7 @@
   #pragma omp parallel
 
 ``ompDefaultClause()`` matches ``default(none)``, ``default(shared)``,
-``default(private)`` and ``default(firstprivate)``
+`` default(private)`` and ``default(firstprivate)``
 
 
 
@@ -2036,6 +2036,14 @@
 
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcStringLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCStringLiteral.html;>ObjCStringLiteral...
+Matches ObjectiveC String literal expressions.
+
+Example matches @"abcd"
+  NSString *s = @"abcd";
+
+
+
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcThrowStmtMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCAtThrowStmt.html;>ObjCAtThrowStmt...
 Matches Objective-C statements.
 
@@ -4716,8 +4724,8 @@
 
 
 

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-28 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 440766.
t-rasmud added a comment.

Removes manually added documentation and adds it in the header file.


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

https://reviews.llvm.org/D128103

Files:
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcStringLiteral);
   REGISTER_MATCHER(objcThrowStmt);
   REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(ofClass);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher 
objcStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,15 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+/// Matches ObjectiveC String literal expressions.
+///
+/// Example matches @"abcd"
+/// \code
+///   NSString *s = @"abcd";
+/// \endcode
+extern const internal::VariadicDynCastAllOfMatcher
+objcStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-27 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 440450.

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

https://reviews.llvm.org/D128103

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcStringLiteral);
   REGISTER_MATCHER(objcThrowStmt);
   REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(ofClass);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher 
objcStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher
+objcStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -2035,6 +2035,13 @@
   [[NSString alloc] initWithString:@"Hello"]
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcStringLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCStringLiteral.html;>ObjCStringLiteral...
+Matches objective-C 
string literals.
+
+Example matches @"abcd"
+  NSString *s = @"abcd";
+
+
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcThrowStmtMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCAtThrowStmt.html;>ObjCAtThrowStmt...
 Matches Objective-C 
statements.
@@ -2146,7 +2153,6 @@
   wchar_t *ws = L"abcd";
 
 
-
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtsubstNonTypeTemplateParmExprMatcherhttps://clang.llvm.org/doxygen/classclang_1_1SubstNonTypeTemplateParmExpr.html;>SubstNonTypeTemplateParmExpr...
 Matches substitutions of non-type 
template parameters.
 


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-27 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 440443.
t-rasmud added a comment.

Documentation edits and other fixes, thanks to suggestions from @ziqingluo-90


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

https://reviews.llvm.org/D128103

Files:
  clang/docs/LibASTMatchersReference.html
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objcStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -505,6 +505,7 @@
   REGISTER_MATCHER(objcObjectPointerType);
   REGISTER_MATCHER(objcPropertyDecl);
   REGISTER_MATCHER(objcProtocolDecl);
+  REGISTER_MATCHER(objcStringLiteral);
   REGISTER_MATCHER(objcThrowStmt);
   REGISTER_MATCHER(objcTryStmt);
   REGISTER_MATCHER(ofClass);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher 
objcStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher
+objcStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang/docs/LibASTMatchersReference.html
===
--- clang/docs/LibASTMatchersReference.html
+++ clang/docs/LibASTMatchersReference.html
@@ -2035,6 +2035,13 @@
   [[NSString alloc] initWithString:@"Hello"]
 
 
+Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcStringLiteralMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCStringLiteral.html;>ObjCStringLiteral...
+Matches objective-C 
string literals.
+
+Example matches "abcd"
+  NSString *s = @"abcd";
+
+
 
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtobjcThrowStmtMatcherhttps://clang.llvm.org/doxygen/classclang_1_1ObjCAtThrowStmt.html;>ObjCAtThrowStmt...
 Matches Objective-C 
statements.
@@ -2146,7 +2153,6 @@
   wchar_t *ws = L"abcd";
 
 
-
 Matcherhttps://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtsubstNonTypeTemplateParmExprMatcherhttps://clang.llvm.org/doxygen/classclang_1_1SubstNonTypeTemplateParmExpr.html;>SubstNonTypeTemplateParmExpr...
 Matches substitutions of non-type 
template parameters.
 


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-27 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 440359.

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

https://reviews.llvm.org/D128103

Files:
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp
  clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objCStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -548,6 +548,7 @@
   REGISTER_MATCHER(stmt);
   REGISTER_MATCHER(stmtExpr);
   REGISTER_MATCHER(stringLiteral);
+  REGISTER_MATCHER(objCStringLiteral);
   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
   REGISTER_MATCHER(substTemplateTypeParmType);
   REGISTER_MATCHER(switchCase);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher 
objCStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher
+objCStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo


Index: clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp
@@ -2352,6 +2352,26 @@
argumentCountIs(0;
 }
 
+TEST(ASTMatchersTestObjC, ObjCStringLiteral) {
+
+  StringRef Objc1String = "@interface NSObject "
+  "@end "
+  "@interface NSString "
+  "@end "
+  "@interface Test : NSObject "
+  "+ (void)someFunction:(NSString *)Desc; "
+  "@end "
+  "@implementation Test "
+  "+ (void)someFunction:(NSString *)Desc { "
+  "return; "
+  "} "
+  "- (void) foo { "
+  "[Test someFunction:@\"Ola!\"]; "
+  "}\n"
+  "@end ";
+EXPECT_TRUE(matchesObjC(Objc1String, objCStringLiteral()));
+}
+
 TEST(ASTMatchersTestObjC, ObjCDecls) {
   StringRef ObjCString = "@protocol Proto "
  "- (void)protoDidThing; "
Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -548,6 +548,7 @@
   REGISTER_MATCHER(stmt);
   REGISTER_MATCHER(stmtExpr);
   REGISTER_MATCHER(stringLiteral);
+  REGISTER_MATCHER(objCStringLiteral);
   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
   REGISTER_MATCHER(substTemplateTypeParmType);
   REGISTER_MATCHER(switchCase);
Index: 

[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-21 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud updated this revision to Diff 438852.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D128103

Files:
  clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
  clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp

Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -548,6 +548,7 @@
   REGISTER_MATCHER(stmt);
   REGISTER_MATCHER(stmtExpr);
   REGISTER_MATCHER(stringLiteral);
+  REGISTER_MATCHER(objCStringLiteral);
   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
   REGISTER_MATCHER(substTemplateTypeParmType);
   REGISTER_MATCHER(switchCase);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher objCStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher
+objCStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
===
--- /dev/null
+++ clang-tools-extra/unittests/clang-tidy/ObjCStringLiteralTest.cpp
@@ -0,0 +1,63 @@
+//=== ObjCStringLiteralTest.cpp - clang-tidy -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "ClangTidyTest.h"
+#include "clang/AST/RecursiveASTVisitor.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace tidy {
+namespace test {
+namespace {
+
+class ObjCStringLiteralCheck : public ClangTidyCheck {
+public:
+ObjCStringLiteralCheck(StringRef CheckName, ClangTidyContext *Context)
+  : ClangTidyCheck(CheckName, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override {
+using namespace ast_matchers;
+  Finder->addMatcher(objCStringLiteral().bind("str_lit"), this);
+  }
+  void check(const ast_matchers::MatchFinder::MatchResult ) override {
+  const auto *StrExpr = Result.Nodes.getNodeAs("str_lit");
+  const StringLiteral* SL = cast(StrExpr)->getString();
+  diag(StrExpr->getExprLoc(), "Matched ObjC StringLiteral: %0") << SL->getString();
+  }
+};
+
+} // namespace
+
+TEST(ObjCStringLiteralTest, ObjCStringLiteralCheck) {
+  std::vector Errors;
+  runCheckOnCode(
+  "@interface NSObject\n"
+  "@end\n"
+  "@interface NSString\n"
+  "@end\n"
+  "@interface Test : NSObject\n"
+  "+ (void)someFunction:(NSString *)Desc;\n"
+  "@end\n"
+  "@implementation Test\n"
+  "+ (void)someFunction:(NSString *)Desc {\n"
+  "return;\n"
+  "}\n"
+  "- (void) foo {\n"
+  "[Test someFunction:@\"Ola!\"];\n"
+  "}\n"
+  "@end\n",
+  ,
+  "input.m");
+  EXPECT_EQ(1ul, Errors.size());
+  EXPECT_EQ(
+  "Matched ObjC StringLiteral: Ola!",
+  Errors[0].Message.Message);
+}
+
+} // namespace test
+} // namespace tidy
+} // namespace clang
Index: clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
===
--- clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
+++ clang-tools-extra/unittests/clang-tidy/CMakeLists.txt
@@ -28,6 +28,7 @@
   ModernizeModuleTest.cpp
   NamespaceAliaserTest.cpp
   ObjCModuleTest.cpp
+  ObjCStringLiteralTest.cpp
   OptionsProviderTest.cpp
   OverlappingReplacementsTest.cpp
   UsingInserterTest.cpp
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D128103: Adds AST Matcher for ObjCStringLiteral

2022-06-17 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud created this revision.
t-rasmud added reviewers: NoQ, usama54321.
Herald added subscribers: carlosgalvezp, mgorny.
Herald added a project: All.
t-rasmud requested review of this revision.
Herald added projects: clang, clang-tools-extra.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128103

Files:
  clang-tools-extra/clang-tidy/misc/CMakeLists.txt
  clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.cpp
  clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.h
  clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst
  clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m
  clang/include/clang/ASTMatchers/ASTMatchers.h
  clang/lib/ASTMatchers/ASTMatchersInternal.cpp
  clang/lib/ASTMatchers/Dynamic/Registry.cpp

Index: clang/lib/ASTMatchers/Dynamic/Registry.cpp
===
--- clang/lib/ASTMatchers/Dynamic/Registry.cpp
+++ clang/lib/ASTMatchers/Dynamic/Registry.cpp
@@ -548,6 +548,7 @@
   REGISTER_MATCHER(stmt);
   REGISTER_MATCHER(stmtExpr);
   REGISTER_MATCHER(stringLiteral);
+  REGISTER_MATCHER(objCStringLiteral);
   REGISTER_MATCHER(substNonTypeTemplateParmExpr);
   REGISTER_MATCHER(substTemplateTypeParmType);
   REGISTER_MATCHER(switchCase);
Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp
===
--- clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -917,6 +917,7 @@
 const internal::VariadicDynCastAllOfMatcher
 cxxBoolLiteral;
 const internal::VariadicDynCastAllOfMatcher stringLiteral;
+const internal::VariadicDynCastAllOfMatcher objCStringLiteral;
 const internal::VariadicDynCastAllOfMatcher
 characterLiteral;
 const internal::VariadicDynCastAllOfMatcher
Index: clang/include/clang/ASTMatchers/ASTMatchers.h
===
--- clang/include/clang/ASTMatchers/ASTMatchers.h
+++ clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -1515,6 +1515,9 @@
 extern const internal::VariadicDynCastAllOfMatcher
 objcMessageExpr;
 
+extern const internal::VariadicDynCastAllOfMatcher
+objCStringLiteral;
+
 /// Matches Objective-C interface declarations.
 ///
 /// Example matches Foo
Index: clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/misc-match-objc-string-literal.m
@@ -0,0 +1,19 @@
+// RUN: %check_clang_tidy %s misc-match-objc-string-literal %t
+@interface NSObject
+@end
+
+@interface NSString
+@end
+
+@interface Test : NSObject
++ (void)someFunction:(NSString *)Desc;
+@end
+
+@implementation Test
++ (void)someFunction:(NSString *)Desc {
+return;
+}
+- (void) foo {
+[Test someFunction:@"Ola!"];
+}
+@end
Index: clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst
===
--- /dev/null
+++ clang-tools-extra/docs/clang-tidy/checks/misc/match-objc-string-literal.rst
@@ -0,0 +1,6 @@
+.. title:: clang-tidy - misc-match-objc-string-literal
+
+misc-match-objc-string-literal
+==
+
+FIXME: Matches all ObjCStringLiteral types.
Index: clang-tools-extra/docs/ReleaseNotes.rst
===
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -123,6 +123,11 @@
Warns when the code is unwrapping a `std::optional`, `absl::optional`,
or `base::Optional` object without assuring that it contains a value.
 
+- New :doc:`misc-match-objc-string-literal
+  ` check.
+
+  Matches ObjCStringLiteral type.
+
 - New :doc:`modernize-macro-to-enum
   ` check.
 
Index: clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
===
--- clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
+++ clang-tools-extra/clang-tidy/misc/MiscTidyModule.cpp
@@ -10,6 +10,7 @@
 #include "../ClangTidyModule.h"
 #include "../ClangTidyModuleRegistry.h"
 #include "DefinitionsInHeadersCheck.h"
+#include "MatchObjcStringLiteralCheck.h"
 #include "MisleadingBidirectional.h"
 #include "MisleadingIdentifier.h"
 #include "MisplacedConstCheck.h"
@@ -35,6 +36,8 @@
   void addCheckFactories(ClangTidyCheckFactories ) override {
 CheckFactories.registerCheck(
 "misc-definitions-in-headers");
+CheckFactories.registerCheck(
+"misc-match-objc-string-literal");
 CheckFactories.registerCheck(
 "misc-misleading-bidirectional");
 CheckFactories.registerCheck(
Index: clang-tools-extra/clang-tidy/misc/MatchObjcStringLiteralCheck.h

[PATCH] D126097: Adds the NSDateFormatter checker to clang-tidy

2022-05-20 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud created this revision.
t-rasmud added a reviewer: NoQ.
Herald added subscribers: carlosgalvezp, mgorny.
Herald added a project: All.
t-rasmud requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Reports incorrect string patterns in the date format specifier.


https://reviews.llvm.org/D126097

Files:
  clang-tools-extra/clang-tidy/add_new_check.py
  clang-tools-extra/clang-tidy/objc/CMakeLists.txt
  clang-tools-extra/clang-tidy/objc/NsdateformatterCheck.cpp
  clang-tools-extra/clang-tidy/objc/NsdateformatterCheck.h
  clang-tools-extra/clang-tidy/objc/ObjCTidyModule.cpp
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/list.rst
  clang-tools-extra/docs/clang-tidy/checks/objc-NSDateFormatter.rst
  clang-tools-extra/test/clang-tidy/checkers/objc-NSDateFormatter.m

Index: clang-tools-extra/test/clang-tidy/checkers/objc-NSDateFormatter.m
===
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/objc-NSDateFormatter.m
@@ -0,0 +1,280 @@
+// RUN: %check_clang_tidy %s objc-NSDateFormatter %t
+
+#import 
+
+@interface TestClass : NSObject{
+}
++(void)testMethod1;
++(void)testMethod2;
++(void)testMethod3;
+@end
+
+@implementation TestClass
+
++(void)testMethod1{
+// Reproducing incorrect behavior from Radar
+NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
+[formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+// CHECK-MESSAGES: :[[@LINE-1]]:17:31: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-NSDateFormatter]
+NSDateComponents *comps = [[NSDateComponents alloc] init];
+[comps setDay:29];
+[comps setMonth:12];
+[comps setYear:2014];
+NSDate *date_radar = [[NSCalendar currentCalendar] dateFromComponents:comps];
+NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@",[formatter stringFromDate:date_radar]);
+
+// Radar correct behavior
+[formatter setDateFormat:@"_MM_dd_HH_mm_ss_SSS_ZZZ"];
+NSLog(@"_MM_dd_HH_mm_ss_SSS_ZZZ %@",[formatter stringFromDate:date_radar]);
+
+// Radar correct behavior - week year
+[formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@",[formatter stringFromDate:date_radar]);
+
+// Radar incorrect behavior
+[formatter setDateFormat:@"_ww_dd_HH_mm_ss_SSS_ZZZ"];
+// CHECK-MESSAGES: :[[@LINE-1]]:35:31: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-NSDateFormatter]
+NSLog(@"_ww_dd_HH_mm_ss_SSS_ZZZ %@",[formatter stringFromDate:date_radar]);
+
+NSLog(@"==");
+// Correct
+[formatter setDateFormat:@"_MM"];
+NSLog(@"_MM %@",[formatter stringFromDate:date_radar]);
+
+// Correct
+[formatter setDateFormat:@"_dd"];
+NSLog(@"_dd %@",[formatter stringFromDate:date_radar]);
+
+// Correct
+[formatter setDateFormat:@"_DD"];
+NSLog(@"_DD %@",[formatter stringFromDate:date_radar]);
+
+// Incorrect
+[formatter setDateFormat:@"_ww"];
+// CHECK-MESSAGES: :[[@LINE-1]]:53:31: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-NSDateFormatter]
+NSLog(@"_ww %@",[formatter stringFromDate:date_radar]);
+
+// Incorrect
+[formatter setDateFormat:@"_WW"];
+// CHECK-MESSAGES: :[[@LINE-1]]:58:31: warning: Week of Month(W) used without the month(M); did you forget M in the format string? [objc-NSDateFormatter]
+NSLog(@"_WW %@",[formatter stringFromDate:date_radar]);
+
+NSLog(@"==");
+// Correct
+[formatter setDateFormat:@"_MM_dd"];
+NSLog(@"_MM_dd %@",[formatter stringFromDate:date_radar]);
+
+// Potentially Incorrect
+[formatter setDateFormat:@"_MM_DD"];
+NSLog(@"_MM_DD %@",[formatter stringFromDate:date_radar]);
+
+// Incorrect
+[formatter setDateFormat:@"_MM_ww"];
+// CHECK-MESSAGES: :[[@LINE-1]]:72:31: warning: use of calendar year(y) with week of the year(w); did you mean to use week-year(Y) instead? [objc-NSDateFormatter]
+NSLog(@"_MM_ww %@",[formatter stringFromDate:date_radar]);
+
+NSLog(@"===WEEK YEAR==");
+// Incorrect
+[formatter setDateFormat:@"_MM"];
+// CHECK-MESSAGES: :[[@LINE-1]]:78:31: warning: use of week year(Y) with month(M); did you mean to use calendar year(y) instead? [objc-NSDateFormatter]
+NSLog(@"_MM %@",[formatter stringFromDate:date_radar]);
+
+// Correct
+[formatter setDateFormat:@"_ww"];
+NSLog(@"_ww %@",[formatter stringFromDate:date_radar]);
+
+// Incorrect
+[formatter setDateFormat:@"_WW"];
+// CHECK-MESSAGES: :[[@LINE-1]]:87:31: 

[PATCH] D124570: Revert "[analyzer][NFC] Refactor GenericTaintChecker to use CallDescriptionMap"

2022-04-28 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud abandoned this revision.
t-rasmud added a comment.

Abandoning this patch because it was a downstream problem and I incorrectly 
diagnosed it on the one source branch.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D124570

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


[PATCH] D124570: Revert "[analyzer][NFC] Refactor GenericTaintChecker to use CallDescriptionMap"

2022-04-28 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud added a comment.

In D124570#3479334 , @steakhal wrote:

> I'm not comfortable with reverting this.
> We depend on taint capabilities downstream, thus we would rather help fixing 
> the underlying issue.
> Do you have a reproducer for the crash in any form? I would take it from 
> there.

@steakhal Thanks for the feedback. I am attaching the pre-processed file and 
the analyzer invocation command.F22926909: clang_crash_hd2dRD.i 


F22926912: command-for-crash 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D124570

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


[PATCH] D124570: Revert "[analyzer][NFC] Refactor GenericTaintChecker to use CallDescriptionMap"

2022-04-27 Thread Rashmi Mudduluru via Phabricator via cfe-commits
t-rasmud created this revision.
t-rasmud added a reviewer: NoQ.
Herald added subscribers: manas, steakhal, ASDenysPetrov, martong, dkrupp, 
donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, 
xazax.hun.
Herald added a project: All.
t-rasmud requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

This reverts commit 6a7d75cf1183802f38d3fea417be0a4edaf03711 
 in order 
to get rid of the crash rdar://90041422


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124570

Files:
  clang/include/clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h
  clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp

Index: clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
===
--- clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
+++ clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
@@ -22,14 +22,15 @@
 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
 #include "clang/StaticAnalyzer/Core/Checker.h"
 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
-#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
 #include "llvm/Support/YAMLTraits.h"
 
+#include 
 #include 
 #include 
+#include 
 #include 
 
 using namespace clang;
@@ -37,651 +38,577 @@
 using namespace taint;
 
 namespace {
-
-class GenericTaintChecker;
-
-/// Check for CWE-134: Uncontrolled Format String.
-constexpr llvm::StringLiteral MsgUncontrolledFormatString =
-"Untrusted data is used as a format string "
-"(CWE-134: Uncontrolled Format String)";
-
-/// Check for:
-/// CERT/STR02-C. "Sanitize data passed to complex subsystems"
-/// CWE-78, "Failure to Sanitize Data into an OS Command"
-constexpr llvm::StringLiteral MsgSanitizeSystemArgs =
-"Untrusted data is passed to a system call "
-"(CERT/STR02-C. Sanitize data passed to complex subsystems)";
-
-/// Check if tainted data is used as a buffer size in strn.. functions,
-/// and allocators.
-constexpr llvm::StringLiteral MsgTaintedBufferSize =
-"Untrusted data is used to specify the buffer size "
-"(CERT/STR31-C. Guarantee that storage for strings has sufficient space "
-"for character data and the null terminator)";
-
-/// Check if tainted data is used as a custom sink's parameter.
-constexpr llvm::StringLiteral MsgCustomSink =
-"Untrusted data is passed to a user-defined sink";
-
-using ArgIdxTy = int;
-using ArgVecTy = llvm::SmallVector;
-
-/// Denotes the return value.
-constexpr ArgIdxTy ReturnValueIndex{-1};
-
-static ArgIdxTy fromArgumentCount(unsigned Count) {
-  assert(Count <=
- static_cast(std::numeric_limits::max()) &&
- "ArgIdxTy is not large enough to represent the number of arguments.");
-  return Count;
-}
-
-/// Check if the region the expression evaluates to is the standard input,
-/// and thus, is tainted.
-/// FIXME: Move this to Taint.cpp.
-bool isStdin(SVal Val, const ASTContext ) {
-  // FIXME: What if Val is NonParamVarRegion?
-
-  // The region should be symbolic, we do not know it's value.
-  const auto *SymReg = dyn_cast_or_null(Val.getAsRegion());
-  if (!SymReg)
-return false;
-
-  // Get it's symbol and find the declaration region it's pointing to.
-  const auto *Sm = dyn_cast(SymReg->getSymbol());
-  if (!Sm)
-return false;
-  const auto *DeclReg = dyn_cast(Sm->getRegion());
-  if (!DeclReg)
-return false;
-
-  // This region corresponds to a declaration, find out if it's a global/extern
-  // variable named stdin with the proper type.
-  if (const auto *D = dyn_cast_or_null(DeclReg->getDecl())) {
-D = D->getCanonicalDecl();
-// FIXME: This should look for an exact match.
-if (D->getName().contains("stdin") && D->isExternC()) {
-  const QualType FILETy = ACtx.getFILEType().getCanonicalType();
-  const QualType Ty = D->getType().getCanonicalType();
-
-  if (Ty->isPointerType())
-return Ty->getPointeeType() == FILETy;
-}
+class GenericTaintChecker : public Checker {
+public:
+  static void *getTag() {
+static int Tag;
+return 
   }
-  return false;
-}
-
-SVal getPointeeOf(const CheckerContext , Loc LValue) {
-  const QualType ArgTy = LValue.getType(C.getASTContext());
-  if (!ArgTy->isPointerType() || !ArgTy->getPointeeType()->isVoidType())
-return C.getState()->getSVal(LValue);
-
-  // Do not dereference void pointers. Treat them as byte pointers instead.
-  // FIXME: we might want to consider more than just the first byte.
-  return C.getState()->getSVal(LValue, C.getASTContext().CharTy);
-}
-
-/// Given a pointer/reference argument, return the value it refers to.
-Optional