Files
TheRock/compiler/amd-llvm/clang/unittests/Format/FormatTokenSourceTest.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

127 lines
5.0 KiB
C++

//===- unittest/Format/FormatTokenSourceTest.cpp --------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "../../lib/Format/FormatTokenSource.h"
#include "TestLexer.h"
#include "clang/Basic/TokenKinds.h"
#include "gtest/gtest.h"
namespace clang {
namespace format {
namespace {
class IndexedTokenSourceTest : public testing::Test {
protected:
TokenList lex(StringRef Code, const FormatStyle &Style = getLLVMStyle()) {
return TestLexer(Allocator, Buffers, Style).lex(Code);
}
llvm::SpecificBumpPtrAllocator<FormatToken> Allocator;
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Buffers;
};
#define EXPECT_TOKEN_KIND(FormatTok, Kind) \
do { \
FormatToken *Tok = FormatTok; \
EXPECT_EQ(Tok->Tok.getKind(), Kind) << *Tok; \
} while (false);
#define EXPECT_TOKEN_ID(FormatTok, Name) \
do { \
FormatToken *Tok = FormatTok; \
EXPECT_EQ(Tok->Tok.getKind(), tok::identifier) << *Tok; \
EXPECT_EQ(Tok->TokenText, Name) << *Tok; \
} while (false);
TEST_F(IndexedTokenSourceTest, EmptyInput) {
IndexedTokenSource Source(lex(""));
EXPECT_FALSE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TRUE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TRUE(Source.isEOF());
EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/false), tok::eof);
EXPECT_TOKEN_KIND(Source.peekNextToken(/*SkipComment=*/true), tok::eof);
EXPECT_EQ(Source.getPreviousToken(), nullptr);
EXPECT_TRUE(Source.isEOF());
}
TEST_F(IndexedTokenSourceTest, NavigateTokenStream) {
IndexedTokenSource Source(lex("int a;"));
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::kw_int);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::kw_int);
EXPECT_EQ(Source.getPreviousToken(), nullptr);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::kw_int);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::semi);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::semi);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::identifier);
EXPECT_TOKEN_KIND(Source.peekNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::semi);
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.getPreviousToken(), tok::semi);
}
TEST_F(IndexedTokenSourceTest, ResetPosition) {
IndexedTokenSource Source(lex("int a;"));
Source.getNextToken();
unsigned Position = Source.getPosition();
Source.getNextToken();
Source.getNextToken();
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_KIND(Source.setPosition(Position), tok::kw_int);
}
TEST_F(IndexedTokenSourceTest, InsertTokens) {
IndexedTokenSource Source(lex("A1 A2"));
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_ID(Source.insertTokens(lex("B1 B2")), "B1");
EXPECT_TOKEN_ID(Source.getNextToken(), "B2");
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_ID(Source.getNextToken(), "A2");
}
TEST_F(IndexedTokenSourceTest, InsertTokensAtEOF) {
IndexedTokenSource Source(lex("A1"));
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
EXPECT_TOKEN_ID(Source.insertTokens(lex("B1 B2")), "B1");
EXPECT_TOKEN_ID(Source.getNextToken(), "B2");
EXPECT_TOKEN_KIND(Source.getNextToken(), tok::eof);
}
TEST_F(IndexedTokenSourceTest, InsertTokensRecursive) {
IndexedTokenSource Source(lex("A1"));
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
// A1
EXPECT_TOKEN_ID(Source.insertTokens(lex("B1")), "B1");
// B1 A1
EXPECT_TOKEN_ID(Source.insertTokens(lex("C1")), "C1");
// C1 B1 A1
EXPECT_TOKEN_ID(Source.insertTokens(lex("D1")), "D1");
// D1 C1 B1 A1
EXPECT_TOKEN_ID(Source.getNextToken(), "C1");
EXPECT_TOKEN_ID(Source.getNextToken(), "B1");
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
}
TEST_F(IndexedTokenSourceTest, InsertTokensRecursiveAtEndOfSequence) {
IndexedTokenSource Source(lex("A1"));
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_ID(Source.insertTokens(lex("B1")), "B1");
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_ID(Source.insertTokens(lex("C1")), "C1");
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
EXPECT_TOKEN_ID(Source.insertTokens(lex("D1")), "D1");
EXPECT_TOKEN_ID(Source.getNextToken(), "A1");
}
} // namespace
} // namespace format
} // namespace clang