[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim added a comment. Hey, I'm back. I actually kind of forgot about this. Unfortunately I cannot to continue the patch soon. How do I grant you full rights on this patch? If just saying here is enough, there you go, anyone can take it and change it. If there is more bureaucracy to it, just let me know. I'll be happy to see any of the code making into the tool. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D31130/new/ https://reviews.llvm.org/D31130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim added a comment. Oh, and if I remember correctly, the reason for this patch was to avoid exactly the github issue mentioned here. I'm embarrassed I didn't explain that anywhere :( CHANGES SINCE LAST ACTION https://reviews.llvm.org/D31130/new/ https://reviews.llvm.org/D31130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D145047: Fix broken link on Clang documentation page
brenoguim accepted this revision. brenoguim added a comment. This revision is now accepted and ready to land. LGTM Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D145047/new/ https://reviews.llvm.org/D145047 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim added a reviewer: cfe-commits. brenoguim added a comment. Forgot to add cfe-commits... https://reviews.llvm.org/D31130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim updated this revision to Diff 92419. brenoguim added a comment. - Using the ImplicitCastExpr type on the AST_MATCHER directly to avoid explicit cast. - Using isa<> instead of dyn_cast<> to just check for type - Move variable declaration into "if" condition https://reviews.llvm.org/D31130 Files: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -1,4 +1,5 @@ // RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t +#include #include namespace gsl { @@ -34,6 +35,11 @@ for (auto &e : a) // OK, iteration internally decays array to pointer e = 1; + + assert(false); // OK, array decay inside system header macro + + assert(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] } const char *g() { Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp === --- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -47,6 +47,25 @@ return InnerMatcher.matches(*E, Finder, Builder); } +AST_MATCHER(ImplicitCastExpr, isArrayToPointerDecay) { + return Node.getCastKind() == CK_ArrayToPointerDecay; +} + +AST_MATCHER(ImplicitCastExpr, sysSymbolDecayInSysHeader) { + auto &SM = Finder->getASTContext().getSourceManager(); + if (SM.isInSystemMacro(Node.getLocStart())) { +if (isa(Node.getSubExpr())) + return true; + +if (const auto *SymbolDeclRef = dyn_cast(Node.getSubExpr())) { + const ValueDecl *SymbolDecl = SymbolDeclRef->getDecl(); + if (SymbolDecl && SM.isInSystemHeader(SymbolDecl->getLocation())) +return true; +} + } + return false; +} + void ProBoundsArrayToPointerDecayCheck::registerMatchers(MatchFinder *Finder) { if (!getLangOpts().CPlusPlus) return; @@ -56,19 +75,19 @@ // 2) inside a range-for over an array // 3) if it converts a string literal to a pointer Finder->addMatcher( - implicitCastExpr(unless(hasParent(arraySubscriptExpr())), + implicitCastExpr(isArrayToPointerDecay(), + unless(hasParent(arraySubscriptExpr())), unless(hasParentIgnoringImpCasts(explicitCastExpr())), unless(isInsideOfRangeBeginEndStmt()), - unless(hasSourceExpression(stringLiteral( + unless(hasSourceExpression(stringLiteral())), + unless(sysSymbolDecayInSysHeader())) .bind("cast"), this); } void ProBoundsArrayToPointerDecayCheck::check( const MatchFinder::MatchResult &Result) { const auto *MatchedCast = Result.Nodes.getNodeAs("cast"); - if (MatchedCast->getCastKind() != CK_ArrayToPointerDecay) -return; diag(MatchedCast->getExprLoc(), "do not implicitly decay an array into a " "pointer; consider using gsl::array_view or " Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -1,4 +1,5 @@ // RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t +#include #include namespace gsl { @@ -34,6 +35,11 @@ for (auto &e : a) // OK, iteration internally decays array to pointer e = 1; + + assert(false); // OK, array decay inside system header macro + + assert(a); + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] } const char *g() { Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp === --- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp @@ -47,6 +47,25 @@ return InnerMatcher.matches(*E, Finder, Builder); } +AST_MATCHER(ImplicitCastExpr, isArrayToPointerDecay) { + return Node.getCastKind() == CK_ArrayToPointerDecay; +} + +AST_MATCHER(ImplicitCastExpr, sysSymbolDecayInSysHeader) { + auto &SM = Finder->getASTContext().getSourceManager(); + if (SM.isInSystemMacro(Node.ge
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim marked 4 inline comments as done. brenoguim added a comment. Hi @aaron.ballman, Thanks for the review! I don't have rights to push this. Who should I contact to push the change in my behalf? https://reviews.llvm.org/D31130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim added a comment. I appreciate it! https://reviews.llvm.org/D31130 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim updated this revision to Diff 93062. brenoguim added a comment. - Removed the "#include " which caused problems in environments without that header - Included a directory with -isystem to simulate system headers - Added a "macro.h" header with definitions of types, functions and macros to simulate the assert.h header and others. - Added checks for behavior in some situations Some of the checks follow the comment "Not Ok. Should it be ok?" to point the situations in which the check could be considered too strict for reporting such as violation. I would like to get feedback specially on them. https://reviews.llvm.org/D31130 Files: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp test/clang-tidy/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay/macro.h test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -1,5 +1,7 @@ -// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t +// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t -- -- -isystem%S/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay + #include +#include namespace gsl { template @@ -45,3 +47,49 @@ void *a[2]; f2(static_cast(a)); // OK, explicit cast } + +void user_fn_decay_str(const char *); +void user_fn_decay_int_array(const int *); +void bug32239() { + sys_macro_with_pretty_function_string_decay; // Ok + sys_macro_with_sys_string_decay; // Ok + sys_macro_with_sys_int_array_decay; // Ok + + sys_fn_decay_str(__PRETTY_FUNCTION__); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + sys_fn_decay_str(SYS_STRING); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + sys_fn_decay_int_array(SYS_INT_ARRAY); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_code_in_sys_macro(sys_fn_decay_str(__PRETTY_FUNCTION__)); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_code_in_sys_macro(sys_fn_decay_str(SYS_STRING)); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_code_in_sys_macro(sys_fn_decay_int_array(SYS_INT_ARRAY)); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:49: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + const char UserString[1] = ""; + decay_to_char_pointer_in_macro(UserString); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + decay_to_char_pointer_in_macro(__PRETTY_FUNCTION__); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + int a[5]; + decay_to_int_pointer_in_macro(a); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_fn_decay_str(__PRETTY_FUNCTION__); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_fn_decay_str(SYS_STRING); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] +
[PATCH] D31130: B32239 clang-tidy should not warn about array to pointer decay on system macros
brenoguim updated this revision to Diff 97385. brenoguim marked an inline comment as done. brenoguim added a comment. Never flag predefinedExpr() types. https://reviews.llvm.org/D31130 Files: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp test/clang-tidy/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay/macro.h test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp Index: test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp === --- test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp +++ test/clang-tidy/cppcoreguidelines-pro-bounds-array-to-pointer-decay.cpp @@ -1,5 +1,7 @@ -// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t +// RUN: %check_clang_tidy %s cppcoreguidelines-pro-bounds-array-to-pointer-decay %t -- -- -isystem%S/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay + #include +#include namespace gsl { template @@ -45,3 +47,45 @@ void *a[2]; f2(static_cast(a)); // OK, explicit cast } + +void user_fn_decay_str(const char *); +void user_fn_decay_int_array(const int *); +void bug32239() { + sys_macro_with_pretty_function_string_decay; // Ok + sys_macro_with_sys_string_decay; // Ok + sys_macro_with_sys_int_array_decay; // Ok + + sys_fn_decay_str(__PRETTY_FUNCTION__); // Ok + + sys_fn_decay_str(SYS_STRING); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + sys_fn_decay_int_array(SYS_INT_ARRAY); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_code_in_sys_macro(sys_fn_decay_str(__PRETTY_FUNCTION__)); // Ok + + user_code_in_sys_macro(sys_fn_decay_str(SYS_STRING)); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:43: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_code_in_sys_macro(sys_fn_decay_int_array(SYS_INT_ARRAY)); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:49: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + const char UserString[1] = ""; + decay_to_char_pointer_in_macro(UserString); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + decay_to_char_pointer_in_macro(__PRETTY_FUNCTION__); // Ok + + int a[5]; + decay_to_int_pointer_in_macro(a); // Not Ok + // CHECK-MESSAGES: :[[@LINE-1]]:33: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_fn_decay_str(__PRETTY_FUNCTION__); // Ok + + user_fn_decay_str(SYS_STRING); // Not Ok - should it be ok? + // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] + + user_fn_decay_int_array(SYS_INT_ARRAY); // Not ok + // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay] +} Index: test/clang-tidy/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay/macro.h === --- /dev/null +++ test/clang-tidy/Inputs/cppcoreguidelines-pro-bounds-array-to-pointer-decay/macro.h @@ -0,0 +1,14 @@ +const char SYS_STRING[2]= "s"; +const int SYS_INT_ARRAY[2] = {0, 0}; + +void sys_fn_decay_str(const char *); +void sys_fn_decay_int_array(const int *); + +#define sys_macro_with_pretty_function_string_decay sys_fn_decay_str(__PRETTY_FUNCTION__) +#define sys_macro_with_sys_string_decay sys_fn_decay_str(SYS_STRING) +#define sys_macro_with_sys_int_array_decay sys_fn_decay_int_array(SYS_INT_ARRAY) + +#define user_code_in_sys_macro(expr) expr; + +#define decay_to_char_pointer_in_macro(expr) sys_fn_decay_str(expr) +#define decay_to_int_pointer_in_macro(expr) sys_fn_decay_int_array(expr) Index: clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp === --- clang-tidy/cppcoreguidelines/ProBoundsArrayToPointerDecayCheck.cpp +++ cla