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