Index: clang-tools-extra/trunk/change-namespace/ChangeNamespace.h =================================================================== --- clang-tools-extra/trunk/change-namespace/ChangeNamespace.h +++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.h @@ -50,6 +50,7 @@ // files matching `FilePattern`. ChangeNamespaceTool( llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern, + llvm::ArrayRef WhiteListedSymbolPatterns, std::map *FileToReplacements, llvm::StringRef FallbackStyle = "LLVM"); @@ -164,6 +165,9 @@ // CallExpr and one as DeclRefExpr), we record all DeclRefExpr's that have // been processed so that we don't handle them twice. llvm::SmallPtrSet ProcessedFuncRefs; + // Patterns of symbol names whose references are not expected to be updated + // when changing namespaces around them. + std::vector WhiteListedSymbolRegexes; }; } // namespace change_namespace Index: clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp =================================================================== --- clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp +++ clang-tools-extra/trunk/change-namespace/ChangeNamespace.cpp @@ -290,6 +290,7 @@ ChangeNamespaceTool::ChangeNamespaceTool( llvm::StringRef OldNs, llvm::StringRef NewNs, llvm::StringRef FilePattern, + llvm::ArrayRef WhiteListedSymbolPatterns, std::map *FileToReplacements, llvm::StringRef FallbackStyle) : FallbackStyle(FallbackStyle), FileToReplacements(*FileToReplacements), @@ -308,6 +309,9 @@ } DiffOldNamespace = joinNamespaces(OldNsSplitted); DiffNewNamespace = joinNamespaces(NewNsSplitted); + + for (const auto &Pattern : WhiteListedSymbolPatterns) + WhiteListedSymbolRegexes.emplace_back(Pattern); } void ChangeNamespaceTool::registerMatchers(ast_matchers::MatchFinder *Finder) { @@ -736,6 +740,9 @@ Result.SourceManager->getSpellingLoc(End)), *Result.SourceManager, Result.Context->getLangOpts()); std::string FromDeclName = FromDecl->getQualifiedNameAsString(); + for (llvm::Regex &RE : WhiteListedSymbolRegexes) + if (RE.match(FromDeclName)) + return; std::string ReplaceName = getShortestQualifiedNameInNamespace(FromDeclName, NewNs); // Checks if there is any using namespace declarations that can shorten the Index: clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp =================================================================== --- clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp +++ clang-tools-extra/trunk/change-namespace/tool/ClangChangeNamespace.cpp @@ -73,6 +73,25 @@ cl::desc("The style name used for reformatting."), cl::init("LLVM"), cl::cat(ChangeNamespaceCategory)); +cl::opt WhiteListFile( + "whitelist_file", + cl::desc("A file containing regexes of symbol names that are not expected " + "to be updated when changing namespaces around them."), + cl::init(""), cl::cat(ChangeNamespaceCategory)); + +llvm::ErrorOr> GetWhiteListedSymbolPatterns() { + llvm::SmallVector Lines; + if (!WhiteListFile.empty()) { + llvm::ErrorOr> File = + llvm::MemoryBuffer::getFile(WhiteListFile); + if (!File) + return File.getError(); + llvm::StringRef Content = File.get()->getBuffer(); + Content.split(Lines, '\n', /*MaxSplit=*/-1, /*KeepEmpty=*/false); + } + return std::vector(Lines.begin(), Lines.end()); +} + } // anonymous namespace int main(int argc, const char **argv) { @@ -81,8 +100,16 @@ ChangeNamespaceCategory); const auto &Files = OptionsParser.getSourcePathList(); tooling::RefactoringTool Tool(OptionsParser.getCompilations(), Files); + llvm::ErrorOr> WhiteListPatterns = + GetWhiteListedSymbolPatterns(); + if (!WhiteListPatterns) { + llvm::errs() << "Failed to open whitelist file " << WhiteListFile << ". " + << WhiteListPatterns.getError().message() << "\n"; + return 1; + } change_namespace::ChangeNamespaceTool NamespaceTool( - OldNamespace, NewNamespace, FilePattern, &Tool.getReplacements(), Style); + OldNamespace, NewNamespace, FilePattern, *WhiteListPatterns, + &Tool.getReplacements(), Style); ast_matchers::MatchFinder Finder; NamespaceTool.registerMatchers(&Finder); std::unique_ptr Factory = Index: clang-tools-extra/trunk/test/change-namespace/Inputs/fake-std.h =================================================================== --- clang-tools-extra/trunk/test/change-namespace/Inputs/fake-std.h +++ clang-tools-extra/trunk/test/change-namespace/Inputs/fake-std.h @@ -0,0 +1,5 @@ +namespace std { + class STD {}; +} + +using namespace std; Index: clang-tools-extra/trunk/test/change-namespace/white-list.cpp =================================================================== --- clang-tools-extra/trunk/test/change-namespace/white-list.cpp +++ clang-tools-extra/trunk/test/change-namespace/white-list.cpp @@ -0,0 +1,19 @@ +// RUN: echo "^std::.*$" > %T/white-list.txt +// RUN: clang-change-namespace -old_namespace "na::nb" -new_namespace "x::y" --file_pattern ".*" --whitelist_file %T/white-list.txt %s -- | sed 's,// CHECK.*,,' | FileCheck %s + +#include "Inputs/fake-std.h" + +// CHECK: namespace x { +// CHECK-NEXT: namespace y { +namespace na { +namespace nb { +void f() { + std::STD x1; + STD x2; +// CHECK: {{^}} std::STD x1;{{$}} +// CHECK-NEXT: {{^}} STD x2;{{$}} +} +// CHECK: } // namespace y +// CHECK-NEXT: } // namespace x +} +} Index: clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp =================================================================== --- clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp +++ clang-tools-extra/trunk/unittests/change-namespace/ChangeNamespaceTests.cpp @@ -38,7 +38,8 @@ std::map FileToReplacements; change_namespace::ChangeNamespaceTool NamespaceTool( - OldNamespace, NewNamespace, FilePattern, &FileToReplacements); + OldNamespace, NewNamespace, FilePattern, + /*WhiteListedSymbolPatterns*/ {}, &FileToReplacements); ast_matchers::MatchFinder Finder; NamespaceTool.registerMatchers(&Finder); std::unique_ptr Factory =