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 return traverse( 45 TK_AsIs, 46 castExpr(anyOf(ImplicitCastToNull, 47 explicitCastExpr(hasDescendant(ImplicitCastToNull))), 48 unless(hasAncestor(explicitCastExpr()))) 49 .bind(CastSequence)); 50 } 51 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. 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. 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: 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 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 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 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 151 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: 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 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. 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: 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. 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 DynTypedNode ContainingAncestor; 281 if (!findContainingAncestor(DynTypedNode::create<Stmt>(*CE), MacroLoc, 282 ContainingAncestor)) 283 return false; 284 285 // Step 3: 286 // Visit children of this containing parent looking for the least-descended 287 // nodes of the containing parent which are macro arg expansions that expand 288 // from the given arg location. 289 // Visitor needs: arg loc. 290 MacroArgUsageVisitor ArgUsageVisitor(SM.getFileLoc(CastLoc), SM); 291 if (const auto *D = ContainingAncestor.get<Decl>()) 292 ArgUsageVisitor.TraverseDecl(const_cast<Decl *>(D)); 293 else if (const auto *S = ContainingAncestor.get<Stmt>()) 294 ArgUsageVisitor.TraverseStmt(const_cast<Stmt *>(S)); 295 else 296 llvm_unreachable("Unhandled ContainingAncestor node type"); 297 298 return !ArgUsageVisitor.foundInvalid(); 299 } 300 301 /// Given the SourceLocation for a macro arg expansion, finds the 302 /// non-macro SourceLocation of the macro the arg was passed to and the 303 /// non-macro SourceLocation of the argument in the arg list to that macro. 304 /// These results are returned via \c MacroLoc and \c ArgLoc respectively. 305 /// These values are undefined if the return value is false. 306 /// 307 /// \returns false if one of the returned SourceLocations would be a 308 /// SourceLocation pointing within the definition of another macro. 309 bool getMacroAndArgLocations(SourceLocation Loc, SourceLocation &ArgLoc, 310 SourceLocation &MacroLoc) { 311 assert(Loc.isMacroID() && "Only reasonable to call this on macros"); 312 313 ArgLoc = Loc; 314 315 // Find the location of the immediate macro expansion. 316 while (true) { 317 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc); 318 const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first); 319 const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 320 321 SourceLocation OldArgLoc = ArgLoc; 322 ArgLoc = Expansion.getExpansionLocStart(); 323 if (!Expansion.isMacroArgExpansion()) { 324 if (!MacroLoc.isFileID()) 325 return false; 326 327 StringRef Name = 328 Lexer::getImmediateMacroName(OldArgLoc, SM, Context.getLangOpts()); 329 return llvm::is_contained(NullMacros, Name); 330 } 331 332 MacroLoc = SM.getExpansionRange(ArgLoc).getBegin(); 333 334 ArgLoc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second); 335 if (ArgLoc.isFileID()) 336 return true; 337 338 // If spelling location resides in the same FileID as macro expansion 339 // location, it means there is no inner macro. 340 FileID MacroFID = SM.getFileID(MacroLoc); 341 if (SM.isInFileID(ArgLoc, MacroFID)) { 342 // Don't transform this case. If the characters that caused the 343 // null-conversion come from within a macro, they can't be changed. 344 return false; 345 } 346 } 347 348 llvm_unreachable("getMacroAndArgLocations"); 349 } 350 351 /// Tests if TestMacroLoc is found while recursively unravelling 352 /// expansions starting at TestLoc. TestMacroLoc.isFileID() must be true. 353 /// Implementation is very similar to getMacroAndArgLocations() except in this 354 /// case, it's not assumed that TestLoc is expanded from a macro argument. 355 /// While unravelling expansions macro arguments are handled as with 356 /// getMacroAndArgLocations() but in this function macro body expansions are 357 /// also handled. 358 /// 359 /// False means either: 360 /// - TestLoc is not from a macro expansion. 361 /// - TestLoc is from a different macro expansion. 362 bool expandsFrom(SourceLocation TestLoc, SourceLocation TestMacroLoc) { 363 if (TestLoc.isFileID()) { 364 return false; 365 } 366 367 SourceLocation Loc = TestLoc, MacroLoc; 368 369 while (true) { 370 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 371 const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first); 372 const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 373 374 Loc = Expansion.getExpansionLocStart(); 375 376 if (!Expansion.isMacroArgExpansion()) { 377 if (Loc.isFileID()) { 378 return Loc == TestMacroLoc; 379 } 380 // Since Loc is still a macro ID and it's not an argument expansion, we 381 // don't need to do the work of handling an argument expansion. Simply 382 // keep recursively expanding until we hit a FileID or a macro arg 383 // expansion or a macro arg expansion. 384 continue; 385 } 386 387 MacroLoc = SM.getImmediateExpansionRange(Loc).getBegin(); 388 if (MacroLoc.isFileID() && MacroLoc == TestMacroLoc) { 389 // Match made. 390 return true; 391 } 392 393 Loc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second); 394 if (Loc.isFileID()) { 395 // If we made it this far without finding a match, there is no match to 396 // be made. 397 return false; 398 } 399 } 400 401 llvm_unreachable("expandsFrom"); 402 } 403 404 /// Given a starting point \c Start in the AST, find an ancestor that 405 /// doesn't expand from the macro called at file location \c MacroLoc. 406 /// 407 /// \pre MacroLoc.isFileID() 408 /// \returns true if such an ancestor was found, false otherwise. 409 bool findContainingAncestor(DynTypedNode Start, SourceLocation MacroLoc, 410 DynTypedNode &Result) { 411 // Below we're only following the first parent back up the AST. This should 412 // be fine since for the statements we care about there should only be one 413 // parent, except for the case specified below. 414 415 assert(MacroLoc.isFileID()); 416 417 while (true) { 418 const auto &Parents = Context.getParents(Start); 419 if (Parents.empty()) 420 return false; 421 if (Parents.size() > 1) { 422 // If there are more than one parents, don't do the replacement unless 423 // they are InitListsExpr (semantic and syntactic form). In this case we 424 // can choose any one here, and the ASTVisitor will take care of 425 // traversing the right one. 426 for (const auto &Parent : Parents) { 427 if (!Parent.get<InitListExpr>()) 428 return false; 429 } 430 } 431 432 const DynTypedNode &Parent = Parents[0]; 433 434 SourceLocation Loc; 435 if (const auto *D = Parent.get<Decl>()) 436 Loc = D->getBeginLoc(); 437 else if (const auto *S = Parent.get<Stmt>()) 438 Loc = S->getBeginLoc(); 439 440 // TypeLoc and NestedNameSpecifierLoc are members of the parent map. Skip 441 // them and keep going up. 442 if (Loc.isValid()) { 443 if (!expandsFrom(Loc, MacroLoc)) { 444 Result = Parent; 445 return true; 446 } 447 } 448 Start = Parent; 449 } 450 451 llvm_unreachable("findContainingAncestor"); 452 } 453 454 private: 455 SourceManager &SM; 456 ASTContext &Context; 457 ArrayRef<StringRef> NullMacros; 458 ClangTidyCheck &Check; 459 Expr *FirstSubExpr; 460 bool PruneSubtree; 461 }; 462 463 } // namespace 464 465 UseNullptrCheck::UseNullptrCheck(StringRef Name, ClangTidyContext *Context) 466 : ClangTidyCheck(Name, Context), 467 NullMacrosStr(Options.get("NullMacros", "")) { 468 StringRef(NullMacrosStr).split(NullMacros, ","); 469 } 470 471 void UseNullptrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { 472 Options.store(Opts, "NullMacros", NullMacrosStr); 473 } 474 475 void UseNullptrCheck::registerMatchers(MatchFinder *Finder) { 476 Finder->addMatcher(makeCastSequenceMatcher(), this); 477 } 478 479 void UseNullptrCheck::check(const MatchFinder::MatchResult &Result) { 480 const auto *NullCast = Result.Nodes.getNodeAs<CastExpr>(CastSequence); 481 assert(NullCast && "Bad Callback. No node provided"); 482 483 // Given an implicit null-ptr cast or an explicit cast with an implicit 484 // null-to-pointer cast within use CastSequenceVisitor to identify sequences 485 // of explicit casts that can be converted into 'nullptr'. 486 CastSequenceVisitor(*Result.Context, NullMacros, *this) 487 .TraverseStmt(const_cast<CastExpr *>(NullCast)); 488 } 489 490 } // namespace modernize 491 } // namespace tidy 492 } // namespace clang 493