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