clang::tooling::Replacements
createInsertedReplacements(const std::vector<std::string> &Includes,
const std::vector<ClangMoveTool::MovedDecl> &Decls,
- llvm::StringRef FileName) {
+ llvm::StringRef FileName,
+ bool IsHeader = false) {
clang::tooling::Replacements InsertedReplacements;
+ std::string GuardName(FileName);
+ if (IsHeader) {
+ std::replace(GuardName.begin(), GuardName.end(), '/', '_');
+ std::replace(GuardName.begin(), GuardName.end(), '.', '_');
+ std::replace(GuardName.begin(), GuardName.end(), '-', '_');
+
+ GuardName = StringRef(GuardName).upper();
+ std::string HeaderGuard = "#ifndef " + GuardName + "\n";
+ HeaderGuard += "#define " + GuardName + "\n";
+ clang::tooling::Replacement HeaderGuardInclude(FileName, 0, 0,
+ HeaderGuard);
+ addOrMergeReplacement(HeaderGuardInclude, &InsertedReplacements);
+ }
// Add #Includes.
std::string AllIncludesString;
FileName, 0, 0, "} // namespace " + NS + "\n");
addOrMergeReplacement(InsertedReplacement, &InsertedReplacements);
}
+
+ if (IsHeader) {
+ clang::tooling::Replacement HeaderGuardEnd(FileName, 0, 0,
+ "#endif // " + GuardName + "\n");
+ addOrMergeReplacement(HeaderGuardEnd, &InsertedReplacements);
+ }
return InsertedReplacements;
}
if (!Spec.NewHeader.empty())
FileToReplacements[Spec.NewHeader] = createInsertedReplacements(
- HeaderIncludes, NewHeaderDecls, Spec.NewHeader);
+ HeaderIncludes, NewHeaderDecls, Spec.NewHeader, /*IsHeader=*/true);
if (!Spec.NewCC.empty())
FileToReplacements[Spec.NewCC] =
createInsertedReplacements(CCIncludes, NewCCDecls, Spec.NewCC);
// RUN: FileCheck -input-file=%T/clang-move/src/test.cpp -check-prefix=CHECK-OLD-TEST-CPP %s
// RUN: FileCheck -input-file=%T/clang-move/include/test.h %s -implicit-check-not='{{namespace.*}}'
//
+//
+// CHECK-NEW-TEST-H: #ifndef {{.*}}CLANG_MOVE_NEW_TEST_H
+// CHECK-NEW-TEST-H: #define {{.*}}CLANG_MOVE_NEW_TEST_H
// CHECK-NEW-TEST-H: namespace a {
// CHECK-NEW-TEST-H: class Foo {
// CHECK-NEW-TEST-H: public:
// CHECK-NEW-TEST-H: int f2(int a, int b);
// CHECK-NEW-TEST-H: };
// CHECK-NEW-TEST-H: } // namespace a
+// CHECK-NEW-TEST-H: #endif // {{.*}}CLANG_MOVE_NEW_TEST_H
//
// CHECK-NEW-TEST-CPP: #include "{{.*}}new_test.h"
// CHECK-NEW-TEST-CPP: #include "test2.h"
// CHECK-OLD-TEST-CPP: }
// CHECK-OLD-TEST-CPP: } // namespace c
+// CHECK-NEW-TEST-H: #ifndef {{.*}}NEW_MULTIPLE_CLASS_TEST_H
+// CHECK-NEW-TEST-H: #define {{.*}}NEW_MULTIPLE_CLASS_TEST_H
// CHECK-NEW-TEST-H: namespace a {
// CHECK-NEW-TEST-H: class Move1 {
// CHECK-NEW-TEST-H: public:
// CHECK-NEW-TEST-H: static int a;
// CHECK-NEW-TEST-H: };
// CHECK-NEW-TEST-H: } // namespace c
+// CHECK-NEW-TEST-H: #endif // {{.*}}NEW_MULTIPLE_CLASS_TEST_H
// CHECK-NEW-TEST-CPP: #include "{{.*}}new_multiple_class_test.h"
// CHECK-NEW-TEST-CPP: namespace a {