clang-tools  8.0.0
UseNodiscardCheck.cpp
Go to the documentation of this file.
1 //===--- UseNodiscardCheck.cpp - clang-tidy -------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UseNodiscardCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/Decl.h"
13 #include "clang/AST/Type.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace modernize {
21 
22 static bool doesNoDiscardMacroExist(ASTContext &Context,
23  const llvm::StringRef &MacroId) {
24  // Don't check for the Macro existence if we are using an attribute
25  // either a C++17 standard attribute or pre C++17 syntax
26  if (MacroId.startswith("[[") || MacroId.startswith("__attribute__"))
27  return true;
28 
29  // Otherwise look up the macro name in the context to see if its defined.
30  return Context.Idents.get(MacroId).hasMacroDefinition();
31 }
32 
33 namespace {
34 AST_MATCHER(CXXMethodDecl, isOverloadedOperator) {
35  // Don't put ``[[nodiscard]]`` in front of operators.
36  return Node.isOverloadedOperator();
37 }
38 AST_MATCHER(CXXMethodDecl, isConversionOperator) {
39  // Don't put ``[[nodiscard]]`` in front of a conversion decl
40  // like operator bool().
41  return isa<CXXConversionDecl>(Node);
42 }
43 AST_MATCHER(CXXMethodDecl, hasClassMutableFields) {
44  // Don't put ``[[nodiscard]]`` on functions on classes with
45  // mutable member variables.
46  return Node.getParent()->hasMutableFields();
47 }
48 AST_MATCHER(ParmVarDecl, hasParameterPack) {
49  // Don't put ``[[nodiscard]]`` on functions with parameter pack arguments.
50  return Node.isParameterPack();
51 }
52 AST_MATCHER(CXXMethodDecl, hasTemplateReturnType) {
53  // Don't put ``[[nodiscard]]`` in front of functions returning a template
54  // type.
55  return Node.getReturnType()->isTemplateTypeParmType() ||
56  Node.getReturnType()->isInstantiationDependentType();
57 }
58 AST_MATCHER(CXXMethodDecl, isDefinitionOrInline) {
59  // A function definition, with optional inline but not the declaration.
60  return !(Node.isThisDeclarationADefinition() && Node.isOutOfLine());
61 }
62 AST_MATCHER(QualType, isInstantiationDependentType) {
63  return Node->isInstantiationDependentType();
64 }
65 AST_MATCHER(QualType, isNonConstReferenceOrPointer) {
66  // If the function has any non-const-reference arguments
67  // bool foo(A &a)
68  // or pointer arguments
69  // bool foo(A*)
70  // then they may not care about the return value because of passing data
71  // via the arguments.
72  return (Node->isTemplateTypeParmType() || Node->isPointerType() ||
73  (Node->isReferenceType() &&
74  !Node.getNonReferenceType().isConstQualified()) ||
75  Node->isInstantiationDependentType());
76 }
77 } // namespace
78 
79 UseNodiscardCheck::UseNodiscardCheck(StringRef Name, ClangTidyContext *Context)
80  : ClangTidyCheck(Name, Context),
81  NoDiscardMacro(Options.get("ReplacementString", "[[nodiscard]]")) {}
82 
84  Options.store(Opts, "ReplacementString", NoDiscardMacro);
85 }
86 
87 void UseNodiscardCheck::registerMatchers(MatchFinder *Finder) {
88  // If we use ``[[nodiscard]]`` attribute, we require at least C++17. Use a
89  // macro or ``__attribute__`` with pre c++17 compilers by using
90  // ReplacementString option.
91  if ((NoDiscardMacro == "[[nodiscard]]" && !getLangOpts().CPlusPlus17) ||
92  !getLangOpts().CPlusPlus)
93  return;
94 
95  auto FunctionObj =
96  cxxRecordDecl(hasAnyName("::std::function", "::boost::function"));
97 
98  // Find all non-void const methods which have not already been marked to
99  // warn on unused result.
100  Finder->addMatcher(
101  cxxMethodDecl(
102  allOf(isConst(), isDefinitionOrInline(),
103  unless(anyOf(
104  returns(voidType()), isNoReturn(), isOverloadedOperator(),
105  isVariadic(), hasTemplateReturnType(),
106  hasClassMutableFields(), isConversionOperator(),
107  hasAttr(clang::attr::WarnUnusedResult),
108  hasType(isInstantiationDependentType()),
109  hasAnyParameter(anyOf(
110  parmVarDecl(anyOf(hasType(FunctionObj),
111  hasType(references(FunctionObj)))),
112  hasType(isNonConstReferenceOrPointer()),
113  hasParameterPack()))))))
114  .bind("no_discard"),
115  this);
116 }
117 
118 void UseNodiscardCheck::check(const MatchFinder::MatchResult &Result) {
119  const auto *MatchedDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("no_discard");
120  // Don't make replacements if the location is invalid or in a macro.
121  SourceLocation Loc = MatchedDecl->getLocation();
122  if (Loc.isInvalid() || Loc.isMacroID())
123  return;
124 
125  SourceLocation RetLoc = MatchedDecl->getInnerLocStart();
126 
127  ASTContext &Context = *Result.Context;
128 
129  auto Diag = diag(RetLoc, "function %0 should be marked " + NoDiscardMacro)
130  << MatchedDecl;
131 
132  // Check for the existence of the keyword being used as the ``[[nodiscard]]``.
133  if (!doesNoDiscardMacroExist(Context, NoDiscardMacro))
134  return;
135 
136  // Possible false positives include:
137  // 1. A const member function which returns a variable which is ignored
138  // but performs some external I/O operation and the return value could be
139  // ignored.
140  Diag << FixItHint::CreateInsertion(RetLoc, NoDiscardMacro + " ");
141 }
142 
143 } // namespace modernize
144 } // namespace tidy
145 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
AST_MATCHER(BinaryOperator, isAssignmentOperator)
Definition: Matchers.h:20
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:473
static bool doesNoDiscardMacroExist(ASTContext &Context, const llvm::StringRef &MacroId)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438