<div dir="ltr">Sorry for the breakage. I was hoping that r245471 fixes it, but apparently, it didn't. And yes, we may need help reproducing the issue / testing possible fixes, as the only platform where the test fails, is Darwin.<div class="gmail_extra"><br><div class="gmail_quote">On Wed, Aug 19, 2015 at 10:32 PM, Justin Bogner <span dir="ltr"><<a href="mailto:mail@justinbogner.com" target="_blank">mail@justinbogner.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">Justin Bogner <<a href="mailto:mail@justinbogner.com">mail@justinbogner.com</a>> writes:<br>
> Alexander Kornienko via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> writes:<br>
>> Author: alexfh<br>
>> Date: Wed Aug 19 08:13:12 2015<br>
>> New Revision: 245434<br>
>><br>
>> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=245434&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=245434&view=rev</a><br>
>> Log:<br>
>> [clang-tidy] Add use-nullptr check to clang-tidy.<br>
>><br>
>> Move UseNullptr from clang-modernize to modernize module in clang-tidy.<br>
>><br>
>> <a href="http://reviews.llvm.org/D12081" rel="noreferrer" target="_blank">http://reviews.llvm.org/D12081</a><br>
><br>
> The test this added has been failing tests on Darwin all morning, but it<br>
> doesn't revert cleanly. Can you fix this fairly quickly? Otherwise I'll<br>
> try harder to revert.<br>
><br>
> <a href="http://lab.llvm.org:8080/green/job/clang-stage1-configure-RA_check/10339/" rel="noreferrer" target="_blank">http://lab.llvm.org:8080/green/job/clang-stage1-configure-RA_check/10339/</a><br>
<br>
</span>Reverted in r245493. Angel: feel free to ping me if you need help<br>
debugging the problem locally.<br>
<div class="HOEnZb"><div class="h5"><br>
>><br>
>> Patch by Angel Garcia!<br>
>><br>
>> Added:<br>
>>     clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp<br>
>>     clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h<br>
>>     clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp<br>
>>     clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp<br>
>> Modified:<br>
>>     clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt<br>
>>     clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp<br>
>><br>
>> Modified: clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt<br>
>> URL:<br>
>><br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt?rev=245434&r1=245433&r2=245434&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt?rev=245434&r1=245433&r2=245434&view=diff</a><br>
>> ==============================================================================<br>
>> --- clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt (original)<br>
>> +++ clang-tools-extra/trunk/clang-tidy/modernize/CMakeLists.txt Wed<br>
> Aug 19 08:13:12 2015<br>
>> @@ -5,6 +5,7 @@ add_clang_library(clangTidyModernizeModu<br>
>>    LoopConvertUtils.cpp<br>
>>    ModernizeTidyModule.cpp<br>
>>    PassByValueCheck.cpp<br>
>> +  UseNullptrCheck.cpp<br>
>><br>
>>    LINK_LIBS<br>
>>    clangAST<br>
>><br>
>> Modified: clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp<br>
>> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp?rev=245434&r1=245433&r2=245434&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp?rev=245434&r1=245433&r2=245434&view=diff</a><br>
>> ==============================================================================<br>
>> ---<br>
> clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp<br>
> (original)<br>
>> +++<br>
> clang-tools-extra/trunk/clang-tidy/modernize/ModernizeTidyModule.cpp<br>
> Wed Aug 19 08:13:12 2015<br>
>> @@ -12,6 +12,7 @@<br>
>>  #include "../ClangTidyModuleRegistry.h"<br>
>>  #include "LoopConvertCheck.h"<br>
>>  #include "PassByValueCheck.h"<br>
>> +#include "UseNullptrCheck.h"<br>
>><br>
>>  using namespace clang::ast_matchers;<br>
>><br>
>> @@ -24,6 +25,7 @@ public:<br>
>>    void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {<br>
>>      CheckFactories.registerCheck<LoopConvertCheck>("modernize-loop-convert");<br>
>><br>
> CheckFactories.registerCheck<PassByValueCheck>("modernize-pass-by-value");<br>
>> +    CheckFactories.registerCheck<UseNullptrCheck>("modernize-use-nullptr");<br>
>>    }<br>
>><br>
>>    ClangTidyOptions getModuleOptions() override {<br>
>> @@ -31,6 +33,9 @@ public:<br>
>>      auto &Opts = Options.CheckOptions;<br>
>>      Opts["modernize-loop-convert.MinConfidence"] = "reasonable";<br>
>>      Opts["modernize-pass-by-value.IncludeStyle"] = "llvm"; // Also: "google".<br>
>> +<br>
>> +    // Comma-separated list of user-defined macros that behave like NULL.<br>
>> +    Opts["modernize-use-nullptr.UserNullMacros"] = "";<br>
>>      return Options;<br>
>>    }<br>
>>  };<br>
>><br>
>> Added: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp<br>
>> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp?rev=245434&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp?rev=245434&view=auto</a><br>
>> ==============================================================================<br>
>> --- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp (added)<br>
>> +++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.cpp<br>
> Wed Aug 19 08:13:12 2015<br>
>> @@ -0,0 +1,472 @@<br>
>> +//===--- UseNullptrCheck.cpp -<br>
> clang-tidy----------------------------------===//<br>
>> +//<br>
>> +//                     The LLVM Compiler Infrastructure<br>
>> +//<br>
>> +// This file is distributed under the University of Illinois Open Source<br>
>> +// License. See LICENSE.TXT for details.<br>
>> +//<br>
>><br>
> +//===----------------------------------------------------------------------===//<br>
>> +<br>
>> +#include "UseNullptrCheck.h"<br>
>> +#include "clang/AST/ASTContext.h"<br>
>> +#include "clang/AST/RecursiveASTVisitor.h"<br>
>> +#include "clang/ASTMatchers/ASTMatchFinder.h"<br>
>> +#include "clang/Lex/Lexer.h"<br>
>> +<br>
>> +using namespace clang;<br>
>> +using namespace clang::ast_matchers;<br>
>> +using namespace llvm;<br>
>> +<br>
>> +namespace clang {<br>
>> +namespace tidy {<br>
>> +namespace modernize {<br>
>> +<br>
>> +const char CastSequence[] = "sequence";<br>
>> +const char NullMacroName[] = "NULL";<br>
>> +<br>
>> +/// \brief Matches cast expressions that have a cast kind of CK_NullToPointer<br>
>> +/// or CK_NullToMemberPointer.<br>
>> +///<br>
>> +/// Given<br>
>> +/// \code<br>
>> +///   int *p = 0;<br>
>> +/// \endcode<br>
>> +/// implicitCastExpr(isNullToPointer()) matches the implicit cast clang adds<br>
>> +/// around \c 0.<br>
>> +AST_MATCHER(CastExpr, isNullToPointer) {<br>
>> +  return Node.getCastKind() == CK_NullToPointer ||<br>
>> +         Node.getCastKind() == CK_NullToMemberPointer;<br>
>> +}<br>
>> +<br>
>> +AST_MATCHER(Type, sugaredNullptrType) {<br>
>> +  const Type *DesugaredType = Node.getUnqualifiedDesugaredType();<br>
>> +  if (const BuiltinType *BT = dyn_cast<BuiltinType>(DesugaredType))<br>
>> +    return BT->getKind() == BuiltinType::NullPtr;<br>
>> +  return false;<br>
>> +}<br>
>> +<br>
>> +/// \brief Create a matcher that finds implicit casts as well as<br>
> the head of a<br>
>> +/// sequence of zero or more nested explicit casts that have an implicit cast<br>
>> +/// to null within.<br>
>> +/// Finding sequences of explict casts is necessary so that an<br>
> entire sequence<br>
>> +/// can be replaced instead of just the inner-most implicit cast.<br>
>> +StatementMatcher makeCastSequenceMatcher() {<br>
>> +  StatementMatcher ImplicitCastToNull = implicitCastExpr(<br>
>> +      isNullToPointer(),<br>
>> +      unless(hasSourceExpression(hasType(sugaredNullptrType()))));<br>
>> +<br>
>> +  return castExpr(anyOf(ImplicitCastToNull,<br>
>> +                        explicitCastExpr(hasDescendant(ImplicitCastToNull))),<br>
>> +                  unless(hasAncestor(explicitCastExpr())))<br>
>> +      .bind(CastSequence);<br>
>> +}<br>
>> +<br>
>> +bool isReplaceableRange(SourceLocation StartLoc, SourceLocation EndLoc,<br>
>> +                        const SourceManager &SM) {<br>
>> +  return SM.isWrittenInSameFile(StartLoc, EndLoc);<br>
>> +}<br>
>> +<br>
>> +/// \brief Replaces the provided range with the text "nullptr", but only if<br>
>> +/// the start and end location are both in main file.<br>
>> +/// Returns true if and only if a replacement was made.<br>
>> +void replaceWithNullptr(ClangTidyCheck &Check, SourceManager &SM,<br>
>> +                        SourceLocation StartLoc, SourceLocation EndLoc) {<br>
>> +  CharSourceRange Range(SourceRange(StartLoc, EndLoc), true);<br>
>> +  // Add a space if nullptr follows an alphanumeric character. This happens<br>
>> +  // whenever there is an c-style explicit cast to nullptr not surrounded by<br>
>> +  // parentheses and right beside a return statement.<br>
>> +  SourceLocation PreviousLocation = StartLoc.getLocWithOffset(-1);<br>
>> +  bool NeedsSpace = isAlphanumeric(*SM.getCharacterData(PreviousLocation));<br>
>> + Check.diag(Range.getBegin(), "use nullptr") <<<br>
> FixItHint::CreateReplacement(<br>
>> +      Range, NeedsSpace ? " nullptr" : "nullptr");<br>
>> +}<br>
>> +<br>
>> +/// \brief Returns the name of the outermost macro.<br>
>> +///<br>
>> +/// Given<br>
>> +/// \code<br>
>> +/// #define MY_NULL NULL<br>
>> +/// \endcode<br>
>> +/// If \p Loc points to NULL, this function will return the name MY_NULL.<br>
>> +StringRef getOutermostMacroName(SourceLocation Loc, const SourceManager &SM,<br>
>> +                                const LangOptions &LO) {<br>
>> +  assert(Loc.isMacroID());<br>
>> +  SourceLocation OutermostMacroLoc;<br>
>> +<br>
>> +  while (Loc.isMacroID()) {<br>
>> +    OutermostMacroLoc = Loc;<br>
>> +    Loc = SM.getImmediateMacroCallerLoc(Loc);<br>
>> +  }<br>
>> +<br>
>> +  return Lexer::getImmediateMacroName(OutermostMacroLoc, SM, LO);<br>
>> +}<br>
>> +<br>
>> +/// \brief RecursiveASTVisitor for ensuring all nodes rooted at a given AST<br>
>> +/// subtree that have file-level source locations corresponding to a macro<br>
>> +/// argument have implicit NullTo(Member)Pointer nodes as ancestors.<br>
>> +class MacroArgUsageVisitor : public<br>
> RecursiveASTVisitor<MacroArgUsageVisitor> {<br>
>> +public:<br>
>> +  MacroArgUsageVisitor(SourceLocation CastLoc, const SourceManager &SM)<br>
>> +      : CastLoc(CastLoc), SM(SM), Visited(false), CastFound(false),<br>
>> +        InvalidFound(false) {<br>
>> +    assert(CastLoc.isFileID());<br>
>> +  }<br>
>> +<br>
>> +  bool TraverseStmt(Stmt *S) {<br>
>> +    bool VisitedPreviously = Visited;<br>
>> +<br>
>> +    if (!RecursiveASTVisitor<MacroArgUsageVisitor>::TraverseStmt(S))<br>
>> +      return false;<br>
>> +<br>
>> + // The point at which VisitedPreviously is false and Visited is<br>
> true is the<br>
>> +    // root of a subtree containing nodes whose locations match CastLoc. It's<br>
>> +    // at this point we test that the Implicit NullTo(Member)Pointer cast was<br>
>> +    // found or not.<br>
>> +    if (!VisitedPreviously) {<br>
>> +      if (Visited && !CastFound) {<br>
>> +        // Found nodes with matching SourceLocations but didn't come across a<br>
>> +        // cast. This is an invalid macro arg use. Can stop traversal<br>
>> +        // completely now.<br>
>> +        InvalidFound = true;<br>
>> +        return false;<br>
>> +      }<br>
>> +      // Reset state as we unwind back up the tree.<br>
>> +      CastFound = false;<br>
>> +      Visited = false;<br>
>> +    }<br>
>> +    return true;<br>
>> +  }<br>
>> +<br>
>> +  bool VisitStmt(Stmt *S) {<br>
>> +    if (SM.getFileLoc(S->getLocStart()) != CastLoc)<br>
>> +      return true;<br>
>> +    Visited = true;<br>
>> +<br>
>> +    const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(S);<br>
>> +    if (Cast && (Cast->getCastKind() == CK_NullToPointer ||<br>
>> +                 Cast->getCastKind() == CK_NullToMemberPointer))<br>
>> +      CastFound = true;<br>
>> +<br>
>> +    return true;<br>
>> +  }<br>
>> +<br>
>> +  bool foundInvalid() const { return InvalidFound; }<br>
>> +<br>
>> +private:<br>
>> +  SourceLocation CastLoc;<br>
>> +  const SourceManager &SM;<br>
>> +<br>
>> +  bool Visited;<br>
>> +  bool CastFound;<br>
>> +  bool InvalidFound;<br>
>> +};<br>
>> +<br>
>> +/// \brief Looks for implicit casts as well as sequences of 0 or<br>
> more explicit<br>
>> +/// casts with an implicit null-to-pointer cast within.<br>
>> +///<br>
>> +/// The matcher this visitor is used with will find a single<br>
> implicit cast or a<br>
>> +/// top-most explicit cast (i.e. it has no explicit casts as an<br>
> ancestor) where<br>
>> +/// an implicit cast is nested within. However, there is no<br>
> guarantee that only<br>
>> +/// explicit casts exist between the found top-most explicit cast and the<br>
>> +/// possibly more than one nested implicit cast. This visitor finds all cast<br>
>> +/// sequences with an implicit cast to null within and creates a replacement<br>
>> +/// leaving the outermost explicit cast unchanged to avoid introducing<br>
>> +/// ambiguities.<br>
>> +class CastSequenceVisitor : public RecursiveASTVisitor<CastSequenceVisitor> {<br>
>> +public:<br>
>> +  CastSequenceVisitor(ASTContext &Context,<br>
>> +                      SmallVector<StringRef, 1> UserNullMacros,<br>
>> +                      ClangTidyCheck &check)<br>
>> +      : SM(Context.getSourceManager()), Context(Context),<br>
>> +        UserNullMacros(std::move(UserNullMacros)), Check(check),<br>
>> +        FirstSubExpr(nullptr), PruneSubtree(false) {}<br>
>> +<br>
>> +  bool TraverseStmt(Stmt *S) {<br>
>> +    // Stop traversing down the tree if requested.<br>
>> +    if (PruneSubtree) {<br>
>> +      PruneSubtree = false;<br>
>> +      return true;<br>
>> +    }<br>
>> +    return RecursiveASTVisitor<CastSequenceVisitor>::TraverseStmt(S);<br>
>> +  }<br>
>> +<br>
>> +  // Only VisitStmt is overridden as we shouldn't find other base AST types<br>
>> +  // within a cast expression.<br>
>> +  bool VisitStmt(Stmt *S) {<br>
>> +    CastExpr *C = dyn_cast<CastExpr>(S);<br>
>> +    if (!C) {<br>
>> +      FirstSubExpr = nullptr;<br>
>> +      return true;<br>
>> +    }<br>
>> +    if (!FirstSubExpr)<br>
>> +      FirstSubExpr = C->getSubExpr()->IgnoreParens();<br>
>> +<br>
>> +    if (C->getCastKind() != CK_NullToPointer &&<br>
>> +        C->getCastKind() != CK_NullToMemberPointer) {<br>
>> +      return true;<br>
>> +    }<br>
>> +<br>
>> +    SourceLocation StartLoc = FirstSubExpr->getLocStart();<br>
>> +    SourceLocation EndLoc = FirstSubExpr->getLocEnd();<br>
>> +<br>
>> +    // If the location comes from a macro arg expansion, *all* uses of that<br>
>> +    // arg must be checked to result in NullTo(Member)Pointer casts.<br>
>> +    //<br>
>> +    // If the location comes from a macro body expansion, check to see if its<br>
>> +    // coming from one of the allowed 'NULL' macros.<br>
>> +    if (SM.isMacroArgExpansion(StartLoc) && SM.isMacroArgExpansion(EndLoc)) {<br>
>> +      SourceLocation FileLocStart = SM.getFileLoc(StartLoc),<br>
>> +                     FileLocEnd = SM.getFileLoc(EndLoc);<br>
>> +      if (isReplaceableRange(FileLocStart, FileLocEnd, SM) &&<br>
>> +          allArgUsesValid(C)) {<br>
>> +        replaceWithNullptr(Check, SM, FileLocStart, FileLocEnd);<br>
>> +      }<br>
>> +      return skipSubTree();<br>
>> +    }<br>
>> +<br>
>> + if (SM.isMacroBodyExpansion(StartLoc) &&<br>
> SM.isMacroBodyExpansion(EndLoc)) {<br>
>> +      StringRef OutermostMacroName =<br>
>> +          getOutermostMacroName(StartLoc, SM, Context.getLangOpts());<br>
>> +<br>
>> +      // Check to see if the user wants to replace the macro being expanded.<br>
>> +      if (std::find(UserNullMacros.begin(), UserNullMacros.end(),<br>
>> +                    OutermostMacroName) == UserNullMacros.end()) {<br>
>> +        return skipSubTree();<br>
>> +      }<br>
>> +<br>
>> +      StartLoc = SM.getFileLoc(StartLoc);<br>
>> +      EndLoc = SM.getFileLoc(EndLoc);<br>
>> +    }<br>
>> +<br>
>> +    if (!isReplaceableRange(StartLoc, EndLoc, SM)) {<br>
>> +      return skipSubTree();<br>
>> +    }<br>
>> +    replaceWithNullptr(Check, SM, StartLoc, EndLoc);<br>
>> +<br>
>> +    return skipSubTree();<br>
>> +  }<br>
>> +<br>
>> +private:<br>
>> +  bool skipSubTree() {<br>
>> +    PruneSubtree = true;<br>
>> +    return true;<br>
>> +  }<br>
>> +<br>
>> + /// \brief Tests that all expansions of a macro arg, one of which<br>
> expands to<br>
>> +  /// result in \p CE, yield NullTo(Member)Pointer casts.<br>
>> +  bool allArgUsesValid(const CastExpr *CE) {<br>
>> +    SourceLocation CastLoc = CE->getLocStart();<br>
>> +<br>
>> + // Step 1: Get location of macro arg and location of the macro the<br>
> arg was<br>
>> +    // provided to.<br>
>> +    SourceLocation ArgLoc, MacroLoc;<br>
>> +    if (!getMacroAndArgLocations(CastLoc, ArgLoc, MacroLoc))<br>
>> +      return false;<br>
>> +<br>
>> +    // Step 2: Find the first ancestor that doesn't expand from this macro.<br>
>> +    ast_type_traits::DynTypedNode ContainingAncestor;<br>
>> +    if (!findContainingAncestor(<br>
>> +            ast_type_traits::DynTypedNode::create<Stmt>(*CE), MacroLoc,<br>
>> +            ContainingAncestor))<br>
>> +      return false;<br>
>> +<br>
>> +    // Step 3:<br>
>> + // Visit children of this containing parent looking for the<br>
> least-descended<br>
>> + // nodes of the containing parent which are macro arg expansions<br>
> that expand<br>
>> +    // from the given arg location.<br>
>> +    // Visitor needs: arg loc<br>
>> +    MacroArgUsageVisitor ArgUsageVisitor(SM.getFileLoc(CastLoc), SM);<br>
>> +    if (const auto *D = ContainingAncestor.get<Decl>())<br>
>> +      ArgUsageVisitor.TraverseDecl(const_cast<Decl *>(D));<br>
>> +    else if (const auto *S = ContainingAncestor.get<Stmt>())<br>
>> +      ArgUsageVisitor.TraverseStmt(const_cast<Stmt *>(S));<br>
>> +    else<br>
>> +      llvm_unreachable("Unhandled ContainingAncestor node type");<br>
>> +<br>
>> +    return !ArgUsageVisitor.foundInvalid();<br>
>> +  }<br>
>> +<br>
>> +  /// \brief Given the SourceLocation for a macro arg expansion, finds the<br>
>> +  /// non-macro SourceLocation of the macro the arg was passed to and the<br>
>> +  /// non-macro SourceLocation of the argument in the arg list to that macro.<br>
>> +  /// These results are returned via \c MacroLoc and \c ArgLoc respectively.<br>
>> +  /// These values are undefined if the return value is false.<br>
>> +  ///<br>
>> +  /// \returns false if one of the returned SourceLocations would be a<br>
>> +  /// SourceLocation pointing within the definition of another macro.<br>
>> +  bool getMacroAndArgLocations(SourceLocation Loc, SourceLocation &ArgLoc,<br>
>> +                               SourceLocation &MacroLoc) {<br>
>> +    assert(Loc.isMacroID() && "Only reasonble to call this on macros");<br>
>> +<br>
>> +    ArgLoc = Loc;<br>
>> +<br>
>> +    // Find the location of the immediate macro expansion.<br>
>> +    while (true) {<br>
>> +      std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(ArgLoc);<br>
>> +      const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);<br>
>> +      const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();<br>
>> +<br>
>> +      SourceLocation OldArgLoc = ArgLoc;<br>
>> +      ArgLoc = Expansion.getExpansionLocStart();<br>
>> +      if (!Expansion.isMacroArgExpansion()) {<br>
>> +        if (!MacroLoc.isFileID())<br>
>> +          return false;<br>
>> +<br>
>> +        StringRef Name =<br>
>> + Lexer::getImmediateMacroName(OldArgLoc, SM,<br>
> Context.getLangOpts());<br>
>> + return std::find(UserNullMacros.begin(), UserNullMacros.end(),<br>
> Name) !=<br>
>> +               UserNullMacros.end();<br>
>> +      }<br>
>> +<br>
>> +      MacroLoc = SM.getImmediateExpansionRange(ArgLoc).first;<br>
>> +<br>
>> +      ArgLoc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second);<br>
>> +      if (ArgLoc.isFileID())<br>
>> +        return true;<br>
>> +<br>
>> +      // If spelling location resides in the same FileID as macro expansion<br>
>> +      // location, it means there is no inner macro.<br>
>> +      FileID MacroFID = SM.getFileID(MacroLoc);<br>
>> +      if (SM.isInFileID(ArgLoc, MacroFID)) {<br>
>> +        // Don't transform this case. If the characters that caused the<br>
>> +        // null-conversion come from within a macro, they can't be changed.<br>
>> +        return false;<br>
>> +      }<br>
>> +    }<br>
>> +<br>
>> +    llvm_unreachable("getMacroAndArgLocations");<br>
>> +  }<br>
>> +<br>
>> +  /// \brief Tests if TestMacroLoc is found while recursively unravelling<br>
>> +  /// expansions starting at TestLoc. TestMacroLoc.isFileID() must be true.<br>
>> + /// Implementation is very similar to getMacroAndArgLocations()<br>
> except in this<br>
>> +  /// case, it's not assumed that TestLoc is expanded from a macro argument.<br>
>> +  /// While unravelling expansions macro arguments are handled as with<br>
>> + /// getMacroAndArgLocations() but in this function macro body<br>
> expansions are<br>
>> +  /// also handled.<br>
>> +  ///<br>
>> +  /// False means either:<br>
>> +  /// - TestLoc is not from a macro expansion<br>
>> +  /// - TestLoc is from a different macro expansion<br>
>> +  bool expandsFrom(SourceLocation TestLoc, SourceLocation TestMacroLoc) {<br>
>> +    if (TestLoc.isFileID()) {<br>
>> +      return false;<br>
>> +    }<br>
>> +<br>
>> +    SourceLocation Loc = TestLoc, MacroLoc;<br>
>> +<br>
>> +    while (true) {<br>
>> +      std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);<br>
>> +      const SrcMgr::SLocEntry *E = &SM.getSLocEntry(LocInfo.first);<br>
>> +      const SrcMgr::ExpansionInfo &Expansion = E->getExpansion();<br>
>> +<br>
>> +      Loc = Expansion.getExpansionLocStart();<br>
>> +<br>
>> +      if (!Expansion.isMacroArgExpansion()) {<br>
>> +        if (Loc.isFileID()) {<br>
>> +          return Loc == TestMacroLoc;<br>
>> +        }<br>
>> + // Since Loc is still a macro ID and it's not an argument<br>
> expansion, we<br>
>> + // don't need to do the work of handling an argument<br>
> expansion. Simply<br>
>> +        // keep recursively expanding until we hit a FileID or a macro arg<br>
>> +        // expansion or a macro arg expansion.<br>
>> +        continue;<br>
>> +      }<br>
>> +<br>
>> +      MacroLoc = SM.getImmediateExpansionRange(Loc).first;<br>
>> +      if (MacroLoc.isFileID() && MacroLoc == TestMacroLoc) {<br>
>> +        // Match made.<br>
>> +        return true;<br>
>> +      }<br>
>> +<br>
>> +      Loc = Expansion.getSpellingLoc().getLocWithOffset(LocInfo.second);<br>
>> +      if (Loc.isFileID()) {<br>
>> + // If we made it this far without finding a match, there is no<br>
> match to<br>
>> +        // be made.<br>
>> +        return false;<br>
>> +      }<br>
>> +    }<br>
>> +<br>
>> +    llvm_unreachable("expandsFrom");<br>
>> +  }<br>
>> +<br>
>> + /// \brief Given a starting point \c Start in the AST, find an<br>
> ancestor that<br>
>> +  /// doesn't expand from the macro called at file location \c MacroLoc.<br>
>> +  ///<br>
>> +  /// \pre MacroLoc.isFileID()<br>
>> +  /// \returns true if such an ancestor was found, false otherwise.<br>
>> +  bool findContainingAncestor(ast_type_traits::DynTypedNode Start,<br>
>> +                              SourceLocation MacroLoc,<br>
>> +                              ast_type_traits::DynTypedNode &Result) {<br>
>> + // Below we're only following the first parent back up the<br>
> AST. This should<br>
>> + // be fine since for the statements we care about there should<br>
> only be one<br>
>> +    // parent as far up as we care. If this assumption doesn't hold, need to<br>
>> +    // revisit what to do here.<br>
>> +<br>
>> +    assert(MacroLoc.isFileID());<br>
>> +<br>
>> +    while (true) {<br>
>> +      const auto &Parents = Context.getParents(Start);<br>
>> +      if (Parents.empty())<br>
>> +        return false;<br>
>> +      assert(Parents.size() == 1 &&<br>
>> +             "Found an ancestor with more than one parent!");<br>
>> +<br>
>> +      const ast_type_traits::DynTypedNode &Parent = Parents[0];<br>
>> +<br>
>> +      SourceLocation Loc;<br>
>> +      if (const auto *D = Parent.get<Decl>())<br>
>> +        Loc = D->getLocStart();<br>
>> +      else if (const auto *S = Parent.get<Stmt>())<br>
>> +        Loc = S->getLocStart();<br>
>> +      else<br>
>> + llvm_unreachable("Expected to find Decl or Stmt containing<br>
> ancestor");<br>
>> +<br>
>> +      if (!expandsFrom(Loc, MacroLoc)) {<br>
>> +        Result = Parent;<br>
>> +        return true;<br>
>> +      }<br>
>> +      Start = Parent;<br>
>> +    }<br>
>> +<br>
>> +    llvm_unreachable("findContainingAncestor");<br>
>> +  }<br>
>> +<br>
>> +private:<br>
>> +  SourceManager &SM;<br>
>> +  ASTContext &Context;<br>
>> +  const SmallVector<StringRef, 1> &UserNullMacros;<br>
>> +  ClangTidyCheck &Check;<br>
>> +  Expr *FirstSubExpr;<br>
>> +  bool PruneSubtree;<br>
>> +};<br>
>> +<br>
>> +UseNullptrCheck::UseNullptrCheck(StringRef Name, ClangTidyContext *Context)<br>
>> +    : ClangTidyCheck(Name, Context) {<br>
>> +  StringRef UserMacrosStr = Options.get("UserNullMacros", "");<br>
>> +  UserMacrosStr.split(UserNullMacros, ",");<br>
>> +  UserNullMacros.push_back(StringRef(NullMacroName));<br>
>> +}<br>
>> +<br>
>> +void UseNullptrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {<br>
>> +  Options.store(Opts, "UserNullMacros", "");<br>
>> +}<br>
>> +<br>
>> +void UseNullptrCheck::registerMatchers(MatchFinder *Finder) {<br>
>> +  Finder->addMatcher(makeCastSequenceMatcher(), this);<br>
>> +}<br>
>> +<br>
>> +void UseNullptrCheck::check(const MatchFinder::MatchResult &Result) {<br>
>> +  const auto *NullCast = Result.Nodes.getNodeAs<CastExpr>(CastSequence);<br>
>> +  assert(NullCast && "Bad Callback. No node provided");<br>
>> +<br>
>> +  // Given an implicit null-ptr cast or an explicit cast with an implicit<br>
>> + // null-to-pointer cast within use CastSequenceVisitor to identify<br>
> sequences<br>
>> +  // of explicit casts that can be converted into 'nullptr'.<br>
>> +  CastSequenceVisitor(*Result.Context, UserNullMacros, *this)<br>
>> +      .TraverseStmt(const_cast<CastExpr *>(NullCast));<br>
>> +}<br>
>> +<br>
>> +} // namespace modernize<br>
>> +} // namespace tidy<br>
>> +} // namespace clang<br>
>><br>
>> Added: clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h<br>
>> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h?rev=245434&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h?rev=245434&view=auto</a><br>
>> ==============================================================================<br>
>> --- clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h (added)<br>
>> +++ clang-tools-extra/trunk/clang-tidy/modernize/UseNullptrCheck.h<br>
> Wed Aug 19 08:13:12 2015<br>
>> @@ -0,0 +1,34 @@<br>
>> +//===--- UseNullptrCheck.h - clang-tidy--------------------------*-<br>
> C++ -*-===//<br>
>> +//<br>
>> +//                     The LLVM Compiler Infrastructure<br>
>> +//<br>
>> +// This file is distributed under the University of Illinois Open Source<br>
>> +// License. See LICENSE.TXT for details.<br>
>> +//<br>
>><br>
> +//===----------------------------------------------------------------------===//<br>
>> +<br>
>> +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_NULLPTR_H<br>
>> +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_NULLPTR_H<br>
>> +<br>
>> +#include "../ClangTidy.h"<br>
>> +<br>
>> +namespace clang {<br>
>> +namespace tidy {<br>
>> +namespace modernize {<br>
>> +<br>
>> +class UseNullptrCheck : public ClangTidyCheck {<br>
>> +public:<br>
>> +  UseNullptrCheck(StringRef Name, ClangTidyContext *Context);<br>
>> +  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;<br>
>> +  void registerMatchers(ast_matchers::MatchFinder *Finder) override;<br>
>> +  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;<br>
>> +<br>
>> +private:<br>
>> +  SmallVector<StringRef, 1> UserNullMacros;<br>
>> +};<br>
>> +<br>
>> +} // namespace modernize<br>
>> +} // namespace tidy<br>
>> +} // namespace clang<br>
>> +<br>
>> +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MODERNIZE_USE_NULLPTR_H<br>
>><br>
>> Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp<br>
>> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp?rev=245434&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp?rev=245434&view=auto</a><br>
>> ==============================================================================<br>
>> ---<br>
> clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp<br>
> (added)<br>
>> +++<br>
> clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr-basic.cpp<br>
> Wed Aug 19 08:13:12 2015<br>
>> @@ -0,0 +1,344 @@<br>
>> +// RUN: $(dirname %s)/check_clang_tidy.sh %s modernize-use-nullptr %t -- \<br>
>> +// RUN:   -std=c++98 -Wno-non-literal-null-conversion<br>
>> +// REQUIRES: shell<br>
>> +<br>
>> +const unsigned int g_null = 0;<br>
>> +#define NULL 0<br>
>> +<br>
>> +void test_assignment() {<br>
>> +  int *p1 = 0;<br>
>> + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
> [modernize-use-nullptr]<br>
>> +  // CHECK-FIXES: int *p1 = nullptr;<br>
>> +  p1 = 0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr<br>
>> +  // CHECK-FIXES: p1 = nullptr;<br>
>> +<br>
>> +  int *p2 = NULL;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
>> +  // CHECK-FIXES: int *p2 = nullptr;<br>
>> +<br>
>> +  p2 = p1;<br>
>> +  // CHECK-FIXES: p2 = p1;<br>
>> +<br>
>> +  const int null = 0;<br>
>> +  int *p3 = null;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
>> +  // CHECK-FIXES: int *p3 = nullptr;<br>
>> +<br>
>> +  p3 = NULL;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr<br>
>> +  // CHECK-FIXES: p3 = nullptr;<br>
>> +<br>
>> +  int *p4 = p3;<br>
>> +  // CHECK-FIXES: int *p4 = p3;<br>
>> +<br>
>> +  p4 = null;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: use nullptr<br>
>> +  // CHECK-FIXES: p4 = nullptr;<br>
>> +<br>
>> +  int i1 = 0;<br>
>> +<br>
>> +  int i2 = NULL;<br>
>> +<br>
>> +  int i3 = null;<br>
>> +<br>
>> +  int *p5, *p6, *p7;<br>
>> +  p5 = p6 = p7 = NULL;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr<br>
>> +  // CHECK-FIXES: p5 = p6 = p7 = nullptr;<br>
>> +}<br>
>> +<br>
>> +struct Foo {<br>
>> +  Foo(int *p = NULL) : m_p1(p) {}<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr<br>
>> +  // CHECK-FIXES: Foo(int *p = nullptr) : m_p1(p) {}<br>
>> +<br>
>> +  void bar(int *p = 0) {}<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: use nullptr<br>
>> +  // CHECK-FIXES: void bar(int *p = nullptr) {}<br>
>> +<br>
>> +  void baz(int i = 0) {}<br>
>> +<br>
>> +  int *m_p1;<br>
>> +  static int *m_p2;<br>
>> +};<br>
>> +<br>
>> +int *Foo::m_p2 = NULL;<br>
>> +// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr<br>
>> +// CHECK-FIXES: int *Foo::m_p2 = nullptr;<br>
>> +<br>
>> +template <typename T><br>
>> +struct Bar {<br>
>> +  Bar(T *p) : m_p(p) {<br>
>> +    m_p = static_cast<T*>(NULL);<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr<br>
>> +    // CHECK-FIXES: m_p = static_cast<T*>(nullptr);<br>
>> +<br>
>> +    m_p = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr<br>
>> +    // CHECK-FIXES: m_p = static_cast<T*>(nullptr);<br>
>> +<br>
>> +    m_p = static_cast<T*>(p ? p : static_cast<void*>(g_null));<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:54: warning: use nullptr<br>
>> + // CHECK-FIXES: m_p = static_cast<T*>(p ? p :<br>
> static_cast<void*>(nullptr));<br>
>> +<br>
>> +    T *p2 = static_cast<T*>(reinterpret_cast<int*>((void*)NULL));<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr<br>
>> +    // CHECK-FIXES: T *p2 = static_cast<T*>(nullptr);<br>
>> +<br>
>> +    m_p = NULL;<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: use nullptr<br>
>> +    // CHECK-FIXES: m_p = nullptr;<br>
>> +<br>
>> +    int i = static_cast<int>(0.f);<br>
>> +    T *i2 = static_cast<int>(0.f);<br>
>> +    // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
>> +    // CHECK-FIXES: T *i2 = nullptr;<br>
>> +  }<br>
>> +<br>
>> +  T *m_p;<br>
>> +};<br>
>> +<br>
>> +struct Baz {<br>
>> +  Baz() : i(0) {}<br>
>> +  int i;<br>
>> +};<br>
>> +<br>
>> +void test_cxx_cases() {<br>
>> +  Foo f(g_null);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr<br>
>> +  // CHECK-FIXES: Foo f(nullptr);<br>
>> +<br>
>> +  f.bar(NULL);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr<br>
>> +  // CHECK-FIXES: f.bar(nullptr);<br>
>> +<br>
>> +  f.baz(g_null);<br>
>> +<br>
>> +  f.m_p1 = 0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr<br>
>> +  // CHECK-FIXES: f.m_p1 = nullptr;<br>
>> +<br>
>> +  Bar<int> b(g_null);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr<br>
>> +  // CHECK-FIXES: Bar<int> b(nullptr);<br>
>> +<br>
>> +  Baz b2;<br>
>> +  int Baz::*memptr(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: use nullptr<br>
>> +  // CHECK-FIXES: int Baz::*memptr(nullptr);<br>
>> +<br>
>> +  memptr = 0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr<br>
>> +  // CHECK-FIXES: memptr = nullptr;<br>
>> +}<br>
>> +<br>
>> +void test_function_default_param1(void *p = 0);<br>
>> +// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr<br>
>> +// CHECK-FIXES: void test_function_default_param1(void *p = nullptr);<br>
>> +<br>
>> +void test_function_default_param2(void *p = NULL);<br>
>> +// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr<br>
>> +// CHECK-FIXES: void test_function_default_param2(void *p = nullptr);<br>
>> +<br>
>> +void test_function_default_param3(void *p = g_null);<br>
>> +// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: use nullptr<br>
>> +// CHECK-FIXES: void test_function_default_param3(void *p = nullptr);<br>
>> +<br>
>> +void test_function(int *p) {}<br>
>> +<br>
>> +void test_function_no_ptr_param(int i) {}<br>
>> +<br>
>> +void test_function_call() {<br>
>> +  test_function(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr<br>
>> +  // CHECK-FIXES: test_function(nullptr);<br>
>> +<br>
>> +  test_function(NULL);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr<br>
>> +  // CHECK-FIXES: test_function(nullptr);<br>
>> +<br>
>> +  test_function(g_null);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr<br>
>> +  // CHECK-FIXES: test_function(nullptr);<br>
>> +<br>
>> +  test_function_no_ptr_param(0);<br>
>> +}<br>
>> +<br>
>> +char *test_function_return1() {<br>
>> +  return 0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr<br>
>> +  // CHECK-FIXES: return nullptr;<br>
>> +}<br>
>> +<br>
>> +void *test_function_return2() {<br>
>> +  return NULL;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr<br>
>> +  // CHECK-FIXES: return nullptr;<br>
>> +}<br>
>> +<br>
>> +long *test_function_return3() {<br>
>> +  return g_null;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr<br>
>> +  // CHECK-FIXES: return nullptr;<br>
>> +}<br>
>> +<br>
>> +int test_function_return4() {<br>
>> +  return 0;<br>
>> +}<br>
>> +<br>
>> +int test_function_return5() {<br>
>> +  return NULL;<br>
>> +}<br>
>> +<br>
>> +int test_function_return6() {<br>
>> +  return g_null;<br>
>> +}<br>
>> +<br>
>> +int *test_function_return_cast1() {<br>
>> +  return(int)0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr<br>
>> +  // CHECK-FIXES: return nullptr;<br>
>> +}<br>
>> +<br>
>> +int *test_function_return_cast2() {<br>
>> +#define RET return<br>
>> +  RET(int)0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: use nullptr<br>
>> +  // CHECK-FIXES: RET nullptr;<br>
>> +#undef RET<br>
>> +}<br>
>> +<br>
>> +// Test parentheses expressions resulting in a nullptr.<br>
>> +int *test_parentheses_expression1() {<br>
>> +  return(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(nullptr);<br>
>> +}<br>
>> +<br>
>> +int *test_parentheses_expression2() {<br>
>> +  return(int(0.f));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(nullptr);<br>
>> +}<br>
>> +<br>
>> +int *test_nested_parentheses_expression() {<br>
>> +  return((((0))));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
>> +  // CHECK-FIXES: return((((nullptr))));<br>
>> +}<br>
>> +<br>
>> +void *test_parentheses_explicit_cast() {<br>
>> +  return(static_cast<void*>(0));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(static_cast<void*>(nullptr));<br>
>> +}<br>
>> +<br>
>> +void *test_parentheses_explicit_cast_sequence1() {<br>
>> +  return(static_cast<void*>(static_cast<int*>((void*)NULL)));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(static_cast<void*>(nullptr));<br>
>> +}<br>
>> +<br>
>> +void *test_parentheses_explicit_cast_sequence2() {<br>
>> +  return(static_cast<void*>(reinterpret_cast<int*>((float*)int(0.f))));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:29: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(static_cast<void*>(nullptr));<br>
>> +}<br>
>> +<br>
>> +// Test explicit cast expressions resulting in nullptr.<br>
>> +struct Bam {<br>
>> +  Bam(int *a) {}<br>
>> +  Bam(float *a) {}<br>
>> +  Bam operator=(int *a) { return Bam(a); }<br>
>> +  Bam operator=(float *a) { return Bam(a); }<br>
>> +};<br>
>> +<br>
>> +void ambiguous_function(int *a) {}<br>
>> +void ambiguous_function(float *a) {}<br>
>> +void const_ambiguous_function(const int *p) {}<br>
>> +void const_ambiguous_function(const float *p) {}<br>
>> +<br>
>> +void test_explicit_cast_ambiguous1() {<br>
>> +  ambiguous_function((int*)0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr<br>
>> +  // CHECK-FIXES: ambiguous_function((int*)nullptr);<br>
>> +}<br>
>> +<br>
>> +void test_explicit_cast_ambiguous2() {<br>
>> +  ambiguous_function((int*)(0));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:28: warning: use nullptr<br>
>> +  // CHECK-FIXES: ambiguous_function((int*)nullptr);<br>
>> +}<br>
>> +<br>
>> +void test_explicit_cast_ambiguous3() {<br>
>> +  ambiguous_function(static_cast<int*>(reinterpret_cast<int*>((float*)0)));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:40: warning: use nullptr<br>
>> +  // CHECK-FIXES: ambiguous_function(static_cast<int*>(nullptr));<br>
>> +}<br>
>> +<br>
>> +Bam test_explicit_cast_ambiguous4() {<br>
>> +  return(((int*)(0)));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use nullptr<br>
>> +  // CHECK-FIXES: return(((int*)nullptr));<br>
>> +}<br>
>> +<br>
>> +void test_explicit_cast_ambiguous5() {<br>
>> +  // Test for ambiguous overloaded constructors.<br>
>> +  Bam k((int*)(0));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: use nullptr<br>
>> +  // CHECK-FIXES: Bam k((int*)nullptr);<br>
>> +<br>
>> +  // Test for ambiguous overloaded operators.<br>
>> +  k = (int*)0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: use nullptr<br>
>> +  // CHECK-FIXES: k = (int*)nullptr;<br>
>> +}<br>
>> +<br>
>> +void test_const_pointers_abiguous() {<br>
>> +  const_ambiguous_function((int*)0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:34: warning: use nullptr<br>
>> +  // CHECK-FIXES: const_ambiguous_function((int*)nullptr);<br>
>> +}<br>
>> +<br>
>> +// Test where the implicit cast to null is surrounded by another implict cast<br>
>> +// with possible explict casts in-between.<br>
>> +void test_const_pointers() {<br>
>> +  const int *const_p1 = 0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr<br>
>> +  // CHECK-FIXES: const int *const_p1 = nullptr;<br>
>> +  const int *const_p2 = NULL;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr<br>
>> +  // CHECK-FIXES: const int *const_p2 = nullptr;<br>
>> +  const int *const_p3 = (int)0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr<br>
>> +  // CHECK-FIXES: const int *const_p3 = nullptr;<br>
>> +  const int *const_p4 = (int)0.0f;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: use nullptr<br>
>> +  // CHECK-FIXES: const int *const_p4 = nullptr;<br>
>> +  const int *const_p5 = (int*)0;<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:31: warning: use nullptr<br>
>> +  // CHECK-FIXES: const int *const_p5 = (int*)nullptr;<br>
>> +  int *t;<br>
>> +  const int *const_p6 = static_cast<int*>(t ? t : static_cast<int*>(0));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:69: warning: use nullptr<br>
>> + // CHECK-FIXES: const int *const_p6 = static_cast<int*>(t ? t :<br>
> static_cast<int*>(nullptr));<br>
>> +}<br>
>> +<br>
>> +<br>
>> +// FIXME: currently, the check doesn't work as it should with templates.<br>
>> +template<typename T><br>
>> +class A {<br>
>> + public:<br>
>> +  A(T *p = NULL) {}<br>
>> +<br>
>> +  void f() {<br>
>> +    Ptr = NULL;<br>
>> +  }<br>
>> +  T *Ptr;<br>
>> +};<br>
>> +<br>
>> +template<typename T><br>
>> +T *f2(T *a = NULL) {<br>
>> +  return a ? a : NULL;<br>
>> +}<br>
>><br>
>> Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp<br>
>> URL:<br>
> <a href="http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp?rev=245434&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp?rev=245434&view=auto</a><br>
>> ==============================================================================<br>
>> --- clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp (added)<br>
>> +++<br>
> clang-tools-extra/trunk/test/clang-tidy/modernize-use-nullptr.cpp Wed<br>
> Aug 19 08:13:12 2015<br>
>> @@ -0,0 +1,178 @@<br>
>> +// RUN: $(dirname %s)/check_clang_tidy.sh %s modernize-use-nullptr %t \<br>
>> +// RUN: -config="{CheckOptions: [{key:<br>
> modernize-use-nullptr.UserNullMacros, value: 'MY_NULL'}]}" \<br>
>> +// RUN:   -- -std=c++11<br>
>> +// REQUIRES: shell<br>
>> +<br>
>> +#define NULL 0<br>
>> +<br>
>> +namespace std {<br>
>> +<br>
>> +typedef decltype(nullptr) nullptr_t;<br>
>> +<br>
>> +} // namespace std<br>
>> +<br>
>> +// Just to make sure make_null() could have side effects.<br>
>> +void external();<br>
>> +<br>
>> +std::nullptr_t make_null() {<br>
>> +  external();<br>
>> +  return nullptr;<br>
>> +}<br>
>> +<br>
>> +void func() {<br>
>> +  void *CallTest = make_null();<br>
>> +<br>
>> +  int var = 1;<br>
>> +  void *CommaTest = (var+=2, make_null());<br>
>> +<br>
>> +  int *CastTest = static_cast<int*>(make_null());<br>
>> +}<br>
>> +<br>
>> +void dummy(int*) {}<br>
>> +void side_effect() {}<br>
>> +<br>
>> +#define MACRO_EXPANSION_HAS_NULL \<br>
>> +  void foo() { \<br>
>> +    dummy(0); \<br>
>> +    dummy(NULL); \<br>
>> +    side_effect(); \<br>
>> +  }<br>
>> +<br>
>> +MACRO_EXPANSION_HAS_NULL;<br>
>> +#undef MACRO_EXPANSION_HAS_NULL<br>
>> +<br>
>> +<br>
>> +void test_macro_expansion1() {<br>
>> +#define MACRO_EXPANSION_HAS_NULL \<br>
>> +  dummy(NULL); \<br>
>> +  side_effect();<br>
>> +<br>
>> +  MACRO_EXPANSION_HAS_NULL;<br>
>> +<br>
>> +#undef MACRO_EXPANSION_HAS_NULL<br>
>> +}<br>
>> +<br>
>> +// Test macro expansion with cast sequence, PR15572.<br>
>> +void test_macro_expansion2() {<br>
>> +#define MACRO_EXPANSION_HAS_NULL \<br>
>> +  dummy((int*)0); \<br>
>> +  side_effect();<br>
>> +<br>
>> +  MACRO_EXPANSION_HAS_NULL;<br>
>> +<br>
>> +#undef MACRO_EXPANSION_HAS_NULL<br>
>> +}<br>
>> +<br>
>> +void test_macro_expansion3() {<br>
>> +#define MACRO_EXPANSION_HAS_NULL \<br>
>> +  dummy(NULL); \<br>
>> +  side_effect();<br>
>> +<br>
>> +#define OUTER_MACRO \<br>
>> +  MACRO_EXPANSION_HAS_NULL; \<br>
>> +  side_effect();<br>
>> +<br>
>> +  OUTER_MACRO;<br>
>> +<br>
>> +#undef OUTER_MACRO<br>
>> +#undef MACRO_EXPANSION_HAS_NULL<br>
>> +}<br>
>> +<br>
>> +void test_macro_expansion4() {<br>
>> +#define MY_NULL NULL<br>
>> +  int *p = MY_NULL;<br>
>> + // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr<br>
> [modernize-use-nullptr]<br>
>> +  // CHECK-FIXES: int *p = nullptr;<br>
>> +#undef MY_NULL<br>
>> +}<br>
>> +<br>
>> +#define IS_EQ(x, y) if (x != y) return;<br>
>> +void test_macro_args() {<br>
>> +  int i = 0;<br>
>> +  int *Ptr;<br>
>> +<br>
>> +  IS_EQ(static_cast<int*>(0), Ptr);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: use nullptr<br>
>> +  // CHECK-FIXES: IS_EQ(static_cast<int*>(nullptr), Ptr);<br>
>> +<br>
>> +  IS_EQ(0, Ptr);    // literal<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr<br>
>> +  // CHECK-FIXES: IS_EQ(nullptr, Ptr);<br>
>> +<br>
>> +  IS_EQ(NULL, Ptr); // macro<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use nullptr<br>
>> +  // CHECK-FIXES: IS_EQ(nullptr, Ptr);<br>
>> +<br>
>> +  // These are ok since the null literal is not spelled within a macro.<br>
>> +#define myassert(x) if (!(x)) return;<br>
>> +  myassert(0 == Ptr);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr<br>
>> +  // CHECK-FIXES: myassert(nullptr == Ptr);<br>
>> +<br>
>> +  myassert(NULL == Ptr);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: use nullptr<br>
>> +  // CHECK-FIXES: myassert(nullptr == Ptr);<br>
>> +<br>
>> +  // These are bad as the null literal is buried in a macro.<br>
>> +#define BLAH(X) myassert(0 == (X));<br>
>> +#define BLAH2(X) myassert(NULL == (X));<br>
>> +  BLAH(Ptr);<br>
>> +  BLAH2(Ptr);<br>
>> +<br>
>> +  // Same as above but testing extra macro expansion.<br>
>> +#define EXPECT_NULL(X) IS_EQ(0, X);<br>
>> +#define EXPECT_NULL2(X) IS_EQ(NULL, X);<br>
>> +  EXPECT_NULL(Ptr);<br>
>> +  EXPECT_NULL2(Ptr);<br>
>> +<br>
>> +  // Almost the same as above but now null literal is not in a macro so ok<br>
>> +  // to transform.<br>
>> +#define EQUALS_PTR(X) IS_EQ(X, Ptr);<br>
>> +  EQUALS_PTR(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr<br>
>> +  // CHECK-FIXES: EQUALS_PTR(nullptr);<br>
>> +  EQUALS_PTR(NULL);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: use nullptr<br>
>> +  // CHECK-FIXES: EQUALS_PTR(nullptr);<br>
>> +<br>
>> +  // Same as above but testing extra macro expansion.<br>
>> +#define EQUALS_PTR_I(X) EQUALS_PTR(X)<br>
>> +  EQUALS_PTR_I(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr<br>
>> +  // CHECK-FIXES: EQUALS_PTR_I(nullptr);<br>
>> +  EQUALS_PTR_I(NULL);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: use nullptr<br>
>> +  // CHECK-FIXES: EQUALS_PTR_I(nullptr);<br>
>> +<br>
>> +  // Ok since null literal not within macro. However, now testing macro<br>
>> +  // used as arg to another macro.<br>
>> +#define decorate(EXPR) side_effect(); EXPR;<br>
>> +  decorate(IS_EQ(NULL, Ptr));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr<br>
>> +  // CHECK-FIXES: decorate(IS_EQ(nullptr, Ptr));<br>
>> +  decorate(IS_EQ(0, Ptr));<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: use nullptr<br>
>> +  // CHECK-FIXES: decorate(IS_EQ(nullptr, Ptr));<br>
>> +<br>
>> + // This macro causes a NullToPointer cast to happen where 0 is<br>
> assigned to z<br>
>> +  // but the 0 literal cannot be replaced because it is also used as an<br>
>> +  // integer in the comparison.<br>
>> +#define INT_AND_PTR_USE(X) do { int *z = X; if (X == 4) break; } while(false)<br>
>> +  INT_AND_PTR_USE(0);<br>
>> +<br>
>> +  // Both uses of X in this case result in NullToPointer casts so replacement<br>
>> +  // is possible.<br>
>> +#define PTR_AND_PTR_USE(X) do { int *z = X; if (X != z) break; } while(false)<br>
>> +  PTR_AND_PTR_USE(0);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use nullptr<br>
>> +  // CHECK-FIXES: PTR_AND_PTR_USE(nullptr);<br>
>> +  PTR_AND_PTR_USE(NULL);<br>
>> +  // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: use nullptr<br>
>> +  // CHECK-FIXES: PTR_AND_PTR_USE(nullptr);<br>
>> +<br>
>> +#define OPTIONAL_CODE(...) __VA_ARGS__<br>
>> +#define NOT_NULL dummy(0)<br>
>> +#define CALL(X) X<br>
>> +  OPTIONAL_CODE(NOT_NULL);<br>
>> +  CALL(NOT_NULL);<br>
>> +}<br>
>><br>
>><br>
>> _______________________________________________<br>
>> cfe-commits mailing list<br>
>> <a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
>> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</div></div></blockquote></div><br>
</div></div>