Skip to content

Commit d226f3a

Browse files
committed
chore: Absolute compilation database
1 parent 66aedd6 commit d226f3a

File tree

4 files changed

+196
-6
lines changed

4 files changed

+196
-6
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// This is a derivative work. originally part of the LLVM Project.
3+
// Licensed 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+
// Copyright (c) 2023 Vinnie Falco ([email protected])
8+
//
9+
// Official repository: https://github.com/cppalliance/mrdox
10+
//
11+
12+
#include "Support/Debug.hpp"
13+
#include "Support/Path.hpp"
14+
#include "AST/AbsoluteCompilationDatabase.hpp"
15+
#include <llvm/Support/FileSystem.h>
16+
#include <llvm/Support/raw_ostream.h>
17+
18+
namespace clang {
19+
namespace mrdox {
20+
21+
AbsoluteCompilationDatabase::
22+
AbsoluteCompilationDatabase(
23+
llvm::StringRef workingDir,
24+
CompilationDatabase const& inner)
25+
{
26+
namespace fs = llvm::sys::fs;
27+
namespace path = llvm::sys::path;
28+
29+
auto allCommands = inner.getAllCompileCommands();
30+
AllCommands_.reserve(allCommands.size());
31+
SmallPathString temp;
32+
for(auto const& cmd0 : allCommands)
33+
{
34+
tooling::CompileCommand cmd;
35+
36+
cmd.CommandLine = cmd0.CommandLine;
37+
cmd.Heuristic = cmd0.Heuristic;
38+
cmd.Output = cmd0.Output;
39+
cmd.CommandLine = cmd0.CommandLine;
40+
41+
if(path::is_absolute(cmd0.Directory))
42+
{
43+
path::native(cmd0.Directory, temp);
44+
cmd.Directory = static_cast<std::string>(temp);
45+
}
46+
else
47+
{
48+
temp = cmd0.Directory;
49+
fs::make_absolute(workingDir, temp);
50+
path::remove_dots(temp, true);
51+
cmd.Directory = static_cast<std::string>(temp);
52+
}
53+
54+
if(path::is_absolute(cmd0.Filename))
55+
{
56+
path::native(cmd0.Filename, temp);
57+
cmd.Filename = static_cast<std::string>(temp);
58+
}
59+
else
60+
{
61+
temp = cmd0.Filename;
62+
fs::make_absolute(workingDir, temp);
63+
path::remove_dots(temp, true);
64+
cmd.Filename = static_cast<std::string>(temp);
65+
}
66+
67+
std::size_t i = AllCommands_.size();
68+
auto result = IndexByFile_.try_emplace(cmd.Filename, i);
69+
AllCommands_.emplace_back(std::move(cmd));
70+
Assert(result.second);
71+
}
72+
}
73+
74+
std::vector<tooling::CompileCommand>
75+
AbsoluteCompilationDatabase::
76+
getCompileCommands(
77+
llvm::StringRef FilePath) const
78+
{
79+
SmallPathString nativeFilePath;
80+
llvm::sys::path::native(FilePath, nativeFilePath);
81+
82+
auto const it = IndexByFile_.find(nativeFilePath);
83+
if (it == IndexByFile_.end())
84+
return {};
85+
std::vector<tooling::CompileCommand> Commands;
86+
Commands.push_back(AllCommands_[it->getValue()]);
87+
return Commands;
88+
}
89+
90+
std::vector<std::string>
91+
AbsoluteCompilationDatabase::
92+
getAllFiles() const
93+
{
94+
std::vector<std::string> allFiles;
95+
allFiles.reserve(AllCommands_.size());
96+
for(auto const& cmd : AllCommands_)
97+
allFiles.push_back(cmd.Filename);
98+
return allFiles;
99+
}
100+
101+
std::vector<tooling::CompileCommand>
102+
AbsoluteCompilationDatabase::
103+
getAllCompileCommands() const
104+
{
105+
return AllCommands_;
106+
}
107+
108+
} // mrdox
109+
} // clang
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2023 Vinnie Falco ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdox
9+
//
10+
11+
#ifndef MRDOX_LIB_AST_ABSOLUTECOMPILATIONDATABASE_HPP
12+
#define MRDOX_LIB_AST_ABSOLUTECOMPILATIONDATABASE_HPP
13+
14+
#include <clang/Tooling/JSONCompilationDatabase.h>
15+
#include <llvm/ADT/StringMap.h>
16+
#include <vector>
17+
18+
namespace clang {
19+
namespace mrdox {
20+
21+
/** A compilation database where all paths are absolute.
22+
23+
All relative paths in the compilation database
24+
will be converted to absolute paths by resolving
25+
them according to the working directory specified
26+
at construction.
27+
*/
28+
class AbsoluteCompilationDatabase
29+
: public tooling::CompilationDatabase
30+
{
31+
std::vector<tooling::CompileCommand> AllCommands_;
32+
llvm::StringMap<std::size_t> IndexByFile_;
33+
34+
public:
35+
/** Constructor.
36+
37+
This copies the contents of the source compilation
38+
database. Every relative path is converted into an
39+
absolute path by resolving against the specified
40+
working directory.
41+
*/
42+
AbsoluteCompilationDatabase(
43+
llvm::StringRef workingDir,
44+
CompilationDatabase const& inner);
45+
46+
std::vector<tooling::CompileCommand>
47+
getCompileCommands(
48+
llvm::StringRef FilePath) const override;
49+
50+
std::vector<std::string>
51+
getAllFiles() const override;
52+
53+
std::vector<tooling::CompileCommand>
54+
getAllCompileCommands() const override;
55+
};
56+
57+
} // mrdox
58+
} // clang
59+
60+
#endif
61+

source/GenerateAction.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
#include "Options.hpp"
1313
#include "ConfigImpl.hpp"
1414
#include "CorpusImpl.hpp"
15+
#include "AST/AbsoluteCompilationDatabase.hpp"
1516
#include <mrdox/Generators.hpp>
17+
#include <mrdox/Support/Report.hpp>
1618
#include <clang/Tooling/AllTUsExecution.h>
1719
#include <clang/Tooling/JSONCompilationDatabase.h>
20+
#include <llvm/Support/Path.h>
1821
#include <cstdlib>
1922

2023
namespace clang {
@@ -23,6 +26,8 @@ namespace mrdox {
2326
Error
2427
DoGenerateAction()
2528
{
29+
namespace path = llvm::sys::path;
30+
2631
auto& generators = getGenerators();
2732

2833
// Calculate additional YAML settings from command line options.
@@ -45,16 +50,23 @@ DoGenerateAction()
4550
return Error("the compilation database path argument is missing");
4651
if(InputPaths.size() > 1)
4752
return Error("got {} input paths where 1 was expected", InputPaths.size());
53+
llvm::StringRef compilationsPath = InputPaths.front();
4854
std::string errorMessage;
49-
auto compilations = tooling::JSONCompilationDatabase::loadFromFile(
50-
InputPaths.front(), errorMessage, tooling::JSONCommandLineSyntax::AutoDetect);
51-
if(! compilations)
55+
auto jsonCompilations = tooling::JSONCompilationDatabase::loadFromFile(
56+
compilationsPath, errorMessage, tooling::JSONCommandLineSyntax::AutoDetect);
57+
if(! jsonCompilations)
5258
return Error(std::move(errorMessage));
5359

60+
// Calculate the working directory
61+
llvm::SmallString<240> workingDir(compilationsPath);
62+
path::remove_filename(workingDir);
63+
64+
// Convert relative paths to absolute
65+
AbsoluteCompilationDatabase compilations(workingDir, *jsonCompilations);
66+
5467
// Create the ToolExecutor from the compilation database
5568
int ThreadCount = 0;
56-
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(
57-
*compilations, ThreadCount);
69+
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(compilations, ThreadCount);
5870

5971
// Create the generator
6072
auto generator = generators.find(FormatType.getValue());
@@ -68,7 +80,7 @@ DoGenerateAction()
6880

6981
// Run the generator.
7082
if(config.get()->verboseOutput)
71-
llvm::outs() << "Generating docs...\n";
83+
reportInfo("Generating docs...\n");
7284
return generator->build(OutputPath.getValue(), **corpus);
7385
}
7486

source/Support/Path.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
#define MRDOX_LIB_SUPPORT_PATH_HPP
1414

1515
#include <mrdox/Platform.hpp>
16+
#include <llvm/ADT/SmallString.h>
1617
#include <llvm/ADT/StringRef.h>
1718
#include <llvm/Support/Path.h>
1819

1920
namespace clang {
2021
namespace mrdox {
2122

23+
/** A reasonably sized small string for paths.
24+
25+
This is for local variables not for use
26+
as data members of long-lived types.
27+
*/
28+
using SmallPathString = llvm::SmallString<340>;
29+
2230
/** Replaces backslashes with slashes if Windows in place.
2331
2432
@param path A path that is transformed to native format.

0 commit comments

Comments
 (0)