blob: e737507abd781639f38ca4fab30bd01d25f507e1 [file] [log] [blame]
James Dennett0492ef02014-03-14 17:44:10 +00001//===- unittests/AST/EvaluateAsRValueTest.cpp -----------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
James Dennett0492ef02014-03-14 17:44:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// \file
10// \brief Unit tests for evaluation of constant initializers.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000014#include "clang/AST/ASTConsumer.h"
James Dennett0492ef02014-03-14 17:44:10 +000015#include "clang/AST/ASTConsumer.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/RecursiveASTVisitor.h"
18#include "clang/Tooling/Tooling.h"
19#include "gtest/gtest.h"
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000020#include <map>
21#include <string>
James Dennett0492ef02014-03-14 17:44:10 +000022
23using namespace clang::tooling;
24
25namespace {
26// For each variable name encountered, whether its initializer was a
27// constant.
28typedef std::map<std::string, bool> VarInfoMap;
29
30/// \brief Records information on variable initializers to a map.
31class EvaluateConstantInitializersVisitor
32 : public clang::RecursiveASTVisitor<EvaluateConstantInitializersVisitor> {
33 public:
34 explicit EvaluateConstantInitializersVisitor(VarInfoMap &VarInfo)
35 : VarInfo(VarInfo) {}
36
37 /// \brief Checks that isConstantInitializer and EvaluateAsRValue agree
38 /// and don't crash.
39 ///
40 /// For each VarDecl with an initializer this also records in VarInfo
41 /// whether the initializer could be evaluated as a constant.
42 bool VisitVarDecl(const clang::VarDecl *VD) {
43 if (const clang::Expr *Init = VD->getInit()) {
44 clang::Expr::EvalResult Result;
45 bool WasEvaluated = Init->EvaluateAsRValue(Result, VD->getASTContext());
46 VarInfo[VD->getNameAsString()] = WasEvaluated;
47 EXPECT_EQ(WasEvaluated, Init->isConstantInitializer(VD->getASTContext(),
48 false /*ForRef*/));
49 }
50 return true;
51 }
52
53 private:
54 VarInfoMap &VarInfo;
55};
56
57class EvaluateConstantInitializersAction : public clang::ASTFrontendAction {
58 public:
David Blaikie6beb6aa2014-08-10 19:56:51 +000059 std::unique_ptr<clang::ASTConsumer>
60 CreateASTConsumer(clang::CompilerInstance &Compiler,
61 llvm::StringRef FilePath) override {
62 return llvm::make_unique<Consumer>();
James Dennett0492ef02014-03-14 17:44:10 +000063 }
64
65 private:
66 class Consumer : public clang::ASTConsumer {
67 public:
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000068 ~Consumer() override {}
James Dennett0492ef02014-03-14 17:44:10 +000069
70 void HandleTranslationUnit(clang::ASTContext &Ctx) override {
71 VarInfoMap VarInfo;
72 EvaluateConstantInitializersVisitor Evaluator(VarInfo);
73 Evaluator.TraverseDecl(Ctx.getTranslationUnitDecl());
74 EXPECT_EQ(2u, VarInfo.size());
75 EXPECT_FALSE(VarInfo["Dependent"]);
76 EXPECT_TRUE(VarInfo["Constant"]);
77 EXPECT_EQ(2u, VarInfo.size());
78 }
79 };
80};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000081}
James Dennett0492ef02014-03-14 17:44:10 +000082
83TEST(EvaluateAsRValue, FailsGracefullyForUnknownTypes) {
84 // This is a regression test; the AST library used to trigger assertion
85 // failures because it assumed that the type of initializers was always
86 // known (which is true only after template instantiation).
87 std::string ModesToTest[] = {"-std=c++03", "-std=c++11", "-std=c++1y"};
88 for (std::string const &Mode : ModesToTest) {
89 std::vector<std::string> Args(1, Mode);
NAKAMURA Takumi7ebbda82014-03-15 00:36:51 +000090 Args.push_back("-fno-delayed-template-parsing");
James Dennett0492ef02014-03-14 17:44:10 +000091 ASSERT_TRUE(runToolOnCodeWithArgs(
Roman Lebedev497fd982018-02-27 15:54:55 +000092 new EvaluateConstantInitializersAction(),
James Dennett622831b2014-03-14 20:08:11 +000093 "template <typename T>"
94 "struct vector {"
95 " explicit vector(int size);"
96 "};"
97 "template <typename R>"
98 "struct S {"
99 " vector<R> intervals() const {"
100 " vector<R> Dependent(2);"
101 " return Dependent;"
102 " }"
103 "};"
104 "void doSomething() {"
105 " int Constant = 2 + 2;"
106 " (void) Constant;"
107 "}",
James Dennett0492ef02014-03-14 17:44:10 +0000108 Args));
109 }
110}