2014-03-15 01:44:10 +08:00
|
|
|
//===- unittests/AST/EvaluateAsRValueTest.cpp -----------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-03-15 01:44:10 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// \file
|
|
|
|
// \brief Unit tests for evaluation of constant initializers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-01-14 19:23:58 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2014-03-15 01:44:10 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "gtest/gtest.h"
|
2015-01-14 19:23:58 +08:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2014-03-15 01:44:10 +08:00
|
|
|
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// For each variable name encountered, whether its initializer was a
|
|
|
|
// constant.
|
|
|
|
typedef std::map<std::string, bool> VarInfoMap;
|
|
|
|
|
|
|
|
/// \brief Records information on variable initializers to a map.
|
|
|
|
class EvaluateConstantInitializersVisitor
|
|
|
|
: public clang::RecursiveASTVisitor<EvaluateConstantInitializersVisitor> {
|
|
|
|
public:
|
|
|
|
explicit EvaluateConstantInitializersVisitor(VarInfoMap &VarInfo)
|
|
|
|
: VarInfo(VarInfo) {}
|
|
|
|
|
|
|
|
/// \brief Checks that isConstantInitializer and EvaluateAsRValue agree
|
|
|
|
/// and don't crash.
|
|
|
|
///
|
|
|
|
/// For each VarDecl with an initializer this also records in VarInfo
|
|
|
|
/// whether the initializer could be evaluated as a constant.
|
|
|
|
bool VisitVarDecl(const clang::VarDecl *VD) {
|
|
|
|
if (const clang::Expr *Init = VD->getInit()) {
|
|
|
|
clang::Expr::EvalResult Result;
|
|
|
|
bool WasEvaluated = Init->EvaluateAsRValue(Result, VD->getASTContext());
|
|
|
|
VarInfo[VD->getNameAsString()] = WasEvaluated;
|
|
|
|
EXPECT_EQ(WasEvaluated, Init->isConstantInitializer(VD->getASTContext(),
|
|
|
|
false /*ForRef*/));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
VarInfoMap &VarInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
class EvaluateConstantInitializersAction : public clang::ASTFrontendAction {
|
|
|
|
public:
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<clang::ASTConsumer>
|
|
|
|
CreateASTConsumer(clang::CompilerInstance &Compiler,
|
|
|
|
llvm::StringRef FilePath) override {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<Consumer>();
|
2014-03-15 01:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
class Consumer : public clang::ASTConsumer {
|
|
|
|
public:
|
2015-10-20 21:23:58 +08:00
|
|
|
~Consumer() override {}
|
2014-03-15 01:44:10 +08:00
|
|
|
|
|
|
|
void HandleTranslationUnit(clang::ASTContext &Ctx) override {
|
|
|
|
VarInfoMap VarInfo;
|
|
|
|
EvaluateConstantInitializersVisitor Evaluator(VarInfo);
|
|
|
|
Evaluator.TraverseDecl(Ctx.getTranslationUnitDecl());
|
|
|
|
EXPECT_EQ(2u, VarInfo.size());
|
|
|
|
EXPECT_FALSE(VarInfo["Dependent"]);
|
|
|
|
EXPECT_TRUE(VarInfo["Constant"]);
|
|
|
|
EXPECT_EQ(2u, VarInfo.size());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2014-03-15 01:44:10 +08:00
|
|
|
|
|
|
|
TEST(EvaluateAsRValue, FailsGracefullyForUnknownTypes) {
|
|
|
|
// This is a regression test; the AST library used to trigger assertion
|
|
|
|
// failures because it assumed that the type of initializers was always
|
|
|
|
// known (which is true only after template instantiation).
|
|
|
|
std::string ModesToTest[] = {"-std=c++03", "-std=c++11", "-std=c++1y"};
|
|
|
|
for (std::string const &Mode : ModesToTest) {
|
|
|
|
std::vector<std::string> Args(1, Mode);
|
2014-03-15 08:36:51 +08:00
|
|
|
Args.push_back("-fno-delayed-template-parsing");
|
2014-03-15 01:44:10 +08:00
|
|
|
ASSERT_TRUE(runToolOnCodeWithArgs(
|
2019-08-30 17:29:34 +08:00
|
|
|
std::make_unique<EvaluateConstantInitializersAction>(),
|
|
|
|
"template <typename T>"
|
|
|
|
"struct vector {"
|
|
|
|
" explicit vector(int size);"
|
|
|
|
"};"
|
|
|
|
"template <typename R>"
|
|
|
|
"struct S {"
|
|
|
|
" vector<R> intervals() const {"
|
|
|
|
" vector<R> Dependent(2);"
|
|
|
|
" return Dependent;"
|
|
|
|
" }"
|
|
|
|
"};"
|
|
|
|
"void doSomething() {"
|
|
|
|
" int Constant = 2 + 2;"
|
|
|
|
" (void) Constant;"
|
|
|
|
"}",
|
|
|
|
Args));
|
2014-03-15 01:44:10 +08:00
|
|
|
}
|
|
|
|
}
|