Skip to content

Commit 08a9f99

Browse files
committed
[clang-doc] init mustache implementation
1 parent 00830fe commit 08a9f99

File tree

6 files changed

+154
-292
lines changed

6 files changed

+154
-292
lines changed

clang-tools-extra/clang-doc/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@ add_clang_library(clangDoc
88
BitcodeReader.cpp
99
BitcodeWriter.cpp
1010
ClangDoc.cpp
11-
FileHelpersClangDoc.cpp
1211
Generators.cpp
1312
HTMLGenerator.cpp
14-
HTMLMustacheGenerator.cpp
1513
Mapper.cpp
1614
MDGenerator.cpp
1715
Representation.cpp
1816
Serialize.cpp
1917
YAMLGenerator.cpp
20-
18+
HTMLMustacheGenerator.cpp
19+
2120
DEPENDS
2221
omp_gen
2322
ClangDriverOptions

clang-tools-extra/clang-doc/HTMLGenerator.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#include "Generators.h"
1010
#include "Representation.h"
11-
#include "FileHelpersClangDoc.h"
1211
#include "clang/Basic/Version.h"
1312
#include "llvm/ADT/StringExtras.h"
1413
#include "llvm/ADT/StringRef.h"
@@ -252,6 +251,46 @@ static void appendVector(std::vector<Derived> &&New,
252251
std::move(New.begin(), New.end(), std::back_inserter(Original));
253252
}
254253

254+
// Compute the relative path from an Origin directory to a Destination directory
255+
static SmallString<128> computeRelativePath(StringRef Destination,
256+
StringRef Origin) {
257+
// If Origin is empty, the relative path to the Destination is its complete
258+
// path.
259+
if (Origin.empty())
260+
return Destination;
261+
262+
// The relative path is an empty path if both directories are the same.
263+
if (Destination == Origin)
264+
return {};
265+
266+
// These iterators iterate through each of their parent directories
267+
llvm::sys::path::const_iterator FileI = llvm::sys::path::begin(Destination);
268+
llvm::sys::path::const_iterator FileE = llvm::sys::path::end(Destination);
269+
llvm::sys::path::const_iterator DirI = llvm::sys::path::begin(Origin);
270+
llvm::sys::path::const_iterator DirE = llvm::sys::path::end(Origin);
271+
// Advance both iterators until the paths differ. Example:
272+
// Destination = A/B/C/D
273+
// Origin = A/B/E/F
274+
// FileI will point to C and DirI to E. The directories behind them is the
275+
// directory they share (A/B).
276+
while (FileI != FileE && DirI != DirE && *FileI == *DirI) {
277+
++FileI;
278+
++DirI;
279+
}
280+
SmallString<128> Result; // This will hold the resulting path.
281+
// Result has to go up one directory for each of the remaining directories in
282+
// Origin
283+
while (DirI != DirE) {
284+
llvm::sys::path::append(Result, "..");
285+
++DirI;
286+
}
287+
// Result has to append each of the remaining directories in Destination
288+
while (FileI != FileE) {
289+
llvm::sys::path::append(Result, *FileI);
290+
++FileI;
291+
}
292+
return Result;
293+
}
255294

256295
// HTML generation
257296

@@ -1094,6 +1133,24 @@ static llvm::Error genIndex(const ClangDocContext &CDCtx) {
10941133
return llvm::Error::success();
10951134
}
10961135

1136+
static llvm::Error
1137+
copyFile(StringRef FilePath, StringRef OutDirectory) {
1138+
llvm::SmallString<128> PathWrite;
1139+
llvm::sys::path::native(OutDirectory, PathWrite);
1140+
llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
1141+
llvm::SmallString<128> PathRead;
1142+
llvm::sys::path::native(FilePath, PathRead);
1143+
std::error_code OK;
1144+
std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
1145+
if (FileErr != OK) {
1146+
return llvm::createStringError(llvm::inconvertibleErrorCode(),
1147+
"error creating file " +
1148+
llvm::sys::path::filename(FilePath) +
1149+
": " + FileErr.message() + "\n");
1150+
}
1151+
return llvm::Error::success();
1152+
}
1153+
10971154
llvm::Error HTMLGenerator::createResources(ClangDocContext &CDCtx) {
10981155
auto Err = serializeIndex(CDCtx);
10991156
if (Err)

0 commit comments

Comments
 (0)