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