1//===- CharacterTest.cpp -- CharacterExprHelper unit tests ----------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "flang/Optimizer/Builder/Character.h"
10#include "gtest/gtest.h"
11#include "flang/Optimizer/Builder/BoxValue.h"
12#include "flang/Optimizer/Builder/FIRBuilder.h"
13#include "flang/Optimizer/Dialect/Support/KindMapping.h"
14#include "flang/Optimizer/Support/InitFIR.h"
15
16struct CharacterTest : public testing::Test {
17public:
18 void SetUp() override {
19 fir::support::loadDialects(context);
20
21 kindMap = std::make_unique<fir::KindMapping>(&context,
22 "i10:80,l3:24,a1:8,r54:Double,c20:X86_FP80,r11:PPC_FP128,"
23 "r12:FP128,r13:X86_FP80,r14:Double,r15:Float,r16:Half,r23:BFloat");
24 mlir::OpBuilder builder(&context);
25 auto loc = builder.getUnknownLoc();
26
27 // Set up a Module with a dummy function operation inside.
28 // Set the insertion point in the function entry block.
29 mlir::ModuleOp mod = builder.create<mlir::ModuleOp>(loc);
30 mlir::func::FuncOp func = mlir::func::FuncOp::create(
31 loc, "func1", builder.getFunctionType(std::nullopt, std::nullopt));
32 auto *entryBlock = func.addEntryBlock();
33 mod.push_back(mod);
34 builder.setInsertionPointToStart(entryBlock);
35
36 firBuilder = std::make_unique<fir::FirOpBuilder>(mod, *kindMap);
37 }
38
39 fir::FirOpBuilder &getBuilder() { return *firBuilder; }
40
41 mlir::MLIRContext context;
42 std::unique_ptr<fir::KindMapping> kindMap;
43 std::unique_ptr<fir::FirOpBuilder> firBuilder;
44};
45
46TEST_F(CharacterTest, smallUtilityFunctions) {
47 auto builder = getBuilder();
48 auto loc = builder.getUnknownLoc();
49 llvm::StringRef strValue("onestringliteral");
50 auto strLit = fir::factory::createStringLiteral(builder, loc, strValue);
51 EXPECT_TRUE(
52 fir::factory::CharacterExprHelper::hasConstantLengthInType(strLit));
53 auto ty = strLit.getCharBox()->getAddr().getType();
54 EXPECT_TRUE(fir::factory::CharacterExprHelper::isCharacterScalar(ty));
55 EXPECT_EQ(fir::factory::CharacterExprHelper::getCharacterOrSequenceKind(ty),
56 fir::factory::CharacterExprHelper::getCharacterKind(ty));
57}
58
59TEST_F(CharacterTest, createSubstring) {
60 auto builder = getBuilder();
61 auto loc = builder.getUnknownLoc();
62 auto charHelper = fir::factory::CharacterExprHelper(builder, loc);
63 llvm::StringRef data("a dummy string to test substring");
64 auto str = fir::factory::createStringLiteral(builder, loc, data);
65 auto lb = builder.createIntegerConstant(loc, builder.getI64Type(), 18);
66 auto ub = builder.createIntegerConstant(loc, builder.getI64Type(), 22);
67 auto substr = charHelper.createSubstring(*str.getCharBox(), {lb, ub});
68 EXPECT_FALSE(
69 fir::factory::CharacterExprHelper::hasConstantLengthInType(substr));
70 EXPECT_FALSE(charHelper.getCharacterType(substr).hasConstantLen());
71 EXPECT_FALSE(fir::factory::CharacterExprHelper::isArray(
72 charHelper.getCharacterType(substr)));
73}
74

source code of flang/unittests/Optimizer/Builder/CharacterTest.cpp