|
| 1 | +/* Copyright (C) 2012-2024 by László Nagy |
| 2 | + This file is part of Bear. |
| 3 | +
|
| 4 | + Bear is a tool to generate compilation database for clang tooling. |
| 5 | +
|
| 6 | + Bear is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + Bear is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "ToolCrayFtnfe.h" |
| 21 | +#include "Common.h" |
| 22 | + |
| 23 | +#include <regex> |
| 24 | +#include <set> |
| 25 | + |
| 26 | +using namespace cs::semantic; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | + Arguments create_argument_list(const Execution& execution) |
| 31 | + { |
| 32 | + Arguments input_arguments; |
| 33 | + std::copy(execution.arguments.begin(), execution.arguments.end(), std::back_inserter(input_arguments)); |
| 34 | + return input_arguments; |
| 35 | + } |
| 36 | + |
| 37 | + bool is_preprocessor(const CompilerFlags& flags) |
| 38 | + { |
| 39 | + return std::any_of(flags.begin(), flags.end(), [](const auto& flag) { |
| 40 | + const std::string& candidate = flag.arguments.front(); |
| 41 | + static const std::set<std::string_view> NO_COMPILATION_FLAG = { "-E", "-eZ", "-e Z", "-eP", "-e P" }; |
| 42 | + return ((flag.type == CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING) && (NO_COMPILATION_FLAG.find(candidate) != NO_COMPILATION_FLAG.end())) |
| 43 | + || ((flag.type == CompilerFlagType::PREPROCESSOR_MAKE)); |
| 44 | + }); |
| 45 | + return false; |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +namespace cs::semantic { |
| 51 | + |
| 52 | + const FlagsByName ToolCrayFtnfe::FLAG_DEFINITION = { |
| 53 | + { "-add-rpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 54 | + { "-add-rpath-shared", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 55 | + { "-add-runpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 56 | + { "-as-needed", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 57 | + { "--as-needed", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 58 | + { "-A", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 59 | + { "-b", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::KIND_OF_OUTPUT_OUTPUT } }, |
| 60 | + { "-c", { MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING } }, |
| 61 | + { "--custom-ld-script=", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED, CompilerFlagType::LINKER } }, |
| 62 | + { "-d", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 63 | + { "-D", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::PREPROCESSOR } }, |
| 64 | + { "-e", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 65 | + { "-E", { MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING } }, |
| 66 | + { "-f", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 67 | + { "-F", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 68 | + { "-g", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 69 | + { "-gcc-rpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 70 | + { "-G", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 71 | + { "-h", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 72 | + { "-I", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH } }, |
| 73 | + { "-J", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH } }, |
| 74 | + { "-K", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 75 | + { "-l", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::LINKER } }, |
| 76 | + { "-L", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH_LINKER } }, |
| 77 | + { "-m", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 78 | + { "-M", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 79 | + { "-no-add-rpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 80 | + { "-no-add-rpath-shared", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 81 | + { "-no-add-runpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 82 | + { "-no-as-needed", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 83 | + { "--no-as-needed", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 84 | + { "--no-custom-ld-script", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 85 | + { "-no-gcc-rpath", { MatchInstruction::EXACTLY, CompilerFlagType::LINKER } }, |
| 86 | + { "-N", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 87 | + { "-O", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED, CompilerFlagType::OTHER } }, |
| 88 | + { "-o", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::KIND_OF_OUTPUT_OUTPUT } }, |
| 89 | + { "-p", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH } }, |
| 90 | + { "-Q", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH } }, |
| 91 | + { "-r", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::KIND_OF_OUTPUT } }, |
| 92 | + { "-R", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 93 | + { "-s", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 94 | + { "-S", { MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING } }, |
| 95 | + { "-T", { MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_INFO } }, |
| 96 | + { "-target-accel=", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED, CompilerFlagType::OTHER } }, |
| 97 | + { "-target-cpu=", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED, CompilerFlagType::OTHER } }, |
| 98 | + { "-target-network=", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED, CompilerFlagType::OTHER } }, |
| 99 | + { "-U", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::PREPROCESSOR } }, |
| 100 | + { "-v", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 101 | + { "-V", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 102 | + { "-W", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 103 | + { "-x", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 104 | + { "-Y", { MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::OTHER } }, |
| 105 | + { "-openmp", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 106 | + { "-noopenmp", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 107 | + { "-mp", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 108 | + { "-Mnoopenmp", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 109 | + { "-qno-openmp", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 110 | + { "-dynamic", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 111 | + { "-shared", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 112 | + { "-static", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 113 | + { "-default64", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 114 | + { "-VV", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 115 | + { "-VVV", { MatchInstruction::EXACTLY, CompilerFlagType::OTHER } }, |
| 116 | + { "-cray", { MatchInstruction::PREFIX, CompilerFlagType::OTHER } }, |
| 117 | + { "--cray", { MatchInstruction::PREFIX, CompilerFlagType::OTHER } }, |
| 118 | + }; |
| 119 | + |
| 120 | + rust::Result<SemanticPtr> ToolCrayFtnfe::recognize(const Execution& execution) const |
| 121 | + { |
| 122 | + if (is_compiler_call(execution.executable)) { |
| 123 | + return compilation_impl(FLAG_DEFINITION, execution, create_argument_list, is_preprocessor); |
| 124 | + } |
| 125 | + return rust::Ok(SemanticPtr()); |
| 126 | + } |
| 127 | + |
| 128 | + bool ToolCrayFtnfe::is_compiler_call(const fs::path& program) const |
| 129 | + { |
| 130 | + static const auto pattern = std::regex(R"(^([^-]*-)*(ftnfe)(-?\w+(\.\d+){0,2})?$)"); |
| 131 | + std::cmatch m; |
| 132 | + return std::regex_match(program.filename().c_str(), m, pattern); |
| 133 | + } |
| 134 | +} |
0 commit comments