rewriter_test.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "migrate_cpp/rewriter.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "clang/AST/ASTConsumer.h"
  8. #include "clang/Frontend/CompilerInstance.h"
  9. #include "clang/Frontend/FrontendAction.h"
  10. #include "clang/Tooling/Tooling.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. // Represents C++ source code with at most one region enclosed in $[[...]]$ as
  14. // an annotated range.
  15. class Annotations {
  16. public:
  17. Annotations(llvm::StringRef annotated_source) {
  18. size_t index = annotated_source.find("$[[");
  19. if (index == llvm::StringRef::npos) {
  20. source_code_ = std::string(annotated_source);
  21. return;
  22. }
  23. start_ = index;
  24. end_ = annotated_source.find("]]$", index);
  25. CARBON_CHECK(end_ != llvm::StringRef::npos)
  26. << "Found `$[[` but no matching `]]$`";
  27. source_code_ = (llvm::Twine(annotated_source.substr(0, start_)) +
  28. annotated_source.substr(start_ + 3, end_ - start_ - 3) +
  29. annotated_source.substr(end_ + 3))
  30. .str();
  31. // Update `end_` so that it is relative to the unannotated source (which
  32. // means three characters earlier due to the `$[[` being removed.
  33. end_ -= 3;
  34. }
  35. // Returns a view into the unannotated source.
  36. llvm::StringRef source() const { return source_code_; }
  37. // Returns the offsets in the file representing the annotated range if they
  38. // exist and `{0, std::numeric_limits<size_t>::max()}` otherwise.
  39. std::pair<size_t, size_t> range() const { return std::pair(start_, end_); }
  40. private:
  41. std::string source_code_;
  42. size_t start_ = 0;
  43. size_t end_ = std::numeric_limits<size_t>::max();
  44. };
  45. // Rewrites the `cpp_code`, return the Carbon equivalent. If the text has no
  46. // source range annotated with $[[...]]$, the entire translation unit will be
  47. // migrated and output. Otherwise, only the migrated output corresponding to the
  48. // annotated range will be be output. No more than one range may be annoated at
  49. // all.
  50. //
  51. // This annotation mechanism is useful in that it allows us to specifically test
  52. // the migration associated with specific nodes even when they require some
  53. // additional context that we do not wish to be covered by the test.
  54. auto RewriteText(llvm::StringRef cpp_code) -> std::string {
  55. std::string result;
  56. Annotations annotated_cpp_code(cpp_code);
  57. bool success = clang::tooling::runToolOnCodeWithArgs(
  58. std::make_unique<MigrationAction>(result, annotated_cpp_code.range()),
  59. annotated_cpp_code.source(), {}, "test.cc", "clang-tool",
  60. std::make_shared<clang::PCHContainerOperations>(),
  61. clang::tooling::FileContentMappings());
  62. return success ? result : "";
  63. }
  64. TEST(Rewriter, BoolLiteral) {
  65. EXPECT_EQ(RewriteText("bool x = $[[true]]$;"), "true");
  66. EXPECT_EQ(RewriteText("bool x = $[[false]]$;"), "false");
  67. }
  68. TEST(Rewriter, IntegerLiteral) {
  69. EXPECT_EQ(RewriteText("int x = $[[0]]$;"), "0");
  70. EXPECT_EQ(RewriteText("int x = $[[1]]$;"), "1");
  71. EXPECT_EQ(RewriteText("int x = $[[1234]]$;"), "1234");
  72. EXPECT_EQ(RewriteText("int x = $[[12'34]]$;"), "12_34");
  73. EXPECT_EQ(RewriteText("int x = $[[12'3'4]]$;"), "12_3_4");
  74. }
  75. TEST(Rewriter, SingleDeclaration) {
  76. EXPECT_EQ(RewriteText("bool b;"), "var b: bool;\n");
  77. EXPECT_EQ(RewriteText("int i;"), "var i: i32;\n");
  78. EXPECT_EQ(RewriteText("const bool b = false;"), "let b: bool = false;\n");
  79. EXPECT_EQ(RewriteText("const int i = 17;"), "let i: i32 = 17;\n");
  80. EXPECT_EQ(RewriteText("bool const b = false;"), "let b: bool = false;\n");
  81. EXPECT_EQ(RewriteText("int const i = 1234;"), "let i: i32 = 1234;\n");
  82. }
  83. TEST(Rewriter, Pointers) {
  84. // TODO: Add tests for pointers-to-const when the syntax is nailed down.
  85. EXPECT_EQ(RewriteText("bool b;\n"
  86. "$[[bool *p = &b]]$;"),
  87. "var p: bool* = &b");
  88. EXPECT_EQ(RewriteText("bool b;\n"
  89. "$[[bool * const p = &b]]$;"),
  90. "let p: bool* = &b");
  91. // Pointers and non-pointers on the same DeclStmt.
  92. EXPECT_EQ(RewriteText("bool b, *p;\n"),
  93. "var b: bool;\n"
  94. "var p: bool*;\n");
  95. EXPECT_EQ(RewriteText("bool b, *p = &b;\n"),
  96. "var b: bool;\n"
  97. "var p: bool* = &b;\n");
  98. }
  99. TEST(Rewriter, DeclarationComma) {
  100. EXPECT_EQ(RewriteText("int x, y;"),
  101. "var x: i32;\n"
  102. "var y: i32;\n");
  103. EXPECT_EQ(RewriteText("int x = 7, y;"),
  104. "var x: i32 = 7;\n"
  105. "var y: i32;\n");
  106. EXPECT_EQ(RewriteText("const int x = 1, y = 2;"),
  107. "let x: i32 = 1;\n"
  108. "let y: i32 = 2;\n");
  109. EXPECT_EQ(RewriteText("int const x = 1234, y = 5678;"),
  110. "let x: i32 = 1234;\n"
  111. "let y: i32 = 5678;\n");
  112. }
  113. TEST(Rewriter, FunctionDeclaration) {
  114. // Function declarations and definitions returning void.
  115. EXPECT_EQ(RewriteText("void f();"), "fn f() -> ();\n");
  116. EXPECT_EQ(RewriteText("void f() {}"),
  117. "fn f() -> () {\n"
  118. "}\n");
  119. // Function declarations and definitions returning int.
  120. EXPECT_EQ(RewriteText("int f();"), "fn f() -> i32;\n");
  121. EXPECT_EQ(RewriteText("int f() { return 0; }"),
  122. "fn f() -> i32 {\n"
  123. "return 0;\n"
  124. "}\n");
  125. // Function declarations and definitions with a single parameter.
  126. EXPECT_EQ(RewriteText("int f(bool);"), "fn f(_: bool) -> i32;\n");
  127. EXPECT_EQ(RewriteText("int f(bool b);"), "fn f(b: bool) -> i32;\n");
  128. EXPECT_EQ(RewriteText("int f(bool) { return 0; }"),
  129. "fn f(_: bool) -> i32 {\n"
  130. "return 0;\n"
  131. "}\n");
  132. EXPECT_EQ(RewriteText("int f(bool b) { return 0; }"),
  133. "fn f(b: bool) -> i32 {\n"
  134. "return 0;\n"
  135. "}\n");
  136. // Function declarations and definitions with a multiple parameters.
  137. EXPECT_EQ(RewriteText("int f(bool, int);"),
  138. "fn f(_: bool, _: i32) -> i32;\n");
  139. EXPECT_EQ(RewriteText("int f(bool b, int n);"),
  140. "fn f(b: bool, n: i32) -> i32;\n");
  141. EXPECT_EQ(RewriteText("int f(bool, int n) { return 0; }"),
  142. "fn f(_: bool, n: i32) -> i32 {\n"
  143. "return 0;\n"
  144. "}\n");
  145. EXPECT_EQ(RewriteText("int f(bool b, int n) { return 0; }"),
  146. "fn f(b: bool, n: i32) -> i32 {\n"
  147. "return 0;\n"
  148. "}\n");
  149. EXPECT_EQ(RewriteText("int f(bool b, int n = 3) { return n; }"),
  150. "fn f(b: bool, n: i32 = 3) -> i32 {\n"
  151. "return n;\n"
  152. "}\n");
  153. // Function declarations with trailing-return syntax.
  154. EXPECT_EQ(RewriteText("auto f(bool b, int n = 3) -> int;"),
  155. "fn f(b: bool, n: i32 = 3) -> i32;\n");
  156. EXPECT_EQ(RewriteText("auto f(bool b, int n = 3) -> int { return n; }"),
  157. "fn f(b: bool, n: i32 = 3) -> i32 {\n"
  158. "return n;\n"
  159. "}\n");
  160. }
  161. } // namespace
  162. } // namespace Carbon::Testing