summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-12-01 17:25:55 +0000
committerEric Liu <ioeric@google.com>2016-12-01 17:25:55 +0000
commitc265b02bec973ab3bea9311e271d32e9938f36e9 (patch)
treec1acdf911d102449b742683e16379135f20eeaf8 /clang-tools-extra
parentbcf23661d08a6f77d873d2b985b34cc70c9d39e7 (diff)
downloadbcm5719-llvm-c265b02bec973ab3bea9311e271d32e9938f36e9.tar.gz
bcm5719-llvm-c265b02bec973ab3bea9311e271d32e9938f36e9.zip
[change-namespace] don't generate replacements for files that don't match file pattern.
Reviewers: hokein Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D27302 llvm-svn: 288376
Diffstat (limited to 'clang-tools-extra')
-rw-r--r--clang-tools-extra/change-namespace/ChangeNamespace.cpp17
-rw-r--r--clang-tools-extra/change-namespace/ChangeNamespace.h2
-rw-r--r--clang-tools-extra/test/change-namespace/macro.cpp29
3 files changed, 42 insertions, 6 deletions
diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.cpp b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
index 44eec1a4dba..7de4690ac11 100644
--- a/clang-tools-extra/change-namespace/ChangeNamespace.cpp
+++ b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
@@ -41,7 +41,7 @@ SourceLocation startLocationForType(TypeLoc TLoc) {
return TLoc.getLocStart();
}
-SourceLocation EndLocationForType(TypeLoc TLoc) {
+SourceLocation endLocationForType(TypeLoc TLoc) {
// Dig past any namespace or keyword qualifications.
while (TLoc.getTypeLocClass() == TypeLoc::Elaborated ||
TLoc.getTypeLocClass() == TypeLoc::Qualified)
@@ -249,7 +249,7 @@ ChangeNamespaceTool::ChangeNamespaceTool(
llvm::StringRef FallbackStyle)
: FallbackStyle(FallbackStyle), FileToReplacements(*FileToReplacements),
OldNamespace(OldNs.ltrim(':')), NewNamespace(NewNs.ltrim(':')),
- FilePattern(FilePattern) {
+ FilePattern(FilePattern), FilePatternRE(FilePattern) {
FileToReplacements->clear();
llvm::SmallVector<llvm::StringRef, 4> OldNsSplitted;
llvm::SmallVector<llvm::StringRef, 4> NewNsSplitted;
@@ -407,7 +407,7 @@ void ChangeNamespaceTool::run(
Result.Nodes.getNodeAs<NestedNameSpecifierLoc>(
"nested_specifier_loc")) {
SourceLocation Start = Specifier->getBeginLoc();
- SourceLocation End = EndLocationForType(Specifier->getTypeLoc());
+ SourceLocation End = endLocationForType(Specifier->getTypeLoc());
fixTypeLoc(Result, Start, End, Specifier->getTypeLoc());
} else if (const auto *BaseInitializer =
Result.Nodes.getNodeAs<CXXCtorInitializer>(
@@ -415,7 +415,7 @@ void ChangeNamespaceTool::run(
BaseCtorInitializerTypeLocs.push_back(
BaseInitializer->getTypeSourceInfo()->getTypeLoc());
} else if (const auto *TLoc = Result.Nodes.getNodeAs<TypeLoc>("type")) {
- fixTypeLoc(Result, startLocationForType(*TLoc), EndLocationForType(*TLoc),
+ fixTypeLoc(Result, startLocationForType(*TLoc), endLocationForType(*TLoc),
*TLoc);
} else if (const auto *VarRef =
Result.Nodes.getNodeAs<DeclRefExpr>("var_ref")) {
@@ -667,8 +667,7 @@ void ChangeNamespaceTool::fixTypeLoc(
return false;
llvm::StringRef Filename =
Result.SourceManager->getFilename(ExpansionLoc);
- llvm::Regex RE(FilePattern);
- return RE.match(Filename);
+ return FilePatternRE.match(Filename);
};
// Don't fix the \p Type if it refers to a type alias decl in the moved
// namespace since the alias decl will be moved along with the type
@@ -779,6 +778,12 @@ void ChangeNamespaceTool::onEndOfTranslationUnit() {
}
FileToReplacements[FilePath] = *CleanReplacements;
}
+
+ // Make sure we don't generate replacements for files that do not match
+ // FilePattern.
+ for (auto &Entry : FileToReplacements)
+ if (!FilePatternRE.match(Entry.first))
+ Entry.second.clear();
}
} // namespace change_namespace
diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.h b/clang-tools-extra/change-namespace/ChangeNamespace.h
index e7521cb8760..300579d943b 100644
--- a/clang-tools-extra/change-namespace/ChangeNamespace.h
+++ b/clang-tools-extra/change-namespace/ChangeNamespace.h
@@ -13,6 +13,7 @@
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Format/Format.h"
#include "clang/Tooling/Core/Replacement.h"
+#include "llvm/Support/Regex.h"
#include <string>
namespace clang {
@@ -131,6 +132,7 @@ private:
std::string DiffNewNamespace;
// A regex pattern that matches files to be processed.
std::string FilePattern;
+ llvm::Regex FilePatternRE;
// Information about moved namespaces grouped by file.
// Since we are modifying code in old namespaces (e.g. add namespace
// spedifiers) as well as moving them, we store information about namespaces
diff --git a/clang-tools-extra/test/change-namespace/macro.cpp b/clang-tools-extra/test/change-namespace/macro.cpp
new file mode 100644
index 00000000000..ba47de603da
--- /dev/null
+++ b/clang-tools-extra/test/change-namespace/macro.cpp
@@ -0,0 +1,29 @@
+// RUN: cp %S/macro.cpp %T/macro.cpp
+// RUN: echo "#define USING using na::nc::X" > %T/macro.h
+//
+// RUN: clang-change-namespace -old_namespace "na::nb" -new_namespace "x::y" --file_pattern "macro.cpp" --i %T/macro.cpp --
+// RUN: FileCheck -input-file=%T/macro.cpp -check-prefix=CHECK-CC %s
+// RUN: FileCheck -input-file=%T/macro.h -check-prefix=CHECK-HEADER %s
+//
+// RUN: cp %S/macro.cpp %T/macro.cpp
+// RUN: echo "#define USING using na::nc::X" > %T/macro.h
+// RUN: clang-change-namespace -old_namespace "na::nb" -new_namespace "x::y" --file_pattern ".*" --i %T/macro.cpp --
+// RUN: FileCheck -input-file=%T/macro.cpp -check-prefix=CHECK-CC %s
+// RUN: FileCheck -input-file=%T/macro.h -check-prefix=CHECK-CHANGED-HEADER %s
+#include "macro.h"
+namespace na { namespace nc { class X{}; } }
+
+namespace na {
+namespace nb {
+USING;
+}
+}
+// CHECK-CC: namespace x {
+// CHECK-CC: namespace y {
+// CHECK-CC: USING;
+// CHECK-CC: } // namespace y
+// CHECK-CC: } // namespace x
+
+// CHECK-HEADER: #define USING using na::nc::X
+
+// CHECK-CHANGED-HEADER: #define USING using ::na::nc::X
OpenPOWER on IntegriCloud