//===----------------------------------------------------------------------===//
#include "RedundantStringCStrCheck.h"
-#include "../utils/FixItHintUtils.h"
#include "../utils/Matchers.h"
#include "../utils/OptionsUtils.h"
#include "clang/Lex/Lexer.h"
namespace {
+// Return true if expr needs to be put in parens when it is an argument of a
+// prefix unary operator, e.g. when it is a binary or ternary operator
+// syntactically.
+bool needParensAfterUnaryOperator(const Expr &ExprNode) {
+ if (isa<clang::BinaryOperator>(&ExprNode) ||
+ isa<clang::ConditionalOperator>(&ExprNode)) {
+ return true;
+ }
+ if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
+ return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
+ Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
+ Op->getOperator() != OO_Subscript;
+ }
+ return false;
+}
+
+// Format a pointer to an expression: prefix with '*' but simplify
+// when it already begins with '&'. Return empty string on failure.
+std::string
+formatDereference(const ast_matchers::MatchFinder::MatchResult &Result,
+ const Expr &ExprNode) {
+ if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
+ if (Op->getOpcode() == UO_AddrOf) {
+ // Strip leading '&'.
+ return std::string(tooling::fixit::getText(
+ *Op->getSubExpr()->IgnoreParens(), *Result.Context));
+ }
+ }
+ StringRef Text = tooling::fixit::getText(ExprNode, *Result.Context);
+
+ if (Text.empty())
+ return std::string();
+
+ // Remove remaining '->' from overloaded operator call
+ Text.consume_back("->");
+
+ // Add leading '*'.
+ if (needParensAfterUnaryOperator(ExprNode)) {
+ return (llvm::Twine("*(") + Text + ")").str();
+ }
+ return (llvm::Twine("*") + Text).str();
+}
+
AST_MATCHER(MaterializeTemporaryExpr, isBoundToLValue) {
return Node.isBoundToLvalueReference();
}
// Replace the "call" node with the "arg" node, prefixed with '*'
// if the call was using '->' rather than '.'.
std::string ArgText =
- Arrow ? utils::fixit::formatDereference(*Arg, *Result.Context)
+ Arrow ? formatDereference(Result, *Arg)
: tooling::fixit::getText(*Arg, *Result.Context).str();
if (ArgText.empty())
return;
#include "FixItHintUtils.h"
#include "LexerUtils.h"
#include "clang/AST/ASTContext.h"
-#include "clang/AST/ExprCXX.h"
#include "clang/AST/Type.h"
-#include "clang/Tooling/FixIt.h"
#include <optional>
namespace clang::tidy::utils::fixit {
return std::nullopt;
}
-
-// Return true if expr needs to be put in parens when it is an argument of a
-// prefix unary operator, e.g. when it is a binary or ternary operator
-// syntactically.
-static bool needParensAfterUnaryOperator(const Expr &ExprNode) {
- if (isa<clang::BinaryOperator>(&ExprNode) ||
- isa<clang::ConditionalOperator>(&ExprNode)) {
- return true;
- }
- if (const auto *Op = dyn_cast<CXXOperatorCallExpr>(&ExprNode)) {
- return Op->getNumArgs() == 2 && Op->getOperator() != OO_PlusPlus &&
- Op->getOperator() != OO_MinusMinus && Op->getOperator() != OO_Call &&
- Op->getOperator() != OO_Subscript;
- }
- return false;
-}
-
-// Format a pointer to an expression: prefix with '*' but simplify
-// when it already begins with '&'. Return empty string on failure.
-std::string formatDereference(const Expr &ExprNode, const ASTContext &Context) {
- if (const auto *Op = dyn_cast<clang::UnaryOperator>(&ExprNode)) {
- if (Op->getOpcode() == UO_AddrOf) {
- // Strip leading '&'.
- return std::string(
- tooling::fixit::getText(*Op->getSubExpr()->IgnoreParens(), Context));
- }
- }
- StringRef Text = tooling::fixit::getText(ExprNode, Context);
-
- if (Text.empty())
- return std::string();
-
- // Remove remaining '->' from overloaded operator call
- Text.consume_back("->");
-
- // Add leading '*'.
- if (needParensAfterUnaryOperator(ExprNode)) {
- return (llvm::Twine("*(") + Text + ")").str();
- }
- return (llvm::Twine("*") + Text).str();
-}
-
} // namespace clang::tidy::utils::fixit