diff options
| author | Haojian Wu <hokein@google.com> | 2016-11-15 09:06:59 +0000 |
|---|---|---|
| committer | Haojian Wu <hokein@google.com> | 2016-11-15 09:06:59 +0000 |
| commit | 53315a7b98dd97f9ead880ecf84906b01e8958d2 (patch) | |
| tree | c35130e07437b56940f1245a7a684d816a9bdc51 /clang-tools-extra/clang-move/ClangMove.cpp | |
| parent | 8741656efdd0b4f5a4dd6d2f6b7ad8d6e4f09725 (diff) | |
| download | bcm5719-llvm-53315a7b98dd97f9ead880ecf84906b01e8958d2.tar.gz bcm5719-llvm-53315a7b98dd97f9ead880ecf84906b01e8958d2.zip | |
[clang-move] Make the output code look more pretty.
Summary:
Add decent blank lines between declarations:
* Add extra blank line after #define or #includes.
* Add extra blank line between declarations.
* Add extra blank line in front of #endif.
Previously, the new generated code is quite tight:
```
#ifndef FOO_H
#define FOO_H
namespace a {
class A { public: int f(); };
int A::f() { return 0; }
} // namespace a
#endif // FOO_H
```
After this patch, the code looks like:
```
#ifndef FOO_H
#define FOO_H
namespace a {
class A { public: int f(); };
int A::f() { return 0; }
} // namespace a
#endif // FOO_H
```
Reviewers: ioeric
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D26493
llvm-svn: 286943
Diffstat (limited to 'clang-tools-extra/clang-move/ClangMove.cpp')
| -rw-r--r-- | clang-tools-extra/clang-move/ClangMove.cpp | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/clang-tools-extra/clang-move/ClangMove.cpp b/clang-tools-extra/clang-move/ClangMove.cpp index 9b5bd03e496..c3613b44057 100644 --- a/clang-tools-extra/clang-move/ClangMove.cpp +++ b/clang-tools-extra/clang-move/ClangMove.cpp @@ -294,7 +294,7 @@ createInsertedReplacements(const std::vector<std::string> &Includes, } GuardName = StringRef(GuardName).upper(); NewCode += "#ifndef " + GuardName + "\n"; - NewCode += "#define " + GuardName + "\n"; + NewCode += "#define " + GuardName + "\n\n"; } // Add #Includes. @@ -306,11 +306,15 @@ createInsertedReplacements(const std::vector<std::string> &Includes, // Add moved class definition and its related declarations. All declarations // in same namespace are grouped together. + // + // Record namespaces where the current position is in. std::vector<std::string> CurrentNamespaces; for (const auto &MovedDecl : Decls) { + // The namespaces of the declaration being moved. std::vector<std::string> DeclNamespaces = GetNamespaces(MovedDecl.Decl); auto CurrentIt = CurrentNamespaces.begin(); auto DeclIt = DeclNamespaces.begin(); + // Skip the common prefix. while (CurrentIt != CurrentNamespaces.end() && DeclIt != DeclNamespaces.end()) { if (*CurrentIt != *DeclIt) @@ -318,19 +322,38 @@ createInsertedReplacements(const std::vector<std::string> &Includes, ++CurrentIt; ++DeclIt; } + // Calculate the new namespaces after adding MovedDecl in CurrentNamespace, + // which is used for next iteration of this loop. std::vector<std::string> NextNamespaces(CurrentNamespaces.begin(), CurrentIt); NextNamespaces.insert(NextNamespaces.end(), DeclIt, DeclNamespaces.end()); + + + // End with CurrentNamespace. + bool HasEndCurrentNamespace = false; auto RemainingSize = CurrentNamespaces.end() - CurrentIt; for (auto It = CurrentNamespaces.rbegin(); RemainingSize > 0; --RemainingSize, ++It) { assert(It < CurrentNamespaces.rend()); NewCode += "} // namespace " + *It + "\n"; + HasEndCurrentNamespace = true; } + // Add trailing '\n' after the nested namespace definition. + if (HasEndCurrentNamespace) + NewCode += "\n"; + + // If the moved declaration is not in CurrentNamespace, add extra namespace + // definitions. + bool IsInNewNamespace = false; while (DeclIt != DeclNamespaces.end()) { NewCode += "namespace " + *DeclIt + " {\n"; + IsInNewNamespace = true; ++DeclIt; } + // If the moved declaration is in same namespace CurrentNamespace, add + // a preceeding `\n' before the moved declaration. + if (!IsInNewNamespace) + NewCode += "\n"; NewCode += getDeclarationSourceText(MovedDecl.Decl, MovedDecl.SM); CurrentNamespaces = std::move(NextNamespaces); } @@ -339,7 +362,7 @@ createInsertedReplacements(const std::vector<std::string> &Includes, NewCode += "} // namespace " + NS + "\n"; if (IsHeader) - NewCode += "#endif // " + GuardName + "\n"; + NewCode += "\n#endif // " + GuardName + "\n"; return clang::tooling::Replacements( clang::tooling::Replacement(FileName, 0, 0, NewCode)); } |

