clang-tools  8.0.0
UseNullptrCheck.cpp
Go to the documentation of this file.
1 //===--- UseNullptrCheck.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 "UseNullptrCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/RecursiveASTVisitor.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/Lex/Lexer.h"
15 
16 using namespace clang;
17 using namespace clang::ast_matchers;
18 using namespace llvm;
19 
20 namespace clang {
21 namespace tidy {
22 namespace modernize {
23 namespace {
24 
25 const char CastSequence[] = "sequence";
26 
27 AST_MATCHER(Type, sugaredNullptrType) {
28  const Type *DesugaredType = Node.getUnqualifiedDesugaredType();
29  if (const auto *BT = dyn_cast<BuiltinType>(DesugaredType))
30  return BT->getKind() == BuiltinType::NullPtr;
31  return false;
32 }
33 
34 /// \brief Create a matcher that finds implicit casts as well as the head of a
35 /// sequence of zero or more nested explicit casts that have an implicit cast
36 /// to null within.
37 /// Finding sequences of explict casts is necessary so that an entire sequence
38 /// can be replaced instead of just the inner-most implicit cast.
39 StatementMatcher makeCastSequenceMatcher() {
40  StatementMatcher ImplicitCastToNull = implicitCastExpr(
41  anyOf(hasCastKind(CK_NullToPointer), hasCastKind(CK_NullToMemberPointer)),
42  unless(hasImplicitDestinationType(qualType(substTemplateTypeParmType()))),
43  unless(hasSourceExpression(hasType(sugaredNullptrType()))));
44 
45  return castExpr(anyOf(ImplicitCastToNull,
46  explicitCastExpr(hasDescendant(ImplicitCastToNull))),
47  unless(hasAncestor(explicitCastExpr())))
48  .bind(CastSequence);
49 }
50 
51 bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,
52  const SourceManager &SM) {
53  return SM.isWrittenInSameFile(StartLoc, EndLoc);
54 }
55 
56 /// \brief Replaces the provided range with the text "nullptr", but only if
57 /// the start and end location are both in main file.
58 /// Returns true if and only if a replacement was made.
59 void replaceWithNullptr(ClangTidyCheck &Check, SourceManager &SM,
60  SourceLocation StartLoc, SourceLocation EndLoc) {
61  CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);
62  // Add a space if nullptr follows an alphanumeric character. This happens
63  // whenever there is an c-style explicit cast to nullptr not surrounded by
64  // parentheses and right beside a return statement.
65  SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);
66  bool NeedsSpace = isAlphanumeric(*SM.getCharacterData(PreviousLocation));
67  Check.diag(Range.getBegin(), "use nullptr") << FixItHint::CreateReplacement(
68  Range, NeedsSpace ? " nullptr" : "nullptr");
69 }
70 
71 /// \brief Returns the name of the outermost macro.
72 ///
73 /// Given
74 /// \code
75 /// #define MY_NULL NULL
76 /// \endcode
77 /// If \p Loc points to NULL, this function will return the name MY_NULL.
78 StringRef getOutermostMacroName(SourceLocation Loc, const SourceManager &SM,
79  const LangOptions &LO) {
80  assert(Loc.isMacroID());
81  SourceLocation OutermostMacroLoc;
82 
83  while (Loc.isMacroID()) {
84  OutermostMacroLoc = Loc;
85  Loc = SM.getImmediateMacroCallerLoc(Loc);
86  }
87 
88  return Lexer::getImmediateMacroName(OutermostMacroLoc, SM, LO);
89 }
90 
91 /// \brief RecursiveASTVisitor for ensuring all nodes rooted at a given AST
92 /// subtree that have file-level source locations corresponding to a macro
93 /// argument have implicit NullTo(Member)Pointer nodes as ancestors.
94 class MacroArgUsageVisitor : public RecursiveASTVisitor<MacroArgUsageVisitor> {
95 public:
96  MacroArgUsageVisitor(SourceLocation CastLoc, const SourceManager &SM)
97  : CastLoc(CastLoc), SM(SM), Visited(false), CastFound(false),
98  InvalidFound(false) {
99  assert(CastLoc.isFileID());
100  }
101 
102  bool TraverseStmt(Stmt *S) {
103  bool VisitedPreviously = Visited;
104 
105  if (!RecursiveASTVisitor<MacroArgUsageVisitor>::TraverseStmt(S))
106  return false;
107 
108  // The point at which VisitedPreviously is false and Visited is true is the
109  // root of a subtree containing nodes whose locations match CastLoc. It's
110  // at this point we test that the Implicit NullTo(Member)Pointer cast was
111  // found or not.
112  if (!VisitedPreviously) {
113  if (Visited && !CastFound) {
114  // Found nodes with matching SourceLocations but didn't come across a
115  // cast. This is an invalid macro arg use. Can stop traversal
116  // completely now.
117  InvalidFound = true;
118  return false;
119  }
120  // Reset state as we unwind back up the tree.
121  CastFound = false;
122  Visited = false;
123  }
124  return true;
125  }
126 
127  bool VisitStmt(Stmt *S) {
128  if (SM.getFileLoc(S->getBeginLoc()) != CastLoc)
129  return true;
130  Visited = true;
131 
132  const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(S);
133  if (Cast && (Cast->getCastKind() == CK_NullToPointer ||
134  Cast->getCastKind() == CK_NullToMemberPointer))
135  CastFound = true;
136 
137  return true;
138  }
139 
140  bool TraverseInitListExpr(InitListExpr *S) {
141  // Only go through the semantic form of the InitListExpr, because
142  // ImplicitCast might not appear in the syntactic form, and this results in
143  // finding usages of the macro argument that don't have a ImplicitCast as an
144  // ancestor (thus invalidating the replacement) when they actually have.
145  return RecursiveASTVisitor<MacroArgUsageVisitor>::
146  TraverseSynOrSemInitListExpr(
147  S->isSemanticForm() ? S : S->getSemanticForm());
148  }
149 
150  bool foundInvalid() const { return InvalidFound; }
151 
152 private:
153  SourceLocation CastLoc;
154  const SourceManager &SM;
155 
156  bool Visited;
157  bool CastFound;
158  bool InvalidFound;
159 };
160 
161 /// \brief Looks for implicit casts as well as sequences of 0 or more explicit
162 /// casts with an implicit null-to-pointer cast within.
163 ///
164 /// The matcher this visitor is used with will find a single implicit cast or a
165 /// top-most explicit cast (i.e. it has no explicit casts as an ancestor) where
166 /// an implicit cast is nested within. However, there is no guarantee that only
167 /// explicit casts exist between the found top-most explicit cast and the
168 /// possibly more than one nested implicit cast. This visitor finds all cast
169 /// sequences with an implicit cast to null within and creates a replacement
170 /// leaving the outermost explicit cast unchanged to avoid introducing
171 /// ambiguities.
172 class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {
173 public:
174  CastSequenceVisitor(ASTContext &Context, ArrayRef<StringRef> NullMacros,
175  ClangTidyCheck &check)
176  : SM(Context.getSourceManager()), Context(Context),
177  NullMacros(NullMacros), Check(check), FirstSubExpr(nullptr),
178  PruneSubtree(false) {}
179 
180  bool TraverseStmt(Stmt *S) {
181  // Stop traversing down the tree if requested.
182  if (PruneSubtree) {
183  PruneSubtree = false;
184  return true;
185  }
186  return RecursiveASTVisitor<CastSequenceVisitor>::TraverseStmt(S);
187  }
188 
189  // Only VisitStmt is overridden as we shouldn't find other base AST types
190  // within a cast expression.
191  bool VisitStmt(Stmt *S) {
192  auto *C = dyn_cast<CastExpr>(S);
193  // Catch the castExpr inside cxxDefaultArgExpr.
194  if (auto *E = dyn_cast<CXXDefaultArgExpr>(S)) {
195  C = dyn_cast<CastExpr>(E->getExpr());
196  FirstSubExpr = nullptr;
197  }
198  if (!C) {
199  FirstSubExpr = nullptr;
200  return true;
201  }
202 
203  auto* CastSubExpr = C->getSubExpr()->IgnoreParens();
204  // Ignore cast expressions which cast nullptr literal.
205  if (isa<CXXNullPtrLiteralExpr>(CastSubExpr)) {
206  return true;
207  }
208 
209  if (!FirstSubExpr)
210  FirstSubExpr = CastSubExpr;
211 
212  if (C->getCastKind() != CK_NullToPointer &&
213  C->getCastKind() != CK_NullToMemberPointer) {
214  return true;
215  }
216 
217  SourceLocation StartLoc = FirstSubExpr->getBeginLoc();
218  SourceLocation EndLoc = FirstSubExpr->getEndLoc();
219 
220  // If the location comes from a macro arg expansion, *all* uses of that
221  // arg must be checked to result in NullTo(Member)Pointer casts.
222  //
223  // If the location comes from a macro body expansion, check to see if its
224  // coming from one of the allowed 'NULL' macros.
225  if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {
226  SourceLocation FileLocStart = SM.getFileLoc(StartLoc),
227  FileLocEnd = SM.getFileLoc(EndLoc);
228  SourceLocation ImmediateMacroArgLoc, MacroLoc;
229  // Skip NULL macros used in macro.
230  if (!getMacroAndArgLocations(StartLoc, ImmediateMacroArgLoc, MacroLoc) ||
231  ImmediateMacroArgLoc != FileLocStart)
232  return skipSubTree();
233 
234  if (isReplaceableRange(FileLocStart, FileLocEnd, SM) &&
235  allArgUsesValid(C)) {
236  replaceWithNullptr(Check, SM, FileLocStart, FileLocEnd);
237  }
238  return true;
239  }
240 
241  if (SM.isMacroBodyExpansion(StartLoc) && SM.isMacroBodyExpansion(EndLoc)) {
242  StringRef OutermostMacroName =
243  getOutermostMacroName(StartLoc, SM, Context.getLangOpts());
244 
245  // Check to see if the user wants to replace the macro being expanded.
246  if (std::find(NullMacros.begin(), NullMacros.end(), OutermostMacroName) ==
247  NullMacros.end()) {
248  return skipSubTree();
249  }
250 
251  StartLoc = SM.getFileLoc(StartLoc);
252  EndLoc = SM.getFileLoc(EndLoc);
253  }
254 
255  if (!isReplaceableRange(StartLoc, EndLoc, SM)) {
256  return skipSubTree();
257  }
258  replaceWithNullptr(Check, SM, StartLoc, EndLoc);
259 
260  return true;
261  }
262 
263 private:
264  bool skipSubTree() {
265  PruneSubtree = true;
266  return true;
267  }
268 
269  /// \brief Tests that all expansions of a macro arg, one of which expands to
270  /// result in \p CE, yield NullTo(Member)Pointer casts.
271  bool allArgUsesValid(const CastExpr *CE) {
272  SourceLocation CastLoc = CE->getBeginLoc();
273 
274  // Step 1: Get location of macro arg and location of the macro the arg was
275  // provided to.
276  SourceLocation ArgLoc, MacroLoc;
277  if (!getMacroAndArgLocations(CastLoc, ArgLoc, MacroLoc))
278  return false;
279 
280  // Step 2: Find the first ancestor that doesn't expand from this macro.
281  ast_type_traits::DynTypedNode ContainingAncestor;
282  if (!findContainingAncestor(
283  ast_type_traits::DynTypedNode::create<Stmt>(*CE), MacroLoc,
284  ContainingAncestor))
285  return false;
286 
287  // Step 3:
288  // Visit children of this containing parent looking for the least-descended
289  // nodes of the containing parent which are macro arg expansions that expand
290  // from the given arg location.
291  // Visitor needs: arg loc.
292  MacroArgUsageVisitor ArgUsageVisitor(SM.getFileLoc(CastLoc), SM);
293  if (const auto *D = ContainingAncestor.get<Decl>())
294  ArgUsageVisitor.TraverseDecl(const_cast<Decl *>(D));
295  else if (const auto *S = ContainingAncestor.get<Stmt>())
296  ArgUsageVisitor.TraverseStmt(const_cast<Stmt *>(S));
297  else
298  llvm_unreachable("Unhandled ContainingAncestor node type");
299 
300  return !ArgUsageVisitor.foundInvalid();
301  }
302 
303  /// \brief Given the SourceLocation for a macro arg expansion, finds the
304  /// non-macro SourceLocation of the macro the arg was passed to and the
305  /// non-macro SourceLocation of the argument in the arg list to that macro.
306  /// These results are returned via \c MacroLoc and \c ArgLoc respectively.
307  /// These values are undefined if the return value is false.
308  ///
309  /// \returns false if one of the returned SourceLocations would be a
310  /// SourceLocation pointing within the definition of another macro.
311  bool getMacroAndArgLocations(SourceLocation Loc, SourceLocation &ArgLoc,
312  SourceLocation &MacroLoc) {
313  assert(Loc.isMacroID() && "Only reasonble to call this on macros");
314 
315  ArgLoc = Loc;
316 
317  // Find the location of the immediate macro expansion.
318  while (true) {
319  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);
320  const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
321  const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
322 
323  SourceLocation OldArgLoc = ArgLoc;
324  ArgLoc = Expansion.getExpansionLocStart();
325  if (!Expansion.isMacroArgExpansion()) {
326  if (!MacroLoc.isFileID())
327  return false;
328 
329  StringRef Name =
330  Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts());
331  return std::find(NullMacros.begin(), NullMacros.end(), Name) !=
332  NullMacros.end();
333  }
334 
335  MacroLoc = SM.getExpansionRange(ArgLoc).getBegin();
336 
337  ArgLoc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second);
338  if (ArgLoc.isFileID())
339  return true;
340 
341  // If spelling location resides in the same FileID as macro expansion
342  // location, it means there is no inner macro.
343  FileID MacroFID = SM.getFileID(MacroLoc);
344  if (SM.isInFileID(ArgLoc, MacroFID)) {
345  // Don't transform this case. If the characters that caused the
346  // null-conversion come from within a macro, they can't be changed.
347  return false;
348  }
349  }
350 
351  llvm_unreachable("getMacroAndArgLocations");
352  }
353 
354  /// \brief Tests if TestMacroLoc is found while recursively unravelling
355  /// expansions starting at TestLoc. TestMacroLoc.isFileID() must be true.
356  /// Implementation is very similar to getMacroAndArgLocations() except in this
357  /// case, it's not assumed that TestLoc is expanded from a macro argument.
358  /// While unravelling expansions macro arguments are handled as with
359  /// getMacroAndArgLocations() but in this function macro body expansions are
360  /// also handled.
361  ///
362  /// False means either:
363  /// - TestLoc is not from a macro expansion.
364  /// - TestLoc is from a different macro expansion.
365  bool expandsFrom(SourceLocation TestLoc, SourceLocation TestMacroLoc) {
366  if (TestLoc.isFileID()) {
367  return false;
368  }
369 
370  SourceLocation Loc = TestLoc, MacroLoc;
371 
372  while (true) {
373  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
374  const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);
375  const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();
376 
377  Loc = Expansion.getExpansionLocStart();
378 
379  if (!Expansion.isMacroArgExpansion()) {
380  if (Loc.isFileID()) {
381  return Loc == TestMacroLoc;
382  }
383  // Since Loc is still a macro ID and it's not an argument expansion, we
384  // don't need to do the work of handling an argument expansion. Simply
385  // keep recursively expanding until we hit a FileID or a macro arg
386  // expansion or a macro arg expansion.
387  continue;
388  }
389 
390  MacroLoc = SM.getImmediateExpansionRange(Loc).getBegin();
391  if (MacroLoc.isFileID() && MacroLoc == TestMacroLoc) {
392  // Match made.
393  return true;
394  }
395 
396  Loc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second);
397  if (Loc.isFileID()) {
398  // If we made it this far without finding a match, there is no match to
399  // be made.
400  return false;
401  }
402  }
403 
404  llvm_unreachable("expandsFrom");
405  }
406 
407  /// \brief Given a starting point \c Start in the AST, find an ancestor that
408  /// doesn't expand from the macro called at file location \c MacroLoc.
409  ///
410  /// \pre MacroLoc.isFileID()
411  /// \returns true if such an ancestor was found, false otherwise.
412  bool findContainingAncestor(ast_type_traits::DynTypedNode Start,
413  SourceLocation MacroLoc,
414  ast_type_traits::DynTypedNode &Result) {
415  // Below we're only following the first parent back up the AST. This should
416  // be fine since for the statements we care about there should only be one
417  // parent, except for the case specified below.
418 
419  assert(MacroLoc.isFileID());
420 
421  while (true) {
422  const auto &Parents = Context.getParents(Start);
423  if (Parents.empty())
424  return false;
425  if (Parents.size() > 1) {
426  // If there are more than one parents, don't do the replacement unless
427  // they are InitListsExpr (semantic and syntactic form). In this case we
428  // can choose any one here, and the ASTVisitor will take care of
429  // traversing the right one.
430  for (const auto &Parent : Parents) {
431  if (!Parent.get<InitListExpr>())
432  return false;
433  }
434  }
435 
436  const ast_type_traits::DynTypedNode &Parent = Parents[0];
437 
438  SourceLocation Loc;
439  if (const auto *D = Parent.get<Decl>())
440  Loc = D->getBeginLoc();
441  else if (const auto *S = Parent.get<Stmt>())
442  Loc = S->getBeginLoc();
443 
444  // TypeLoc and NestedNameSpecifierLoc are members of the parent map. Skip
445  // them and keep going up.
446  if (Loc.isValid()) {
447  if (!expandsFrom(Loc, MacroLoc)) {
448  Result = Parent;
449  return true;
450  }
451  }
452  Start = Parent;
453  }
454 
455  llvm_unreachable("findContainingAncestor");
456  }
457 
458 private:
459  SourceManager &SM;
460  ASTContext &Context;
461  ArrayRef<StringRef> NullMacros;
462  ClangTidyCheck &Check;
463  Expr *FirstSubExpr;
464  bool PruneSubtree;
465 };
466 
467 } // namespace
468 
469 UseNullptrCheck::UseNullptrCheck(StringRef Name, ClangTidyContext *Context)
470  : ClangTidyCheck(Name, Context),
471  NullMacrosStr(Options.get("NullMacros", "")) {
472  StringRef(NullMacrosStr).split(NullMacros, ",");
473 }
474 
476  Options.store(Opts, "NullMacros", NullMacrosStr);
477 }
478 
479 void UseNullptrCheck::registerMatchers(MatchFinder *Finder) {
480  // Only register the matcher for C++. Because this checker is used for
481  // modernization, it is reasonable to run it on any C++ standard with the
482  // assumption the user is trying to modernize their codebase.
483  if (getLangOpts().CPlusPlus)
484  Finder->addMatcher(makeCastSequenceMatcher(), this);
485 }
486 
487 void UseNullptrCheck::check(const MatchFinder::MatchResult &Result) {
488  const auto *NullCast = Result.Nodes.getNodeAs<CastExpr>(CastSequence);
489  assert(NullCast && "Bad Callback. No node provided");
490 
491  // Given an implicit null-ptr cast or an explicit cast with an implicit
492  // null-to-pointer cast within use CastSequenceVisitor to identify sequences
493  // of explicit casts that can be converted into 'nullptr'.
494  CastSequenceVisitor(*Result.Context, NullMacros, *this)
495  .TraverseStmt(const_cast<CastExpr *>(NullCast));
496 }
497 
498 } // namespace modernize
499 } // namespace tidy
500 } // 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
Some operations such as code completion produce a set of candidates.
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
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
const Decl * D
Definition: XRefs.cpp:79
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...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
CharSourceRange Range
SourceRange for the file name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.