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"
25 unless(isExpansionInSystemHeader()),
26 anyOf(cxxOperatorCallExpr(
27 hasOverloadedOperatorName(
"<<"),
28 hasRHS(declRefExpr(to(namedDecl(hasName(
"::std::endl"))))
30 callExpr(argumentCountIs(1),
31 callee(functionDecl(hasName(
"::std::endl"))))
37 const auto *Expression = Result.Nodes.getNodeAs<Expr>(
"expr");
39 assert(isa<DeclRefExpr>(Expression) || isa<CallExpr>(Expression));
45 if (llvm::isa<DeclRefExpr>(Expression)) {
47 const CharSourceRange TokenRange =
48 CharSourceRange::getTokenRange(Expression->getSourceRange());
49 StringRef SourceText = Lexer::getSourceText(
50 TokenRange, *Result.SourceManager, Result.Context->getLangOpts());
51 if (SourceText.empty())
52 SourceText =
"std::endl";
53 auto Diag =
diag(Expression->getBeginLoc(),
54 "do not use '%0' with streams; use '\\n' instead")
56 if (TokenRange.isValid())
57 Diag << FixItHint::CreateReplacement(TokenRange,
"'\\n'");
60 const auto *CallExpression = llvm::cast<CallExpr>(Expression);
61 assert(CallExpression->getNumArgs() == 1);
63 StringRef SourceText = Lexer::getSourceText(
64 CharSourceRange::getTokenRange(
65 CallExpression->getCallee()->getSourceRange()),
66 *Result.SourceManager, Result.Context->getLangOpts());
67 if (SourceText.empty())
68 SourceText =
"std::endl";
69 auto Diag =
diag(CallExpression->getBeginLoc(),
70 "do not use '%0' with streams; use '\\n' instead")
73 const CharSourceRange ArgTokenRange = CharSourceRange::getTokenRange(
74 CallExpression->getArg(0)->getSourceRange());
75 const StringRef ArgSourceText = Lexer::getSourceText(
76 ArgTokenRange, *Result.SourceManager, Result.Context->getLangOpts());
77 const CharSourceRange ReplacementRange =
78 CharSourceRange::getTokenRange(CallExpression->getSourceRange());
79 if (!ArgSourceText.empty() && ReplacementRange.isValid()) {
80 const std::string ReplacementString =
81 std::string(ArgSourceText) +
" << '\\n'";
82 Diag << FixItHint::CreateReplacement(ReplacementRange, ReplacementString);
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.