[clang-tools-extra] [clang-tidy] [NFC] Move comment scanning to `LexerUtils` and add tests (PR #180371)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 9 06:39:57 PST 2026
================
@@ -0,0 +1,130 @@
+//===--- 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 {
+namespace tidy {
+namespace 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);
----------------
vbvictor wrote:
Can we also `EXPECT_EQ()` on SourceLoc of these comments?
Or we can't get any meaningful result for `Loc`'s
https://github.com/llvm/llvm-project/pull/180371
More information about the cfe-commits
mailing list