Re: [PATCH] D16278: ASTMatcher for ParenExpr node.
adek05 updated this revision to Diff 45344. adek05 added a comment. @aaron.ballman comments. If it looks good, please commit it for me :) http://reviews.llvm.org/D16278 Files: docs/LibASTMatchersReference.html include/clang/ASTMatchers/ASTMatchers.h lib/ASTMatchers/Dynamic/Registry.cpp unittests/ASTMatchers/ASTMatchersTest.cpp unittests/ASTMatchers/Dynamic/RegistryTest.cpp Index: unittests/ASTMatchers/Dynamic/RegistryTest.cpp === --- unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -506,6 +506,12 @@ EXPECT_FALSE(matches("struct X {};", Value)); } +TEST_F(RegistryTest, ParenExpr) { + Matcher Value = constructMatcher("parenExpr").getTypedMatcher(); + EXPECT_TRUE(matches("int i = (1);", Value)); + EXPECT_FALSE(matches("int i = 1;", Value)); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers Index: unittests/ASTMatchers/ASTMatchersTest.cpp === --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3563,6 +3563,14 @@ varDecl(isExceptionVariable(; } +TEST(ParenExpression, SimpleCases) { + EXPECT_TRUE(matches("int i = (3);", parenExpr())); + EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr())); + EXPECT_TRUE(notMatches("int i = 3;", parenExpr())); + EXPECT_TRUE(notMatches("int foo() { return 1; }; int a = foo();", + parenExpr())); +} + TEST(HasConditionVariableStatement, DoesNotMatchCondition) { EXPECT_TRUE(notMatches( "void x() { if(true) {} }", Index: lib/ASTMatchers/Dynamic/Registry.cpp === --- lib/ASTMatchers/Dynamic/Registry.cpp +++ lib/ASTMatchers/Dynamic/Registry.cpp @@ -326,6 +326,7 @@ REGISTER_MATCHER(on); REGISTER_MATCHER(onImplicitObjectArgument); REGISTER_MATCHER(parameterCountIs); + REGISTER_MATCHER(parenExpr); REGISTER_MATCHER(parenType); REGISTER_MATCHER(parmVarDecl); REGISTER_MATCHER(pointee); Index: include/clang/ASTMatchers/ASTMatchers.h === --- include/clang/ASTMatchers/ASTMatchers.h +++ include/clang/ASTMatchers/ASTMatchers.h @@ -1048,6 +1048,17 @@ Decl, UnresolvedUsingTypenameDecl> unresolvedUsingTypenameDecl; +/// \brief Matches parentheses used in expressions. +/// +/// Example matches (foo() + 1) +/// \code +/// int foo() { return 1; } +/// int a = (foo() + 1); +/// \endcode +const internal::VariadicDynCastAllOfMatcher< + Stmt, + ParenExpr> parenExpr; + /// \brief Matches constructor call expressions (including implicit ones). /// /// Example matches string(ptr, n) and ptr within arguments of f Index: docs/LibASTMatchersReference.html === --- docs/LibASTMatchersReference.html +++ docs/LibASTMatchersReference.html @@ -1061,6 +1061,14 @@ [[NSString alloc] initWithString:@"Hello"] +Matcherhttp://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtparenExprMatcherhttp://clang.llvm.org/doxygen/classclang_1_1ParenExpr.html;>ParenExpr... +Matches parentheses used in expressions. + +Given + int foo() { return 1; } + int a = (foo() + 1); +matches '(foo() + 1)' + Matcherhttp://clang.llvm.org/doxygen/classclang_1_1Stmt.html;>StmtreturnStmtMatcherhttp://clang.llvm.org/doxygen/classclang_1_1ReturnStmt.html;>ReturnStmt... Matches return statements. Index: unittests/ASTMatchers/Dynamic/RegistryTest.cpp === --- unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -506,6 +506,12 @@ EXPECT_FALSE(matches("struct X {};", Value)); } +TEST_F(RegistryTest, ParenExpr) { + Matcher Value = constructMatcher("parenExpr").getTypedMatcher(); + EXPECT_TRUE(matches("int i = (1);", Value)); + EXPECT_FALSE(matches("int i = 1;", Value)); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers Index: unittests/ASTMatchers/ASTMatchersTest.cpp === --- unittests/ASTMatchers/ASTMatchersTest.cpp +++ unittests/ASTMatchers/ASTMatchersTest.cpp @@ -3563,6 +3563,14 @@ varDecl(isExceptionVariable(; } +TEST(ParenExpression, SimpleCases) { + EXPECT_TRUE(matches("int i = (3);", parenExpr())); + EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr())); + EXPECT_TRUE(notMatches("int i = 3;", parenExpr())); + EXPECT_TRUE(notMatches("int foo() { return 1; }; int a = foo();", + parenExpr())); +} + TEST(HasConditionVariableStatement, DoesNotMatchCondition) { EXPECT_TRUE(notMatches( "void x() { if(true) {} }", Index: lib/ASTMatchers/Dynamic/Registry.cpp
Re: [PATCH] D16286: [clang-tidy] Readability check for redundant parenthesis in return expression.
adek05 updated this revision to Diff 45228. adek05 added a comment. @LegalizeAdulthood, @Eugene.Zelenko and @omtcyf0 comments http://reviews.llvm.org/D16286 Files: clang-tidy/readability/CMakeLists.txt clang-tidy/readability/ReadabilityTidyModule.cpp clang-tidy/readability/ReturnWithRedundantParensCheck.cpp clang-tidy/readability/ReturnWithRedundantParensCheck.h test/clang-tidy/readability-return-with-redundant-parens.cpp Index: test/clang-tidy/readability-return-with-redundant-parens.cpp === --- /dev/null +++ test/clang-tidy/readability-return-with-redundant-parens.cpp @@ -0,0 +1,24 @@ +// RUN: %check_clang_tidy %s readability-return-with-redundant-parens %t + +int no_parens() { + return 1; +} + +int simple1() { + return (1); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return 1;{{$}} +} + +int complex1() { + int a = 0; + return (a + a * (a + a)); + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: redundant parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return a + a * (a + a);{{$}} +} + +int no_space() { + return(1); + // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: redundant parentheses for expression in return statement [readability-return-with-redundant-parens] + // CHECK-FIXES: {{^ }}return 1;{{$}} +} Index: clang-tidy/readability/ReturnWithRedundantParensCheck.h === --- /dev/null +++ clang-tidy/readability/ReturnWithRedundantParensCheck.h @@ -0,0 +1,38 @@ +//===--- ReturnWithRedundantParensCheck.h - clang-tidy --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H + +#include "../ClangTidy.h" + +namespace clang { +namespace tidy { +namespace readability { + +/// Find and remove redundant parenthesis surrounding returned expression. +/// +/// Examples: +/// \code +/// void f() { return (1); } ==> void f() { return 1; } +/// \endcode +class ReturnWithRedundantParensCheck : public ClangTidyCheck { +public: + ReturnWithRedundantParensCheck(StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) {} + + void registerMatchers(ast_matchers::MatchFinder *Finder) override; + void check(const ast_matchers::MatchFinder::MatchResult ) override; +}; + +} // namespace readability +} // namespace tidy +} // namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_RETURNBRACKETSCHECK_H Index: clang-tidy/readability/ReturnWithRedundantParensCheck.cpp === --- /dev/null +++ clang-tidy/readability/ReturnWithRedundantParensCheck.cpp @@ -0,0 +1,40 @@ +//===--- ReturnWithRedundantParensCheck.cpp - clang-tidy *- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#include "ReturnWithRedundantParensCheck.h" + +#include + +using namespace clang::ast_matchers; + +namespace clang { +namespace tidy { +namespace readability { + +void ReturnWithRedundantParensCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher(returnStmt(hasDescendant(parenExpr().bind("paren_expr"))), + this); +} + +void ReturnWithRedundantParensCheck::check( +const ast_matchers::MatchFinder::MatchResult ) { + + const ParenExpr *ParenExpression = + Result.Nodes.getNodeAs("paren_expr"); + + const SourceLocation = ParenExpression->getLParen(); + const SourceLocation = ParenExpression->getRParen(); + diag(LParenLoc, "redundant parentheses for expression in return statement") + << FixItHint::CreateRemoval(LParenLoc) + << FixItHint::CreateRemoval(RParenLoc); +} + +} // namespace readability +} // namespace tidy +} // namespace clang Index: clang-tidy/readability/ReadabilityTidyModule.cpp === --- clang-tidy/readability/ReadabilityTidyModule.cpp +++ clang-tidy/readability/ReadabilityTidyModule.cpp @@ -20,6 +20,7 @@ #include "NamedParameterCheck.h" #include "RedundantSmartptrGetCheck.h" #include "RedundantStringCStrCheck.h" +#include "ReturnWithRedundantParensCheck.h" #include "SimplifyBooleanExprCheck.h" #include "UniqueptrDeleteReleaseCheck.h" @@ -54,6 +55,8 @@
Re: [PATCH] D15443: Fix getLocEnd for function declarations with exception specification.
adek05 added a comment. Would you mind committing it for me, and I don't have a commit access yet. Also, could you take a look at http://reviews.llvm.org/D15444 with tests for clang-tidy related to this change? http://reviews.llvm.org/D15443 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15443: Fix getLocEnd for function declarations with exception specification.
adek05 updated this revision to Diff 44598. adek05 added a comment. Adding testcases in unittest/AST/SourceLocationTest.cpp as suggested by @aaronballman Interestingly, without my change tests for function declarations pass. Only member functions fail: tools/clang/unittests/AST/ASTTests ... [--] 2 tests from FunctionDecl [ RUN ] FunctionDecl.FunctionDeclWithThrowSpecification [ OK ] FunctionDecl.FunctionDeclWithThrowSpecification (17 ms) [ RUN ] FunctionDecl.FunctionDeclWithNoExceptSpecification [ OK ] FunctionDecl.FunctionDeclWithNoExceptSpecification (10 ms) [--] 2 tests from FunctionDecl (27 ms total) [--] 2 tests from CXXMethodDecl [ RUN ] CXXMethodDecl.CXXMethodDeclWithThrowSpecification /Users/adek/llvm-git/tools/clang/unittests/AST/SourceLocationTest.cpp:569: Failure Value of: Verifier.match( "class A {\n" "void f() throw();\n" "};\n", functionDecl()) Actual: false (Expected range <2:1-2:16>, found ) Expected: true [ FAILED ] CXXMethodDecl.CXXMethodDeclWithThrowSpecification (10 ms) [ RUN ] CXXMethodDecl.CXXMethodDeclWithNoExceptSpecification /Users/adek/llvm-git/tools/clang/unittests/AST/SourceLocationTest.cpp:580: Failure Value of: Verifier.match( "class A {\n" "void f() noexcept(false);\n" "};\n", functionDecl(), Language::Lang_CXX11) Actual: false (Expected range <2:1-2:24>, found ) Expected: true [ FAILED ] CXXMethodDecl.CXXMethodDeclWithNoExceptSpecification (10 ms) [--] 2 tests from CXXMethodDecl (20 ms total) Not sure why would they take different codepaths, throw and noexcept are C++(11) specific. Is the code parsed as C++11 anyway for Verifiers? http://reviews.llvm.org/D15443 Files: lib/Parse/ParseDeclCXX.cpp unittests/AST/SourceLocationTest.cpp Index: unittests/AST/SourceLocationTest.cpp === --- unittests/AST/SourceLocationTest.cpp +++ unittests/AST/SourceLocationTest.cpp @@ -542,5 +542,43 @@ cxxConstructExpr(), Lang_OBJCXX)); } +TEST(FunctionDecl, FunctionDeclWithThrowSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(1, 1, 1, 16); + EXPECT_TRUE(Verifier.match( + "void f() throw();\n", + functionDecl())); +} + +TEST(FunctionDecl, FunctionDeclWithNoExceptSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(1, 1, 1, 24); + EXPECT_TRUE(Verifier.match( + "void f() noexcept(false);\n", + functionDecl(), + Language::Lang_CXX11)); +} + +TEST(CXXMethodDecl, CXXMethodDeclWithThrowSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(2, 1, 2, 16); + EXPECT_TRUE(Verifier.match( + "class A {\n" + "void f() throw();\n" + "};\n", + functionDecl())); +} + +TEST(CXXMethodDecl, CXXMethodDeclWithNoExceptSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(2, 1, 2, 24); + EXPECT_TRUE(Verifier.match( + "class A {\n" + "void f() noexcept(false);\n" + "};\n", + functionDecl(), + Language::Lang_CXX11)); +} + } // end namespace ast_matchers } // end namespace clang Index: lib/Parse/ParseDeclCXX.cpp === --- lib/Parse/ParseDeclCXX.cpp +++ lib/Parse/ParseDeclCXX.cpp @@ -3358,7 +3358,8 @@ ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens, /*StopAtSemi=*/true, /*ConsumeFinalToken=*/true); -SpecificationRange.setEnd(Tok.getLocation()); +SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation()); + return EST_Unparsed; } Index: unittests/AST/SourceLocationTest.cpp === --- unittests/AST/SourceLocationTest.cpp +++ unittests/AST/SourceLocationTest.cpp @@ -542,5 +542,43 @@ cxxConstructExpr(), Lang_OBJCXX)); } +TEST(FunctionDecl, FunctionDeclWithThrowSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(1, 1, 1, 16); + EXPECT_TRUE(Verifier.match( + "void f() throw();\n", + functionDecl())); +} + +TEST(FunctionDecl, FunctionDeclWithNoExceptSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(1, 1, 1, 24); + EXPECT_TRUE(Verifier.match( + "void f() noexcept(false);\n", + functionDecl(), + Language::Lang_CXX11)); +} + +TEST(CXXMethodDecl, CXXMethodDeclWithThrowSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(2, 1, 2, 16); + EXPECT_TRUE(Verifier.match( + "class A {\n" + "void f() throw();\n" + "};\n", + functionDecl())); +} + +TEST(CXXMethodDecl, CXXMethodDeclWithNoExceptSpecification) { + RangeVerifier Verifier; + Verifier.expectRange(2, 1, 2, 24); + EXPECT_TRUE(Verifier.match( + "class A {\n" + "void f() noexcept(false);\n" + "};\n", + functionDecl(), + Language::Lang_CXX11)); +} + } // end namespace ast_matchers } // end
Re: [PATCH] D15443: Fix getLocEnd for function declarations with exception specification.
adek05 added a comment. I think this won't work. If there is a problem with the expression inside `throw` or `noexcept` specifier, it will be highlighted inside the parens, never trying to actually access the end location of the function declaration. It would be enough if there is a warning/error which would highlight the whole function declaration, because we could check whether it points at `;` or the character in front of it, but I don't know about such warning and I don't know how to smartly look for it. http://reviews.llvm.org/D15443 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15443: Fix getLocEnd for function declarations with exception specification.
adek05 added reviewers: aaron.ballman, jroelofs. adek05 added a comment. Ping http://reviews.llvm.org/D15443 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15486: [RFC] Emit note pointing to a discarded qualifier [-Wincompatible-pointer-types-discards-qualifiers]
adek05 added a comment. @aaron.ballman I think this could be hard to achieve without an extra note if you have something like: cat test2.c int main() { char *c = 'a'; char volatile** cc = cc = } test2.c:2:15: warning: incompatible integer to pointer conversion initializing 'char *' with an expression of type 'int' [-Wint-conversion] char *c = 'a'; ^ ~~~ test2.c:4:25: warning: initializing 'volatile char **' with an expression of type 'char **' discards qualifiers in nested pointer types [-Wincompatible-pointer-types-discards-qualifiers] char volatile** cc = ^~~ test2.c:4:9: note: nested pointer type with discarded 'const' qualifier char volatile** cc = ^~ test2.c:5:12: warning: assigning to 'volatile char **' from 'char **' discards qualifiers in nested pointer types [-Wincompatible-pointer-types-discards-qualifiers] cc = ^ ~~ 3 warnings generated. Sadly, my code doesn't print a note in the second case, which I would have to debug. In case of initialization, I think we can just print one line and omit note. For assignment which is not an initialization, it might be useful to point to the declaration of a variable which is assigned. I will try to handle the second case and write tests for this. Let me know if you feel like it is still worth proceeding. http://reviews.llvm.org/D15486 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15486: [RFC] Emit note pointing to a discarded qualifier [-Wincompatible-pointer-types-discards-qualifiers]
adek05 added inline comments. Comment at: lib/Sema/SemaPseudoObject.cpp:739-751 @@ -738,15 +738,15 @@ // C++ class type. if (!S.getLangOpts().CPlusPlus || !op->getType()->isRecordType()) { QualType paramType = (*Setter->param_begin())->getType() .substObjCMemberType( receiverType, Setter->getDeclContext(), ObjCSubstitutionContext::Parameter); if (!S.getLangOpts().CPlusPlus || !paramType->isRecordType()) { ExprResult opResult = op; Sema::AssignConvertType assignResult = S.CheckSingleAssignmentConstraints(paramType, opResult); - if (S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType, + if (S.DiagnoseAssignmentResult(assignResult, opcLoc, paramType, nullptr, op->getType(), opResult.get(), Sema::AA_Assigning)) return ExprError(); This is the place which I suspect may trigger this warning, but I need to read the code more careful to come up with an example. http://reviews.llvm.org/D15486 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15444: [clang-modernize] AddOverride: tests for handling throw() and noexcept() specifiers
adek05 updated this revision to Diff 42568. adek05 added a comment. Moved tests to clang-tidy test suite following Eugene.Zelenko suggestion. http://reviews.llvm.org/D15444 Files: test/clang-tidy/modernize-use-override.cpp Index: test/clang-tidy/modernize-use-override.cpp === --- test/clang-tidy/modernize-use-override.cpp +++ test/clang-tidy/modernize-use-override.cpp @@ -37,6 +37,9 @@ virtual void cv() const volatile; virtual void cv2() const volatile; + + virtual void ne() noexcept(false); + virtual void t() throw(); }; struct SimpleCases : public Base { @@ -104,6 +107,14 @@ virtual void o() __attribute__((unused)); // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using // CHECK-FIXES: {{^}} void o() override __attribute__((unused)); + + virtual void ne() noexcept(false); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void ne() noexcept(false) override; + + virtual void t() throw(); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void t() throw() override; }; // CHECK-MESSAGES-NOT: warning: Index: test/clang-tidy/modernize-use-override.cpp === --- test/clang-tidy/modernize-use-override.cpp +++ test/clang-tidy/modernize-use-override.cpp @@ -37,6 +37,9 @@ virtual void cv() const volatile; virtual void cv2() const volatile; + + virtual void ne() noexcept(false); + virtual void t() throw(); }; struct SimpleCases : public Base { @@ -104,6 +107,14 @@ virtual void o() __attribute__((unused)); // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using // CHECK-FIXES: {{^}} void o() override __attribute__((unused)); + + virtual void ne() noexcept(false); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void ne() noexcept(false) override; + + virtual void t() throw(); + // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using + // CHECK-FIXES: {{^}} void t() throw() override; }; // CHECK-MESSAGES-NOT: warning: ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15444: [clang-modernize] AddOverride: tests for handling throw() and noexcept() specifiers
adek05 added a comment. I need a brave soul to review http://reviews.llvm.org/D15443 too, so if you know who else could take a look at it feel free to add people there. http://reviews.llvm.org/D15444 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
Re: [PATCH] D15444: [clang-modernize] AddOverride: tests for handling throw() and noexcept() specifiers
adek05 added a comment. I don't see any tests for AddOverride in tests/clang-tidy yet. Is clang-tidy linking everything that clang-modernizer has? If so, I can create another patch which just moves these tests over. http://reviews.llvm.org/D15444 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits