James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 1 | //===- unittests/AST/EvaluateAsRValueTest.cpp -----------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // \file |
| 10 | // \brief Unit tests for evaluation of constant initializers. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 15 | #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 Carruth | 575bc3ba | 2015-01-14 11:23:58 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <string> |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace clang::tooling; |
| 24 | |
| 25 | namespace { |
| 26 | // For each variable name encountered, whether its initializer was a |
| 27 | // constant. |
| 28 | typedef std::map<std::string, bool> VarInfoMap; |
| 29 | |
| 30 | /// \brief Records information on variable initializers to a map. |
| 31 | class 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 | |
| 57 | class EvaluateConstantInitializersAction : public clang::ASTFrontendAction { |
| 58 | public: |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 59 | std::unique_ptr<clang::ASTConsumer> |
| 60 | CreateASTConsumer(clang::CompilerInstance &Compiler, |
| 61 | llvm::StringRef FilePath) override { |
| 62 | return llvm::make_unique<Consumer>(); |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
| 66 | class Consumer : public clang::ASTConsumer { |
| 67 | public: |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 68 | ~Consumer() override {} |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 69 | |
| 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 Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 81 | } |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 82 | |
| 83 | TEST(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 Takumi | 7ebbda8 | 2014-03-15 00:36:51 +0000 | [diff] [blame] | 90 | Args.push_back("-fno-delayed-template-parsing"); |
James Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 91 | ASSERT_TRUE(runToolOnCodeWithArgs( |
Roman Lebedev | 497fd98 | 2018-02-27 15:54:55 +0000 | [diff] [blame] | 92 | new EvaluateConstantInitializersAction(), |
James Dennett | 622831b | 2014-03-14 20:08:11 +0000 | [diff] [blame] | 93 | "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 Dennett | 0492ef0 | 2014-03-14 17:44:10 +0000 | [diff] [blame] | 108 | Args)); |
| 109 | } |
| 110 | } |