Insert fn for void functions (#596)

This commit is contained in:
Jon Meow
2021-06-24 10:16:21 -07:00
committed by GitHub
parent 2344a52e24
commit 9bdabda851
14 changed files with 50 additions and 35 deletions
+7 -2
View File
@@ -13,8 +13,13 @@ namespace Carbon {
FnInserter::FnInserter(std::map<std::string, Replacements>& in_replacements,
cam::MatchFinder* finder)
: Matcher(in_replacements) {
finder->addMatcher(cam::functionDecl(cam::hasTrailingReturn()).bind(Label),
this);
finder->addMatcher(
cam::functionDecl(cam::anyOf(cam::hasTrailingReturn(),
cam::returns(cam::asString("void"))),
cam::unless(cam::anyOf(cam::cxxConstructorDecl(),
cam::cxxDestructorDecl())))
.bind(Label),
this);
}
void FnInserter::run(const cam::MatchFinder::MatchResult& result) {
@@ -30,13 +30,12 @@ TEST_F(FnInserterTest, Inline) {
}
TEST_F(FnInserterTest, Void) {
// TODO: void needs to be handled.
constexpr char Before[] = "void A();";
ExpectReplacement(Before, Before);
constexpr char After[] = "fn A();";
ExpectReplacement(Before, After);
}
TEST_F(FnInserterTest, Methods) {
// TODO: void needs to be handled.
// TODO: Need to re-lex tokens, this should probably be "fn virtual" for now.
constexpr char Before[] = R"cpp(
class Shape {
@@ -58,13 +57,13 @@ TEST_F(FnInserterTest, Methods) {
constexpr char After[] = R"(
class Shape {
public:
virtual void Draw() = 0;
fn void Draw() = 0;
fn auto NumSides() -> int = 0;
};
class Circle : public Shape {
public:
void Draw() override;
fn Draw() override;
fn NumSides() -> int override;
fn Radius() -> double { return radius_; }
@@ -75,6 +74,17 @@ TEST_F(FnInserterTest, Methods) {
ExpectReplacement(Before, After);
}
TEST_F(FnInserterTest, ConstructorDestructor) {
constexpr char Before[] = R"cpp(
class Shape {
public:
Shape() {}
~Shape() {}
};
)cpp";
ExpectReplacement(Before, Before);
}
TEST_F(FnInserterTest, LegacyReturn) {
// Code should be migrated to trailing returns by clang-tidy, so this is okay
// to miss.