namespace {
+const char *NullMacroName = "NULL";
+
+static llvm::cl::opt<std::string> UserNullMacroNames(
+ "user-null-macros", llvm::cl::desc("Comma-separated list of user-defined "
+ "macro names that behave like NULL"),
+ llvm::cl::init(""));
+
/// \brief Replaces the provided range with the text "nullptr", but only if
/// the start and end location are both in main file.
/// Returns true if and only if a replacement was made.
return false;
}
+/// \brief Returns the name of the outermost macro.
+///
+/// Given
+/// \code
+/// #define MY_NULL NULL
+/// \endcode
+/// If \p Loc points to NULL, this function will return the name MY_NULL.
+llvm::StringRef GetOutermostMacroName(
+ SourceLocation Loc, const SourceManager &SM, const LangOptions &LO) {
+ assert(Loc.isMacroID());
+ SourceLocation OutermostMacroLoc;
+
+ while (Loc.isMacroID()) {
+ OutermostMacroLoc = Loc;
+ Loc = SM.getImmediateMacroCallerLoc(Loc);
+ }
+
+ return clang::Lexer::getImmediateMacroName(OutermostMacroLoc, SM, LO);
+}
}
/// \brief Looks for a sequences of 0 or more explicit casts with an implicit
Expr *FirstSubExpr;
};
+NullptrFixer::NullptrFixer(clang::tooling::Replacements &Replace,
+ unsigned &AcceptedChanges, RiskLevel)
+ : Replace(Replace), AcceptedChanges(AcceptedChanges) {
+ if (!UserNullMacroNames.empty()) {
+ llvm::StringRef S = UserNullMacroNames;
+ S.split(UserNullMacros, ",");
+ }
+ UserNullMacros.insert(UserNullMacros.begin(), llvm::StringRef(NullMacroName));
+}
+
void NullptrFixer::run(const ast_matchers::MatchFinder::MatchResult &Result) {
SourceManager &SM = *Result.SourceManager;
EndLoc = SM.getFileLoc(EndLoc);
} else if (SM.isMacroBodyExpansion(StartLoc) &&
SM.isMacroBodyExpansion(EndLoc)) {
- llvm::StringRef ImmediateMacroName = clang::Lexer::getImmediateMacroName(
- StartLoc, SM, Result.Context->getLangOpts());
- if (ImmediateMacroName != "NULL")
- return;
+ llvm::StringRef OutermostMacroName =
+ GetOutermostMacroName(StartLoc, SM, Result.Context->getLangOpts());
- SourceLocation MacroCallerStartLoc =
- SM.getImmediateMacroCallerLoc(StartLoc);
- SourceLocation MacroCallerEndLoc = SM.getImmediateMacroCallerLoc(EndLoc);
+ // Check to see if the user wants to replace the macro being expanded.
+ bool ReplaceNullMacro =
+ std::find(UserNullMacros.begin(), UserNullMacros.end(),
+ OutermostMacroName) != UserNullMacros.end();
- if (MacroCallerStartLoc.isFileID() && MacroCallerEndLoc.isFileID()) {
- StartLoc = SM.getFileLoc(StartLoc);
- EndLoc = SM.getFileLoc(EndLoc);
- } else
+ if (!ReplaceNullMacro)
return;
+
+ StartLoc = SM.getFileLoc(StartLoc);
+ EndLoc = SM.getFileLoc(EndLoc);
}
AcceptedChanges +=
Makes use of the new C++11 keyword ``nullptr`` where possible.
See :doc:`UseNullptrTransform`.
+.. option:: -user-null-macros=<string>
+
+ ``<string>`` is a comma-separated list of user-defined macros that behave like
+ the ``NULL`` macro. The :option:`-use-nullptr` transform will replace these
+ macros along with ``NULL``. See :doc:`UseNullptrTransform`.
+
.. option:: -use-auto
Replace the type specifier of variable declarations with the ``auto`` type
:ref:`transform documentation <transforms>` for details.
.. option:: -final-syntax-check
-
+
After applying the final transform to a file, parse the file to ensure the
last transform did not introduce syntax errors. Syntax errors introduced by
earlier transforms are already caught when subsequent transforms parse the
// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
// RUN: cpp11-migrate -use-nullptr %t.cpp -- -I %S
// RUN: FileCheck -input-file=%t.cpp %s
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t2.cpp
+// RUN: cpp11-migrate -use-nullptr -user-null-macros=MY_NULL %t2.cpp -- -I %S
+// RUN: FileCheck -check-prefix=USER-SUPPLIED-NULL -input-file=%t2.cpp %s
#define NULL 0
// CHECK: #define NULL 0
void test_macro_expansion3() {
#define MY_NULL NULL
- // TODO: Eventually we should fix the transform to detect cases like this so
- // that we can replace MY_NULL with nullptr.
int *p = MY_NULL;
// CHECK: int *p = MY_NULL;
+ // USER-SUPPLIED-NULL: int *p = nullptr;
#undef MY_NULL
}