https://github.com/PeterChou1 updated https://github.com/llvm/llvm-project/pull/101387
>From 3ec4d5b9e5bc24bfb466d65fd9c45c51fa6d3e94 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Wed, 31 Jul 2024 14:52:11 -0400 Subject: [PATCH 1/4] [clang-doc] uncomment unsupported --- clang-tools-extra/test/clang-doc/basic-project.test | 3 --- 1 file changed, 3 deletions(-) diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test index 38569d824f1f0..51d3ac6ce6dcd 100644 --- a/clang-tools-extra/test/clang-doc/basic-project.test +++ b/clang-tools-extra/test/clang-doc/basic-project.test @@ -1,6 +1,3 @@ -// See https://github.com/llvm/llvm-project/issues/97507. -// UNSUPPORTED: target={{.*}} - // RUN: rm -rf %t && mkdir -p %t/docs %t/build // RUN: sed 's|$test_dir|%/S|g' %S/Inputs/basic-project/database_template.json > %t/build/compile_commands.json // RUN: clang-doc --format=html --output=%t/docs --executor=all-TUs %t/build/compile_commands.json >From 114a5721e593a9b57ef31ed9856d0bce4a62da94 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 1 Aug 2024 03:24:42 -0400 Subject: [PATCH 2/4] [clang-doc] fix non-determinism --- clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 3363cafeded5e..9a540a5061b6a 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -287,7 +287,14 @@ Example usage for a project using a compile commands database: auto R = USRToBitcode.try_emplace(Key, std::vector<StringRef>()); R.first->second.emplace_back(Value); }); - + + for (auto &Entry : USRToBitcode) { + std::vector<llvm::StringRef> &Bitcode = Entry.second; + std::sort(Bitcode.begin(), Bitcode.end(), + [](const llvm::StringRef &A, const llvm::StringRef &B) { + return A < B; + }); + } // Collects all Infos according to their unique USR value. This map is added // to from the thread pool below and is protected by the USRToInfoMutex. llvm::sys::Mutex USRToInfoMutex; >From 556adbf8e1219d73c4ae859036be7ac21abd3762 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 1 Aug 2024 04:11:45 -0400 Subject: [PATCH 3/4] [clang-doc] fix tests --- .../test/clang-doc/basic-project.test | 12 +++---- .../test/clang-doc/namespace.cpp | 6 ++-- .../test/clang-doc/templates.cpp | 34 +++++++++---------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test index 51d3ac6ce6dcd..ee80493f6d7fa 100644 --- a/clang-tools-extra/test/clang-doc/basic-project.test +++ b/clang-tools-extra/test/clang-doc/basic-project.test @@ -73,12 +73,12 @@ // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">add</h3> // HTML-CALC: <p>public int add(int a, int b)</p> // HTML-CALC: <p>Defined at line 3 of file {{.*}}Calculator.cpp</p> -// HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">subtract</h3> -// HTML-CALC: <p>public int subtract(int a, int b)</p> -// HTML-CALC: <p>Defined at line 7 of file {{.*}}Calculator.cpp</p> // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">multiply</h3> // HTML-CALC: <p>public int multiply(int a, int b)</p> // HTML-CALC: <p>Defined at line 11 of file {{.*}}Calculator.cpp</p> +// HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">subtract</h3> +// HTML-CALC: <p>public int subtract(int a, int b)</p> +// HTML-CALC: <p>Defined at line 7 of file {{.*}}Calculator.cpp</p> // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">divide</h3> // HTML-CALC: <p>public double divide(int a, int b)</p> // HTML-CALC: <p>Defined at line 15 of file {{.*}}Calculator.cpp</p> @@ -114,15 +114,15 @@ // HTML-CIRCLE: <h2 id="Members">Members</h2> // HTML-CIRCLE: <li>private double radius_</li> // HTML-CIRCLE: <h2 id="Functions">Functions</h2> -// HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">Circle</h3> -// HTML-CIRCLE: <p>public void Circle(double radius)</p> -// HTML-CIRCLE: <p>Defined at line 3 of file {{.*}}Circle.cpp</p> // HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">area</h3> // HTML-CIRCLE: <p>public double area()</p> // HTML-CIRCLE: <p>Defined at line 5 of file {{.*}}Circle.cpp</p> // HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">perimeter</h3> // HTML-CIRCLE: <p>public double perimeter()</p> // HTML-CIRCLE: <p>Defined at line 9 of file {{.*}}Circle.cpp</p> +// HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">Circle</h3> +// HTML-CIRCLE: <p>public void Circle(double radius)</p> +// HTML-CIRCLE: <p>Defined at line 3 of file {{.*}}Circle.cpp</p> // MD-CALC: # class Calculator // MD-CALC: *Defined at .{{[\/]}}include{{[\/]}}Calculator.h#8* diff --git a/clang-tools-extra/test/clang-doc/namespace.cpp b/clang-tools-extra/test/clang-doc/namespace.cpp index 12f3cb8a84bc6..4881f942fe52a 100644 --- a/clang-tools-extra/test/clang-doc/namespace.cpp +++ b/clang-tools-extra/test/clang-doc/namespace.cpp @@ -271,15 +271,15 @@ namespace AnotherNamespace { // HTML-GLOBAL-INDEX: <div id="main-content" class="col-xs-12 col-sm-9 col-md-8 main-content"> // HTML-GLOBAL-INDEX: <h1>Global Namespace</h1> // HTML-GLOBAL-INDEX: <h2 id="Namespaces">Namespaces</h2> -// HTML-GLOBAL-INDEX: <li>@nonymous_namespace</li> // HTML-GLOBAL-INDEX: <li>PrimaryNamespace</li> // HTML-GLOBAL-INDEX: <li>AnotherNamespace</li> +// HTML-GLOBAL-INDEX: <li>@nonymous_namespace</li> // MD-GLOBAL-INDEX: # Global Namespace // MD-GLOBAL-INDEX: ## Namespaces -// MD-GLOBAL-INDEX: * [@nonymous_namespace](..{{[\/]}}@nonymous_namespace{{[\/]}}index.md) // MD-GLOBAL-INDEX: * [PrimaryNamespace](..{{[\/]}}PrimaryNamespace{{[\/]}}index.md) // MD-GLOBAL-INDEX: * [AnotherNamespace](..{{[\/]}}AnotherNamespace{{[\/]}}index.md) +// MD-GLOBAL-INDEX: * [@nonymous_namespace](..{{[\/]}}@nonymous_namespace{{[\/]}}index.md) // MD-ALL-FILES: # All Files // MD-ALL-FILES: ## [@nonymous_namespace](@nonymous_namespace{{[\/]}}index.md) @@ -290,4 +290,4 @@ namespace AnotherNamespace { // MD-INDEX: # C/C++ Reference // MD-INDEX: * Namespace: [@nonymous_namespace](@nonymous_namespace) // MD-INDEX: * Namespace: [AnotherNamespace](AnotherNamespace) -// MD-INDEX: * Namespace: [PrimaryNamespace](PrimaryNamespace) \ No newline at end of file +// MD-INDEX: * Namespace: [PrimaryNamespace](PrimaryNamespace) diff --git a/clang-tools-extra/test/clang-doc/templates.cpp b/clang-tools-extra/test/clang-doc/templates.cpp index 2e04a77ac9e62..4d4a25b8d3b82 100644 --- a/clang-tools-extra/test/clang-doc/templates.cpp +++ b/clang-tools-extra/test/clang-doc/templates.cpp @@ -18,6 +18,23 @@ void ParamPackFunction(T... args); // CHECK: --- // CHECK-NEXT: USR: '{{([0-9A-F]{40})}}' // CHECK-NEXT: ChildFunctions: +// CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' +// CHECK-NEXT: Name: 'ParamPackFunction' +// CHECK-NEXT: Location: +// CHECK-NEXT: - LineNumber: 16 +// CHECK-NEXT: Filename: '{{.*}}' +// CHECK-NEXT: Params: +// CHECK-NEXT: - Type: +// CHECK-NEXT: Name: 'T...' +// CHECK-NEXT: QualName: 'T...' +// CHECK-NEXT: Name: 'args' +// CHECK-NEXT: ReturnType: +// CHECK-NEXT: Type: +// CHECK-NEXT: Name: 'void' +// CHECK-NEXT: QualName: 'void' +// CHECK-NEXT: Template: +// CHECK-NEXT: Params: +// CHECK-NEXT: - Contents: 'class... T' // CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' // CHECK-NEXT: Name: 'function' // CHECK-NEXT: DefLocation: @@ -56,21 +73,4 @@ void ParamPackFunction(T... args); // CHECK-NEXT: Params: // CHECK-NEXT: - Contents: 'bool' // CHECK-NEXT: - Contents: '0' -// CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' -// CHECK-NEXT: Name: 'ParamPackFunction' -// CHECK-NEXT: Location: -// CHECK-NEXT: - LineNumber: 16 -// CHECK-NEXT: Filename: '{{.*}}' -// CHECK-NEXT: Params: -// CHECK-NEXT: - Type: -// CHECK-NEXT: Name: 'T...' -// CHECK-NEXT: QualName: 'T...' -// CHECK-NEXT: Name: 'args' -// CHECK-NEXT: ReturnType: -// CHECK-NEXT: Type: -// CHECK-NEXT: Name: 'void' -// CHECK-NEXT: QualName: 'void' -// CHECK-NEXT: Template: -// CHECK-NEXT: Params: -// CHECK-NEXT: - Contents: 'class... T' // CHECK-NEXT: ... >From 399c51e2c0cf6d53480fdc49c3342236677cf0c4 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 1 Aug 2024 05:35:23 -0400 Subject: [PATCH 4/4] [clang-doc] sort by location --- .../clang-doc/Representation.cpp | 7 ++++ clang-tools-extra/clang-doc/Representation.h | 19 +++++++++-- .../clang-doc/tool/ClangDocMain.cpp | 25 ++++++++++---- .../test/clang-doc/basic-project.test | 13 +++---- .../test/clang-doc/namespace.cpp | 4 +-- .../test/clang-doc/templates.cpp | 34 +++++++++---------- 6 files changed, 68 insertions(+), 34 deletions(-) diff --git a/clang-tools-extra/clang-doc/Representation.cpp b/clang-tools-extra/clang-doc/Representation.cpp index d08afbb962189..6ed18e8a91f5c 100644 --- a/clang-tools-extra/clang-doc/Representation.cpp +++ b/clang-tools-extra/clang-doc/Representation.cpp @@ -384,5 +384,12 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx, } } +void ScopeChildren::sort() { + std::sort(Namespaces.begin(), Namespaces.end()); + std::sort(Records.begin(), Records.end()); + std::sort(Functions.begin(), Functions.end()); + std::sort(Enums.begin(), Enums.end()); + std::sort(Typedefs.begin(), Typedefs.end()); +} } // namespace doc } // namespace clang diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h index d70c279f7a2bd..93145b9e9a779 100644 --- a/clang-tools-extra/clang-doc/Representation.h +++ b/clang-tools-extra/clang-doc/Representation.h @@ -104,6 +104,9 @@ struct Reference { bool mergeable(const Reference &Other); void merge(Reference &&I); + bool operator<(const Reference &Other) const { + return llvm::toStringRef(USR) < llvm::toStringRef(Other.USR); + } /// Returns the path for this Reference relative to CurrentPath. llvm::SmallString<64> getRelativeFilePath(const StringRef &CurrentPath) const; @@ -145,6 +148,9 @@ struct ScopeChildren { std::vector<FunctionInfo> Functions; std::vector<EnumInfo> Enums; std::vector<TypedefInfo> Typedefs; + + void sort(); + }; // A base struct for TypeInfos @@ -269,10 +275,13 @@ struct Info { Info(Info &&Other) = default; virtual ~Info() = default; + + Info& operator=(const Info &Other) = delete; + Info& operator=(Info &&Other) = default; SymbolID USR = SymbolID(); // Unique identifier for the decl described by this Info. - const InfoType IT = InfoType::IT_default; // InfoType of this particular Info. + InfoType IT = InfoType::IT_default; // InfoType of this particular Info. SmallString<16> Name; // Unqualified name of the decl. llvm::SmallVector<Reference, 4> Namespace; // List of parent namespaces for this decl. @@ -282,7 +291,7 @@ struct Info { void mergeBase(Info &&I); bool mergeable(const Info &Other); - + llvm::SmallString<16> extractName() const; /// Returns the file path for this Info relative to CurrentPath. @@ -312,6 +321,12 @@ struct SymbolInfo : public Info { std::optional<Location> DefLoc; // Location where this decl is defined. llvm::SmallVector<Location, 2> Loc; // Locations where this decl is declared. + + bool operator<(const SymbolInfo &Other) const { + assert(Loc.size() > 0 && Other.Loc.size() > 0 && + "SymbolInfo must have at least one location"); + return Loc[0] < Other.Loc[0]; + } }; // TODO: Expand to allow for documenting templating and default args. diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 9a540a5061b6a..48179bd3d5238 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -205,6 +205,22 @@ llvm::Error getHtmlAssetFiles(const char *Argv0, return getDefaultAssetFiles(Argv0, CDCtx); } +/// Make the output of clang-doc deterministic by sorting the children of +/// namespaces and records. +void sortUsrToInfo(llvm::StringMap<std::unique_ptr<doc::Info>>& USRToInfo) { + for (auto &I : USRToInfo) { + auto &Info = I.second; + if (Info->IT == doc::InfoType::IT_namespace) { + auto *Namespace = static_cast<clang::doc::NamespaceInfo *>(Info.get()); + Namespace->Children.sort(); + } + if (Info->IT == doc::InfoType::IT_record) { + auto *Record = static_cast<clang::doc::RecordInfo *>(Info.get()); + Record->Children.sort(); + } + } +} + int main(int argc, const char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(argv[0]); std::error_code OK; @@ -288,13 +304,6 @@ Example usage for a project using a compile commands database: R.first->second.emplace_back(Value); }); - for (auto &Entry : USRToBitcode) { - std::vector<llvm::StringRef> &Bitcode = Entry.second; - std::sort(Bitcode.begin(), Bitcode.end(), - [](const llvm::StringRef &A, const llvm::StringRef &B) { - return A < B; - }); - } // Collects all Infos according to their unique USR value. This map is added // to from the thread pool below and is protected by the USRToInfoMutex. llvm::sys::Mutex USRToInfoMutex; @@ -348,6 +357,8 @@ Example usage for a project using a compile commands database: if (Error) return 1; + sortUsrToInfo(USRToInfo); + // Ensure the root output directory exists. if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory); Err != std::error_code()) { diff --git a/clang-tools-extra/test/clang-doc/basic-project.test b/clang-tools-extra/test/clang-doc/basic-project.test index ee80493f6d7fa..2d8e3be4bd504 100644 --- a/clang-tools-extra/test/clang-doc/basic-project.test +++ b/clang-tools-extra/test/clang-doc/basic-project.test @@ -73,12 +73,12 @@ // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">add</h3> // HTML-CALC: <p>public int add(int a, int b)</p> // HTML-CALC: <p>Defined at line 3 of file {{.*}}Calculator.cpp</p> -// HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">multiply</h3> -// HTML-CALC: <p>public int multiply(int a, int b)</p> -// HTML-CALC: <p>Defined at line 11 of file {{.*}}Calculator.cpp</p> // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">subtract</h3> // HTML-CALC: <p>public int subtract(int a, int b)</p> // HTML-CALC: <p>Defined at line 7 of file {{.*}}Calculator.cpp</p> +// HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">multiply</h3> +// HTML-CALC: <p>public int multiply(int a, int b)</p> +// HTML-CALC: <p>Defined at line 11 of file {{.*}}Calculator.cpp</p> // HTML-CALC: <h3 id="{{([0-9A-F]{40})}}">divide</h3> // HTML-CALC: <p>public double divide(int a, int b)</p> // HTML-CALC: <p>Defined at line 15 of file {{.*}}Calculator.cpp</p> @@ -114,15 +114,16 @@ // HTML-CIRCLE: <h2 id="Members">Members</h2> // HTML-CIRCLE: <li>private double radius_</li> // HTML-CIRCLE: <h2 id="Functions">Functions</h2> +// HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">Circle</h3> +// HTML-CIRCLE: <p>public void Circle(double radius)</p> +// HTML-CIRCLE: <p>Defined at line 3 of file {{.*}}Circle.cpp</p> // HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">area</h3> // HTML-CIRCLE: <p>public double area()</p> // HTML-CIRCLE: <p>Defined at line 5 of file {{.*}}Circle.cpp</p> // HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">perimeter</h3> // HTML-CIRCLE: <p>public double perimeter()</p> // HTML-CIRCLE: <p>Defined at line 9 of file {{.*}}Circle.cpp</p> -// HTML-CIRCLE: <h3 id="{{([0-9A-F]{40})}}">Circle</h3> -// HTML-CIRCLE: <p>public void Circle(double radius)</p> -// HTML-CIRCLE: <p>Defined at line 3 of file {{.*}}Circle.cpp</p> + // MD-CALC: # class Calculator // MD-CALC: *Defined at .{{[\/]}}include{{[\/]}}Calculator.h#8* diff --git a/clang-tools-extra/test/clang-doc/namespace.cpp b/clang-tools-extra/test/clang-doc/namespace.cpp index 4881f942fe52a..72ed277a2c1e9 100644 --- a/clang-tools-extra/test/clang-doc/namespace.cpp +++ b/clang-tools-extra/test/clang-doc/namespace.cpp @@ -271,15 +271,15 @@ namespace AnotherNamespace { // HTML-GLOBAL-INDEX: <div id="main-content" class="col-xs-12 col-sm-9 col-md-8 main-content"> // HTML-GLOBAL-INDEX: <h1>Global Namespace</h1> // HTML-GLOBAL-INDEX: <h2 id="Namespaces">Namespaces</h2> +// HTML-GLOBAL-INDEX: <li>@nonymous_namespace</li> // HTML-GLOBAL-INDEX: <li>PrimaryNamespace</li> // HTML-GLOBAL-INDEX: <li>AnotherNamespace</li> -// HTML-GLOBAL-INDEX: <li>@nonymous_namespace</li> // MD-GLOBAL-INDEX: # Global Namespace // MD-GLOBAL-INDEX: ## Namespaces +// MD-GLOBAL-INDEX: * [@nonymous_namespace](..{{[\/]}}@nonymous_namespace{{[\/]}}index.md) // MD-GLOBAL-INDEX: * [PrimaryNamespace](..{{[\/]}}PrimaryNamespace{{[\/]}}index.md) // MD-GLOBAL-INDEX: * [AnotherNamespace](..{{[\/]}}AnotherNamespace{{[\/]}}index.md) -// MD-GLOBAL-INDEX: * [@nonymous_namespace](..{{[\/]}}@nonymous_namespace{{[\/]}}index.md) // MD-ALL-FILES: # All Files // MD-ALL-FILES: ## [@nonymous_namespace](@nonymous_namespace{{[\/]}}index.md) diff --git a/clang-tools-extra/test/clang-doc/templates.cpp b/clang-tools-extra/test/clang-doc/templates.cpp index 4d4a25b8d3b82..2e04a77ac9e62 100644 --- a/clang-tools-extra/test/clang-doc/templates.cpp +++ b/clang-tools-extra/test/clang-doc/templates.cpp @@ -18,23 +18,6 @@ void ParamPackFunction(T... args); // CHECK: --- // CHECK-NEXT: USR: '{{([0-9A-F]{40})}}' // CHECK-NEXT: ChildFunctions: -// CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' -// CHECK-NEXT: Name: 'ParamPackFunction' -// CHECK-NEXT: Location: -// CHECK-NEXT: - LineNumber: 16 -// CHECK-NEXT: Filename: '{{.*}}' -// CHECK-NEXT: Params: -// CHECK-NEXT: - Type: -// CHECK-NEXT: Name: 'T...' -// CHECK-NEXT: QualName: 'T...' -// CHECK-NEXT: Name: 'args' -// CHECK-NEXT: ReturnType: -// CHECK-NEXT: Type: -// CHECK-NEXT: Name: 'void' -// CHECK-NEXT: QualName: 'void' -// CHECK-NEXT: Template: -// CHECK-NEXT: Params: -// CHECK-NEXT: - Contents: 'class... T' // CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' // CHECK-NEXT: Name: 'function' // CHECK-NEXT: DefLocation: @@ -73,4 +56,21 @@ void ParamPackFunction(T... args); // CHECK-NEXT: Params: // CHECK-NEXT: - Contents: 'bool' // CHECK-NEXT: - Contents: '0' +// CHECK-NEXT: - USR: '{{([0-9A-F]{40})}}' +// CHECK-NEXT: Name: 'ParamPackFunction' +// CHECK-NEXT: Location: +// CHECK-NEXT: - LineNumber: 16 +// CHECK-NEXT: Filename: '{{.*}}' +// CHECK-NEXT: Params: +// CHECK-NEXT: - Type: +// CHECK-NEXT: Name: 'T...' +// CHECK-NEXT: QualName: 'T...' +// CHECK-NEXT: Name: 'args' +// CHECK-NEXT: ReturnType: +// CHECK-NEXT: Type: +// CHECK-NEXT: Name: 'void' +// CHECK-NEXT: QualName: 'void' +// CHECK-NEXT: Template: +// CHECK-NEXT: Params: +// CHECK-NEXT: - Contents: 'class... T' // CHECK-NEXT: ... _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits