mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Insert fn for void functions (#596)
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user