clang-tools 23.0.0git
AvoidEndlCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "AvoidEndlCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/Expr.h"
13#include "clang/AST/ExprCXX.h"
14#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/ASTMatchers/ASTMatchers.h"
16#include "clang/Lex/Lexer.h"
17
18using namespace clang::ast_matchers;
19
21
22void AvoidEndlCheck::registerMatchers(MatchFinder *Finder) {
23 Finder->addMatcher(
24 callExpr(
25 anyOf(cxxOperatorCallExpr(
26 hasOverloadedOperatorName("<<"),
27 hasRHS(declRefExpr(to(namedDecl(hasName("::std::endl"))))
28 .bind("expr"))),
29 callExpr(argumentCountIs(1),
30 callee(functionDecl(hasName("::std::endl"))))
31 .bind("expr"))),
32 this);
33}
34
35void AvoidEndlCheck::check(const MatchFinder::MatchResult &Result) {
36 const auto *Expression = Result.Nodes.getNodeAs<Expr>("expr");
37 assert(Expression);
38 assert(isa<DeclRefExpr>(Expression) || isa<CallExpr>(Expression));
39
40 // FIXME: It would be great if we could transform
41 // 'std::cout << "Hi" << std::endl;' into
42 // 'std::cout << "Hi\n"';
43
44 if (isa<DeclRefExpr>(Expression)) {
45 // Handle the more common streaming '... << std::endl' case
46 const CharSourceRange TokenRange =
47 CharSourceRange::getTokenRange(Expression->getSourceRange());
48 StringRef SourceText = Lexer::getSourceText(
49 TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
50 if (SourceText.empty())
51 SourceText = "std::endl";
52 auto Diag = diag(Expression->getBeginLoc(),
53 "do not use '%0' with streams; use '\\n' instead")
54 << SourceText;
55 if (TokenRange.isValid())
56 Diag << FixItHint::CreateReplacement(TokenRange, "'\\n'");
57 } else {
58 // Handle the less common function call 'std::endl(...)' case
59 const auto *CallExpression = cast<CallExpr>(Expression);
60 assert(CallExpression->getNumArgs() == 1);
61
62 StringRef SourceText = Lexer::getSourceText(
63 CharSourceRange::getTokenRange(
64 CallExpression->getCallee()->getSourceRange()),
65 *Result.SourceManager, Result.Context->getLangOpts());
66 if (SourceText.empty())
67 SourceText = "std::endl";
68 auto Diag = diag(CallExpression->getBeginLoc(),
69 "do not use '%0' with streams; use '\\n' instead")
70 << SourceText;
71
72 const CharSourceRange ArgTokenRange = CharSourceRange::getTokenRange(
73 CallExpression->getArg(0)->getSourceRange());
74 const StringRef ArgSourceText = Lexer::getSourceText(
75 ArgTokenRange, *Result.SourceManager, Result.Context->getLangOpts());
76 const CharSourceRange ReplacementRange =
77 CharSourceRange::getTokenRange(CallExpression->getSourceRange());
78 if (!ArgSourceText.empty() && ReplacementRange.isValid()) {
79 const std::string ReplacementString =
80 std::string(ArgSourceText) + " << '\\n'";
81 Diag << FixItHint::CreateReplacement(ReplacementRange, ReplacementString);
82 }
83 }
84}
85
86} // namespace clang::tidy::performance
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override