summaryrefslogtreecommitdiff
path: root/clang-tools-extra/change-namespace
diff options
context:
space:
mode:
authorEric Liu <ioeric@google.com>2016-11-10 18:29:01 +0000
committerEric Liu <ioeric@google.com>2016-11-10 18:29:01 +0000
commit46f669bad28b47393b247e3782b40113073e01cf (patch)
tree1102a11167b5bed6d6f7fdd57f8aa92417a1f651 /clang-tools-extra/change-namespace
parent408e6da2bcf7224a1b49d2ede09f5860a66208b7 (diff)
Handle adding new nested namespace in old namespace.
Reviewers: hokein Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D26456
Diffstat (limited to 'clang-tools-extra/change-namespace')
-rw-r--r--clang-tools-extra/change-namespace/ChangeNamespace.cpp29
1 files changed, 18 insertions, 11 deletions
diff --git a/clang-tools-extra/change-namespace/ChangeNamespace.cpp b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
index 81fa6073e3e..a400a37773b 100644
--- a/clang-tools-extra/change-namespace/ChangeNamespace.cpp
+++ b/clang-tools-extra/change-namespace/ChangeNamespace.cpp
@@ -57,16 +57,19 @@ SourceLocation EndLocationForType(TypeLoc TLoc) {
}
// Returns the containing namespace of `InnerNs` by skipping `PartialNsName`.
-// If the `InnerNs` does not have `PartialNsName` as suffix, nullptr is
-// returned.
+// If the `InnerNs` does not have `PartialNsName` as suffix, or `PartialNsName`
+// is empty, nullptr is returned.
// For example, if `InnerNs` is "a::b::c" and `PartialNsName` is "b::c", then
// the NamespaceDecl of namespace "a" will be returned.
const NamespaceDecl *getOuterNamespace(const NamespaceDecl *InnerNs,
llvm::StringRef PartialNsName) {
+ if (!InnerNs || PartialNsName.empty())
+ return nullptr;
const auto *CurrentContext = llvm::cast<DeclContext>(InnerNs);
const auto *CurrentNs = InnerNs;
llvm::SmallVector<llvm::StringRef, 4> PartialNsNameSplitted;
- PartialNsName.split(PartialNsNameSplitted, "::");
+ PartialNsName.split(PartialNsNameSplitted, "::", /*MaxSplit=*/-1,
+ /*KeepEmpty=*/false);
while (!PartialNsNameSplitted.empty()) {
// Get the inner-most namespace in CurrentContext.
while (CurrentContext && !llvm::isa<NamespaceDecl>(CurrentContext))
@@ -468,16 +471,20 @@ void ChangeNamespaceTool::moveOldNamespace(
// "x::y" will be inserted inside the existing namespace "a" and after "a::b".
// `OuterNs` is the first namespace in `DiffOldNamespace`, e.g. "namespace b"
// in the above example.
- // FIXME: consider the case where DiffOldNamespace is empty.
+ // If there is no outer namespace (i.e. DiffOldNamespace is empty), the new
+ // namespace will be a nested namespace in the old namespace.
const NamespaceDecl *OuterNs = getOuterNamespace(NsDecl, DiffOldNamespace);
- SourceLocation LocAfterNs =
- getStartOfNextLine(OuterNs->getRBraceLoc(), *Result.SourceManager,
- Result.Context->getLangOpts());
- assert(LocAfterNs.isValid() &&
- "Failed to get location after DiffOldNamespace");
+ SourceLocation InsertionLoc = Start;
+ if (OuterNs) {
+ SourceLocation LocAfterNs =
+ getStartOfNextLine(OuterNs->getRBraceLoc(), *Result.SourceManager,
+ Result.Context->getLangOpts());
+ assert(LocAfterNs.isValid() &&
+ "Failed to get location after DiffOldNamespace");
+ InsertionLoc = LocAfterNs;
+ }
MoveNs.InsertionOffset = Result.SourceManager->getFileOffset(
- Result.SourceManager->getSpellingLoc(LocAfterNs));
-
+ Result.SourceManager->getSpellingLoc(InsertionLoc));
MoveNs.FID = Result.SourceManager->getFileID(Start);
MoveNs.SourceMgr = Result.SourceManager;
MoveNamespaces[R.getFilePath()].push_back(MoveNs);