clang-tools 23.0.0git
File.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file contains the implementations of file I/O utility functions
11/// used in clang-doc.
12///
13//===----------------------------------------------------------------------===//
14#include "File.h"
15#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
18namespace clang {
19namespace doc {
20
21llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory) {
22 llvm::SmallString<128> PathWrite;
23 llvm::sys::path::native(OutDirectory, PathWrite);
24 llvm::sys::path::append(PathWrite, llvm::sys::path::filename(FilePath));
25 llvm::SmallString<128> PathRead;
26 llvm::sys::path::native(FilePath, PathRead);
27 std::error_code FileErr = llvm::sys::fs::copy_file(PathRead, PathWrite);
28 if (FileErr) {
29 return llvm::createStringError(llvm::inconvertibleErrorCode(),
30 "error creating file " +
31 llvm::sys::path::filename(FilePath) +
32 ": " + FileErr.message() + "\n");
33 }
34 return llvm::Error::success();
35}
36
37llvm::SmallString<128> computeRelativePath(llvm::StringRef Destination,
38 llvm::StringRef Origin) {
39 // If Origin is empty, the relative path to the Destination is its complete
40 // path.
41 if (Origin.empty())
42 return Destination;
43
44 // The relative path is an empty path if both directories are the same.
45 if (Destination == Origin)
46 return {};
47
48 // These iterators iterate through each of their parent directories
49 llvm::sys::path::const_iterator FileI = llvm::sys::path::begin(Destination);
50 llvm::sys::path::const_iterator FileE = llvm::sys::path::end(Destination);
51 llvm::sys::path::const_iterator DirI = llvm::sys::path::begin(Origin);
52 llvm::sys::path::const_iterator DirE = llvm::sys::path::end(Origin);
53 // Advance both iterators until the paths differ. Example:
54 // Destination = A/B/C/D
55 // Origin = A/B/E/F
56 // FileI will point to C and DirI to E. The directories behind them is the
57 // directory they share (A/B).
58 while (FileI != FileE && DirI != DirE && *FileI == *DirI) {
59 ++FileI;
60 ++DirI;
61 }
62 llvm::SmallString<128> Result; // This will hold the resulting path.
63 // Result has to go up one directory for each of the remaining directories in
64 // Origin
65 while (DirI != DirE) {
66 llvm::sys::path::append(Result, "..");
67 ++DirI;
68 }
69 // Result has to append each of the remaining directories in Destination
70 while (FileI != FileE) {
71 llvm::sys::path::append(Result, *FileI);
72 ++FileI;
73 }
74 return Result;
75}
76
77} // namespace doc
78} // namespace clang
static llvm::cl::opt< std::string > OutDirectory("output", llvm::cl::desc("Directory for outputting generated files."), llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory))
This file contains file I/O utility functions used in clang-doc, such as creating directories and wri...
llvm::Error copyFile(llvm::StringRef FilePath, llvm::StringRef OutDirectory)
Definition File.cpp:21
llvm::SmallString< 128 > computeRelativePath(llvm::StringRef Destination, llvm::StringRef Origin)
Definition File.cpp:37
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//