[clang-tools-extra] [clang-tidy] [NFC] Move comment scanning to `LexerUtils` and add tests (PR #180371)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 11 17:51:45 PST 2026
================
@@ -0,0 +1,149 @@
+//===--- LexerUtilsTest.cpp - clang-tidy ---------------------------------===//
+//
+// 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 "../clang-tidy/utils/LexerUtils.h"
+
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Frontend/ASTUnit.h"
+#include "clang/Serialization/PCHContainerOperations.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Testing/Annotations/Annotations.h"
+#include "gtest/gtest.h"
+
+namespace clang::tidy::test {
+
+using clang::tooling::FileContentMappings;
+
+static std::unique_ptr<ASTUnit>
+buildAST(StringRef Code, const FileContentMappings &Mappings = {}) {
+ std::vector<std::string> Args = {"-std=c++20"};
+ return clang::tooling::buildASTFromCodeWithArgs(
+ Code, Args, "input.cc", "clang-tool",
+ std::make_shared<PCHContainerOperations>(),
+ clang::tooling::getClangStripDependencyFileAdjuster(), Mappings);
+}
+
+static CharSourceRange rangeFromAnnotations(const llvm::Annotations &A,
+ const SourceManager &SM, FileID FID,
+ llvm::StringRef Name = "") {
+ const auto R = A.range(Name);
+ const SourceLocation Begin =
+ SM.getLocForStartOfFile(FID).getLocWithOffset(R.Begin);
+ const SourceLocation End =
+ SM.getLocForStartOfFile(FID).getLocWithOffset(R.End);
+ return CharSourceRange::getCharRange(Begin, End);
+}
+
+namespace {
+
+TEST(LexerUtilsTest, GetTrailingCommentsInRangeAdjacentComments) {
+ llvm::Annotations Code(R"cpp(
+void f() {
+ $range[[/*first*/ /*second*/]]
+ int x = 0;
+}
+)cpp");
+ std::unique_ptr<ASTUnit> AST = buildAST(Code.code());
+ ASSERT_TRUE(AST);
+ const ASTContext &Context = AST->getASTContext();
+ const SourceManager &SM = Context.getSourceManager();
+ const LangOptions &LangOpts = Context.getLangOpts();
+
+ const CharSourceRange Range =
+ rangeFromAnnotations(Code, SM, SM.getMainFileID(), "range");
+ const std::vector<utils::lexer::CommentToken> Comments =
+ utils::lexer::getTrailingCommentsInRange(Range, SM, LangOpts);
+ ASSERT_EQ(2u, Comments.size());
+ EXPECT_EQ("/*first*/", Comments[0].Text);
+ EXPECT_EQ("/*second*/", Comments[1].Text);
+ const StringRef CodeText = Code.code();
+ const size_t FirstOffset = CodeText.find("/*first*/");
+ ASSERT_NE(StringRef::npos, FirstOffset);
+ const size_t SecondOffset = CodeText.find("/*second*/");
+ ASSERT_NE(StringRef::npos, SecondOffset);
+ EXPECT_EQ(static_cast<unsigned>(FirstOffset),
----------------
localspook wrote:
The casts to `unsigned` here and below seem unnecessary; there's nothing wrong with comparing an `unsigned` and a `size_t` and letting implicit conversions do their thing
https://github.com/llvm/llvm-project/pull/180371
More information about the cfe-commits
mailing list