Skip to content

Commit b6d23f2

Browse files
committed
[ASTMatchers] Force c++ unittests to specify correct language standard
Force the unittests on c++ code for matchers to specify the correct standard. Reviewed By: gribozavr2 Differential Revision: https://reviews.llvm.org/D80884
1 parent 11efb08 commit b6d23f2

File tree

2 files changed

+145
-100
lines changed

2 files changed

+145
-100
lines changed

clang/unittests/ASTMatchers/ASTMatchersTest.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ matchesConditionally(const std::string &Code, const T &AMatcher,
153153
}
154154

155155
for (auto Mode : LangModes) {
156-
std::string LangModeArg;
156+
StringRef LangModeArg;
157157
switch (Mode) {
158158
case LanguageMode::Cxx11:
159159
LangModeArg = "-std=c++11";
@@ -171,8 +171,10 @@ matchesConditionally(const std::string &Code, const T &AMatcher,
171171
llvm_unreachable("Invalid language mode");
172172
}
173173

174-
auto Result =
175-
matchesConditionally(Code, AMatcher, ExpectMatch, LangModeArg);
174+
auto Result = matchesConditionally(Code, AMatcher, ExpectMatch,
175+
{LangModeArg, "-Werror=c++14-extensions",
176+
"-Werror=c++17-extensions",
177+
"-Werror=c++20-extensions"});
176178
if (!Result)
177179
return Result;
178180
}

clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Lines changed: 140 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,14 @@ TEST(TemplateTypeParmDecl, VarTemplatePartialSpecializationDecl) {
950950
"template<typename U>\n"
951951
"template<typename U2>\n"
952952
"int Struct<U>::field<U2*> = 123;\n";
953-
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T"))));
954-
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T2"))));
955-
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U"))));
956-
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U2"))));
953+
EXPECT_TRUE(
954+
matches(input, templateTypeParmDecl(hasName("T")), LanguageMode::Cxx14));
955+
EXPECT_TRUE(
956+
matches(input, templateTypeParmDecl(hasName("T2")), LanguageMode::Cxx14));
957+
EXPECT_TRUE(
958+
matches(input, templateTypeParmDecl(hasName("U")), LanguageMode::Cxx14));
959+
EXPECT_TRUE(
960+
matches(input, templateTypeParmDecl(hasName("U2")), LanguageMode::Cxx14));
957961
}
958962

959963
TEST(TemplateTypeParmDecl, ClassTemplatePartialSpecializationDecl) {
@@ -2061,113 +2065,146 @@ void func14() {
20612065
20622066
)cpp";
20632067

2064-
EXPECT_TRUE(matches(
2065-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2066-
returnStmt(forFunction(functionDecl(hasName("func1"))),
2067-
hasReturnValue(integerLiteral(equals(42)))))));
2068+
EXPECT_TRUE(
2069+
matches(Code,
2070+
traverse(TK_IgnoreUnlessSpelledInSource,
2071+
returnStmt(forFunction(functionDecl(hasName("func1"))),
2072+
hasReturnValue(integerLiteral(equals(42))))),
2073+
LanguageMode::Cxx2a));
20682074

2069-
EXPECT_TRUE(matches(
2070-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2071-
integerLiteral(equals(42),
2072-
hasParent(returnStmt(forFunction(
2073-
functionDecl(hasName("func1")))))))));
2075+
EXPECT_TRUE(
2076+
matches(Code,
2077+
traverse(TK_IgnoreUnlessSpelledInSource,
2078+
integerLiteral(equals(42),
2079+
hasParent(returnStmt(forFunction(
2080+
functionDecl(hasName("func1"))))))),
2081+
LanguageMode::Cxx2a));
20742082

20752083
EXPECT_TRUE(matches(
20762084
Code,
20772085
traverse(TK_IgnoreUnlessSpelledInSource,
20782086
returnStmt(forFunction(functionDecl(hasName("func2"))),
20792087
hasReturnValue(cxxTemporaryObjectExpr(
2080-
hasArgument(0, integerLiteral(equals(42)))))))));
2088+
hasArgument(0, integerLiteral(equals(42))))))),
2089+
LanguageMode::Cxx2a));
20812090
EXPECT_TRUE(matches(
20822091
Code,
2083-
traverse(TK_IgnoreUnlessSpelledInSource,
2084-
integerLiteral(
2085-
equals(42),
2086-
hasParent(cxxTemporaryObjectExpr(hasParent(returnStmt(
2087-
forFunction(functionDecl(hasName("func2")))))))))));
2092+
traverse(
2093+
TK_IgnoreUnlessSpelledInSource,
2094+
integerLiteral(equals(42),
2095+
hasParent(cxxTemporaryObjectExpr(hasParent(returnStmt(
2096+
forFunction(functionDecl(hasName("func2"))))))))),
2097+
LanguageMode::Cxx2a));
20882098

20892099
EXPECT_TRUE(matches(
2090-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2091-
returnStmt(forFunction(functionDecl(hasName("func3"))),
2092-
hasReturnValue(
2093-
cxxFunctionalCastExpr(hasSourceExpression(
2094-
integerLiteral(equals(42)))))))));
2100+
Code,
2101+
traverse(
2102+
TK_IgnoreUnlessSpelledInSource,
2103+
returnStmt(forFunction(functionDecl(hasName("func3"))),
2104+
hasReturnValue(cxxFunctionalCastExpr(
2105+
hasSourceExpression(integerLiteral(equals(42))))))),
2106+
LanguageMode::Cxx2a));
20952107

20962108
EXPECT_TRUE(matches(
20972109
Code,
2098-
traverse(TK_IgnoreUnlessSpelledInSource,
2099-
integerLiteral(
2100-
equals(42),
2101-
hasParent(cxxFunctionalCastExpr(hasParent(returnStmt(
2102-
forFunction(functionDecl(hasName("func3")))))))))));
2110+
traverse(
2111+
TK_IgnoreUnlessSpelledInSource,
2112+
integerLiteral(equals(42),
2113+
hasParent(cxxFunctionalCastExpr(hasParent(returnStmt(
2114+
forFunction(functionDecl(hasName("func3"))))))))),
2115+
LanguageMode::Cxx2a));
21032116

2104-
EXPECT_TRUE(matches(
2105-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2106-
returnStmt(forFunction(functionDecl(hasName("func4"))),
2107-
hasReturnValue(cxxTemporaryObjectExpr())))));
2117+
EXPECT_TRUE(
2118+
matches(Code,
2119+
traverse(TK_IgnoreUnlessSpelledInSource,
2120+
returnStmt(forFunction(functionDecl(hasName("func4"))),
2121+
hasReturnValue(cxxTemporaryObjectExpr()))),
2122+
LanguageMode::Cxx2a));
21082123

2109-
EXPECT_TRUE(matches(
2110-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2111-
returnStmt(forFunction(functionDecl(hasName("func5"))),
2112-
hasReturnValue(cxxTemporaryObjectExpr())))));
2124+
EXPECT_TRUE(
2125+
matches(Code,
2126+
traverse(TK_IgnoreUnlessSpelledInSource,
2127+
returnStmt(forFunction(functionDecl(hasName("func5"))),
2128+
hasReturnValue(cxxTemporaryObjectExpr()))),
2129+
LanguageMode::Cxx2a));
21132130

2114-
EXPECT_TRUE(matches(
2115-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2116-
returnStmt(forFunction(functionDecl(hasName("func6"))),
2117-
hasReturnValue(cxxTemporaryObjectExpr())))));
2131+
EXPECT_TRUE(
2132+
matches(Code,
2133+
traverse(TK_IgnoreUnlessSpelledInSource,
2134+
returnStmt(forFunction(functionDecl(hasName("func6"))),
2135+
hasReturnValue(cxxTemporaryObjectExpr()))),
2136+
LanguageMode::Cxx2a));
21182137

2119-
EXPECT_TRUE(matches(
2120-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2121-
returnStmt(forFunction(functionDecl(hasName("func7"))),
2122-
hasReturnValue(cxxTemporaryObjectExpr())))));
2138+
EXPECT_TRUE(
2139+
matches(Code,
2140+
traverse(TK_IgnoreUnlessSpelledInSource,
2141+
returnStmt(forFunction(functionDecl(hasName("func7"))),
2142+
hasReturnValue(cxxTemporaryObjectExpr()))),
2143+
LanguageMode::Cxx2a));
21232144

2124-
EXPECT_TRUE(matches(
2125-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2126-
returnStmt(forFunction(functionDecl(hasName("func8"))),
2127-
hasReturnValue(cxxFunctionalCastExpr(
2128-
hasSourceExpression(initListExpr())))))));
2145+
EXPECT_TRUE(
2146+
matches(Code,
2147+
traverse(TK_IgnoreUnlessSpelledInSource,
2148+
returnStmt(forFunction(functionDecl(hasName("func8"))),
2149+
hasReturnValue(cxxFunctionalCastExpr(
2150+
hasSourceExpression(initListExpr()))))),
2151+
LanguageMode::Cxx2a));
21292152

2130-
EXPECT_TRUE(matches(
2131-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2132-
returnStmt(forFunction(functionDecl(hasName("func9"))),
2133-
hasReturnValue(cxxFunctionalCastExpr(
2134-
hasSourceExpression(initListExpr())))))));
2153+
EXPECT_TRUE(
2154+
matches(Code,
2155+
traverse(TK_IgnoreUnlessSpelledInSource,
2156+
returnStmt(forFunction(functionDecl(hasName("func9"))),
2157+
hasReturnValue(cxxFunctionalCastExpr(
2158+
hasSourceExpression(initListExpr()))))),
2159+
LanguageMode::Cxx2a));
21352160

21362161
EXPECT_TRUE(matches(
2137-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2138-
returnStmt(forFunction(functionDecl(hasName("func10"))),
2139-
hasReturnValue(
2140-
declRefExpr(to(varDecl(hasName("a")))))))));
2162+
Code,
2163+
traverse(
2164+
TK_IgnoreUnlessSpelledInSource,
2165+
returnStmt(forFunction(functionDecl(hasName("func10"))),
2166+
hasReturnValue(declRefExpr(to(varDecl(hasName("a"))))))),
2167+
LanguageMode::Cxx2a));
21412168

2142-
EXPECT_TRUE(matches(
2143-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2144-
declRefExpr(to(varDecl(hasName("a"))),
2145-
hasParent(returnStmt(forFunction(
2146-
functionDecl(hasName("func10")))))))));
2169+
EXPECT_TRUE(
2170+
matches(Code,
2171+
traverse(TK_IgnoreUnlessSpelledInSource,
2172+
declRefExpr(to(varDecl(hasName("a"))),
2173+
hasParent(returnStmt(forFunction(
2174+
functionDecl(hasName("func10"))))))),
2175+
LanguageMode::Cxx2a));
21472176

21482177
EXPECT_TRUE(matches(
2149-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2150-
returnStmt(forFunction(functionDecl(hasName("func11"))),
2151-
hasReturnValue(
2152-
declRefExpr(to(varDecl(hasName("b")))))))));
2178+
Code,
2179+
traverse(
2180+
TK_IgnoreUnlessSpelledInSource,
2181+
returnStmt(forFunction(functionDecl(hasName("func11"))),
2182+
hasReturnValue(declRefExpr(to(varDecl(hasName("b"))))))),
2183+
LanguageMode::Cxx2a));
21532184

2154-
EXPECT_TRUE(matches(
2155-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2156-
declRefExpr(to(varDecl(hasName("b"))),
2157-
hasParent(returnStmt(forFunction(
2158-
functionDecl(hasName("func11")))))))));
2185+
EXPECT_TRUE(
2186+
matches(Code,
2187+
traverse(TK_IgnoreUnlessSpelledInSource,
2188+
declRefExpr(to(varDecl(hasName("b"))),
2189+
hasParent(returnStmt(forFunction(
2190+
functionDecl(hasName("func11"))))))),
2191+
LanguageMode::Cxx2a));
21592192

21602193
EXPECT_TRUE(matches(
2161-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2162-
returnStmt(forFunction(functionDecl(hasName("func12"))),
2163-
hasReturnValue(
2164-
declRefExpr(to(varDecl(hasName("c")))))))));
2194+
Code,
2195+
traverse(
2196+
TK_IgnoreUnlessSpelledInSource,
2197+
returnStmt(forFunction(functionDecl(hasName("func12"))),
2198+
hasReturnValue(declRefExpr(to(varDecl(hasName("c"))))))),
2199+
LanguageMode::Cxx2a));
21652200

2166-
EXPECT_TRUE(matches(
2167-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2168-
declRefExpr(to(varDecl(hasName("c"))),
2169-
hasParent(returnStmt(forFunction(
2170-
functionDecl(hasName("func12")))))))));
2201+
EXPECT_TRUE(
2202+
matches(Code,
2203+
traverse(TK_IgnoreUnlessSpelledInSource,
2204+
declRefExpr(to(varDecl(hasName("c"))),
2205+
hasParent(returnStmt(forFunction(
2206+
functionDecl(hasName("func12"))))))),
2207+
LanguageMode::Cxx2a));
21712208

21722209
EXPECT_TRUE(matches(
21732210
Code,
@@ -2178,32 +2215,38 @@ void func14() {
21782215
has(declRefExpr(to(varDecl(hasName("a"))))),
21792216
has(varDecl(hasName("b"), hasInitializer(declRefExpr(to(
21802217
varDecl(hasName("c"))))))),
2181-
has(parmVarDecl(hasName("d")))))));
2218+
has(parmVarDecl(hasName("d"))))),
2219+
LanguageMode::Cxx2a));
21822220

2183-
EXPECT_TRUE(matches(
2184-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2185-
declRefExpr(to(varDecl(hasName("a"))),
2186-
hasParent(lambdaExpr(forFunction(
2187-
functionDecl(hasName("func13")))))))));
2221+
EXPECT_TRUE(
2222+
matches(Code,
2223+
traverse(TK_IgnoreUnlessSpelledInSource,
2224+
declRefExpr(to(varDecl(hasName("a"))),
2225+
hasParent(lambdaExpr(forFunction(
2226+
functionDecl(hasName("func13"))))))),
2227+
LanguageMode::Cxx2a));
21882228

21892229
EXPECT_TRUE(matches(
21902230
Code,
21912231
traverse(TK_IgnoreUnlessSpelledInSource,
21922232
varDecl(hasName("b"),
21932233
hasInitializer(declRefExpr(to(varDecl(hasName("c"))))),
21942234
hasParent(lambdaExpr(
2195-
forFunction(functionDecl(hasName("func13")))))))));
2235+
forFunction(functionDecl(hasName("func13"))))))),
2236+
LanguageMode::Cxx2a));
21962237

21972238
EXPECT_TRUE(matches(
2198-
Code, traverse(TK_IgnoreUnlessSpelledInSource,
2199-
lambdaExpr(
2200-
forFunction(functionDecl(hasName("func14"))),
2201-
has(templateTypeParmDecl(hasName("TemplateType")))))));
2239+
Code,
2240+
traverse(TK_IgnoreUnlessSpelledInSource,
2241+
lambdaExpr(forFunction(functionDecl(hasName("func14"))),
2242+
has(templateTypeParmDecl(hasName("TemplateType"))))),
2243+
LanguageMode::Cxx2a));
22022244

2203-
EXPECT_TRUE(
2204-
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
2205-
functionDecl(hasName("func14"),
2206-
hasDescendant(floatLiteral())))));
2245+
EXPECT_TRUE(matches(
2246+
Code,
2247+
traverse(TK_IgnoreUnlessSpelledInSource,
2248+
functionDecl(hasName("func14"), hasDescendant(floatLiteral()))),
2249+
LanguageMode::Cxx2a));
22072250
}
22082251

22092252
TEST(IgnoringImpCasts, MatchesImpCasts) {

0 commit comments

Comments
 (0)