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