From: Sam McCall Date: Tue, 18 Oct 2022 17:12:47 +0000 (+0200) Subject: [include-cleaner] Add line numbers to HTML output X-Git-Tag: upstream/17.0.6~30167 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=25bac38a02e3d43a16282d66c38084bcd553c9b5;p=platform%2Fupstream%2Fllvm.git [include-cleaner] Add line numbers to HTML output --- diff --git a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp index c7bfc36..191fa55 100644 --- a/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp +++ b/clang-tools-extra/include-cleaner/lib/HTMLReport.cpp @@ -25,7 +25,16 @@ namespace clang::include_cleaner { namespace { constexpr llvm::StringLiteral CSS = R"css( - pre { line-height: 1.5em; } + body { margin: 0; } + pre { line-height: 1.5em; counter-reset: line; margin: 0; } + pre .line { counter-increment: line; } + pre .line::before { + content: counter(line); + display: inline-block; + background-color: #eee; border-right: 1px solid #ccc; + text-align: right; + width: 3em; padding-right: 0.5em; margin-right: 0.5em; + } .ref { text-decoration: underline; color: #008; } .sel { position: relative; cursor: pointer; } #hover { @@ -83,10 +92,12 @@ class Reporter { const SourceManager &SM; FileID File; + // Symbols that are referenced from the main file. struct Target { const NamedDecl *D; }; std::vector Targets; + // Points within the main file that reference a Target. std::vector> Refs; public: @@ -94,11 +105,17 @@ public: : OS(OS), Ctx(Ctx), SM(Ctx.getSourceManager()), File(File) {} void addRef(SourceLocation Loc, const NamedDecl &D) { - auto Coords = SM.getDecomposedLoc(SM.getFileLoc(Loc)); - if (Coords.first != File) - llvm::errs() << "Ref location outside file!\n"; + auto [File, Offset] = SM.getDecomposedLoc(SM.getFileLoc(Loc)); + if (File != this->File) { + // Can get here e.g. if there's an #include inside a root Decl. + // FIXME: do something more useful than this. + llvm::errs() << "Ref location outside file! " + << D.getQualifiedNameAsString() << " at " + << Loc.printToString(SM) << "\n"; + return; + } Targets.push_back({&D}); - Refs.push_back({Coords.second, Targets.size() - 1}); + Refs.push_back({Offset, Targets.size() - 1}); } void write() { @@ -154,11 +171,13 @@ private: llvm::sort(Refs); llvm::StringRef Code = SM.getBufferData(File); - OS << "
";
+    OS << "
";
+    OS << "";
     auto Rest = llvm::makeArrayRef(Refs);
     unsigned End = 0;
     for (unsigned I = 0; I < Code.size(); ++I) {
-      if (End == I && I > 0) {
+      // Finish refs early at EOL to avoid dealing with splitting the span.
+      if (End && (End == I || Code[I] == '\n')) {
         OS << "";
         End = 0;
       }
@@ -178,9 +197,12 @@ private:
         End = I + Lexer::MeasureTokenLength(SM.getComposedLoc(File, I), SM,
                                             Ctx.getLangOpts());
       }
-      escapeChar(Code[I]);
+      if (Code[I] == '\n')
+        OS << "\n";
+      else
+        escapeChar(Code[I]);
     }
-    OS << "
\n"; + OS << "
\n"; } }; diff --git a/clang-tools-extra/include-cleaner/test/CMakeLists.txt b/clang-tools-extra/include-cleaner/test/CMakeLists.txt index a31858f..7852534 100644 --- a/clang-tools-extra/include-cleaner/test/CMakeLists.txt +++ b/clang-tools-extra/include-cleaner/test/CMakeLists.txt @@ -1,5 +1,6 @@ set(CLANG_INCLUDE_CLEANER_TEST_DEPS ClangIncludeCleanerTests + clang-include-cleaner ) foreach (dep FileCheck not count) diff --git a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp index 898ac1b..8eaa3ff 100644 --- a/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/RecordTest.cpp @@ -1,3 +1,11 @@ +//===-- RecordTest.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 "clang-include-cleaner/Record.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Testing/TestAST.h"