|
| 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 (vinnie.falco@gmail.com) |
| 8 | +// |
| 9 | +// Official repository: https://github.com/cppalliance/mrdocs |
| 10 | +// |
| 11 | + |
| 12 | +#include "MrDocsSettingsDB.hpp" |
| 13 | +#include <mrdocs/Support/Path.hpp> |
| 14 | + |
| 15 | +namespace clang { |
| 16 | +namespace mrdocs { |
| 17 | + |
| 18 | +MrDocsSettingsDB::MrDocsSettingsDB(ConfigImpl const& config) |
| 19 | +{ |
| 20 | + std::vector<std::string> sourceFiles; |
| 21 | + auto& s = config.settings(); |
| 22 | + for (auto const& curInput: s.input) |
| 23 | + { |
| 24 | + forEachFile( |
| 25 | + curInput, |
| 26 | + s.recursive, |
| 27 | + [&](std::string_view path) -> Expected<void> { |
| 28 | + if (files::isDirectory(path)) |
| 29 | + { |
| 30 | + return {}; |
| 31 | + } |
| 32 | + // Check file patterns that should match |
| 33 | + auto inputFilename = files::getFileName(path); |
| 34 | + if (std::ranges::none_of( |
| 35 | + s.filePatterns, |
| 36 | + [&](PathGlobPattern const& pattern) { |
| 37 | + return pattern.match(inputFilename); |
| 38 | + })) |
| 39 | + { |
| 40 | + return {}; |
| 41 | + } |
| 42 | + // Check file or directories in path that should be skipped |
| 43 | + if (std::ranges::any_of(s.exclude, [&](std::string const& excludePath) { |
| 44 | + return files::startsWith(path, excludePath); |
| 45 | + })) |
| 46 | + { |
| 47 | + return {}; |
| 48 | + } |
| 49 | + // Check if the relative path matches any of the s.excludePatterns |
| 50 | + if (std::ranges::any_of( |
| 51 | + s.excludePatterns, |
| 52 | + [&](PathGlobPattern const& pattern) { |
| 53 | + return pattern.match(path); |
| 54 | + })) |
| 55 | + { |
| 56 | + return {}; |
| 57 | + } |
| 58 | + sourceFiles.emplace_back(path); |
| 59 | + return {}; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + for (auto const& pathName: sourceFiles) |
| 64 | + { |
| 65 | + // auto fileName = files::getFileName(pathName); |
| 66 | + auto parentDir = files::getParentDir(pathName); |
| 67 | + |
| 68 | + std::vector<std::string> cmds; |
| 69 | + cmds.emplace_back("clang"); |
| 70 | + cmds.emplace_back("-fsyntax-only"); |
| 71 | + cmds.emplace_back("-std=c++23"); |
| 72 | + cmds.emplace_back("-pedantic-errors"); |
| 73 | + cmds.emplace_back("-Werror"); |
| 74 | + cmds.emplace_back("-x"); |
| 75 | + cmds.emplace_back("c++"); |
| 76 | + cmds.emplace_back(pathName); |
| 77 | + cc_.emplace_back(parentDir, pathName, std::move(cmds), parentDir); |
| 78 | + cc_.back().Heuristic = "generated from mrdocs.yml"; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +std::vector<tooling::CompileCommand> |
| 83 | +MrDocsSettingsDB::getCompileCommands(llvm::StringRef FilePath) const |
| 84 | +{ |
| 85 | + std::vector<tooling::CompileCommand> result; |
| 86 | + for (auto const& cmd: cc_) |
| 87 | + { |
| 88 | + if (cmd.Filename == FilePath) |
| 89 | + { |
| 90 | + result.push_back(cmd); |
| 91 | + } |
| 92 | + } |
| 93 | + return result; |
| 94 | +} |
| 95 | + |
| 96 | +std::vector<std::string> |
| 97 | +MrDocsSettingsDB::getAllFiles() const |
| 98 | +{ |
| 99 | + std::vector<std::string> result; |
| 100 | + result.reserve(cc_.size()); |
| 101 | + for (auto const& cmd: cc_) |
| 102 | + { |
| 103 | + result.push_back(cmd.Filename); |
| 104 | + } |
| 105 | + return result; |
| 106 | +} |
| 107 | + |
| 108 | +std::vector<tooling::CompileCommand> |
| 109 | +MrDocsSettingsDB::getAllCompileCommands() const |
| 110 | +{ |
| 111 | + return cc_; |
| 112 | +} |
| 113 | + |
| 114 | +} // namespace mrdocs |
| 115 | +} // namespace clang |
0 commit comments