|
| 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 "ToolIntelFortran.h" |
| 21 | +#include "Common.h" |
| 22 | +#include "Parsers.h" |
| 23 | +#include "libsys/Path.h" |
| 24 | + |
| 25 | +#include <regex> |
| 26 | +#include <utility> |
| 27 | +#include <functional> |
| 28 | +#include <set> |
| 29 | +#include <string_view> |
| 30 | + |
| 31 | +using namespace cs::semantic; |
| 32 | + |
| 33 | +namespace { |
| 34 | + Arguments create_argument_list(const Execution& execution) |
| 35 | + { |
| 36 | + Arguments input_arguments; |
| 37 | + std::copy(execution.arguments.begin(), execution.arguments.end(), std::back_inserter(input_arguments)); |
| 38 | + return input_arguments; |
| 39 | + } |
| 40 | + |
| 41 | + bool is_preprocessor(const CompilerFlags& flags) |
| 42 | + { |
| 43 | + return std::any_of(flags.begin(), flags.end(), [](const auto& flag) { |
| 44 | + const std::string& candidate = flag.arguments.front(); |
| 45 | + |
| 46 | + static const std::set<std::string_view> NO_COMPILATION_FLAG = { "-preprocess-only", "-P", "-E", "-Ep" }; |
| 47 | + |
| 48 | + return ((flag.type == CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING) && (NO_COMPILATION_FLAG.find(candidate) != NO_COMPILATION_FLAG.end())) |
| 49 | + || ((flag.type == CompilerFlagType::PREPROCESSOR_MAKE)); |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +namespace cs::semantic { |
| 55 | + |
| 56 | + const FlagsByName ToolIntelFortran::FLAG_DEFINITION = { |
| 57 | + {"-c", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 58 | + {"-S", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 59 | + {"-E", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 60 | + {"-Ep", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 61 | + {"-preprocess-only", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 62 | + {"-P", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_NO_LINKING}}, |
| 63 | + {"-o", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::KIND_OF_OUTPUT_OUTPUT}}, |
| 64 | + {"-debug", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 65 | + {"-debug-parameters", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 66 | + {"@", {MatchInstruction::PREFIX, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 67 | + {"-Fa", {MatchInstruction::PREFIX, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 68 | + {"-FA", {MatchInstruction::PREFIX, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 69 | + {"-shared", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT}}, |
| 70 | + {"-dryrun", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 71 | + {"-dumpmachine", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 72 | + {"-v", {MatchInstruction::PREFIX, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 73 | + {"-V", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 74 | + {"--help", {MatchInstruction::PREFIX, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 75 | + {"--version", {MatchInstruction::EXACTLY, CompilerFlagType::KIND_OF_OUTPUT_INFO}}, |
| 76 | + {"-D", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::PREPROCESSOR}}, |
| 77 | + {"-U", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::PREPROCESSOR}}, |
| 78 | + {"-include", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::PREPROCESSOR}}, |
| 79 | + {"-undef", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR}}, |
| 80 | + {"-pthread", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR}}, |
| 81 | + {"-MD", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR_MAKE}}, |
| 82 | + {"-MMD", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR_MAKE}}, |
| 83 | + {"-MF", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::PREPROCESSOR_MAKE}}, |
| 84 | + {"-gen-dep", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::PREPROCESSOR_MAKE}}, |
| 85 | + {"-C", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR}}, |
| 86 | + {"-Xoption,cpp", {MatchInstruction::PREFIX, CompilerFlagType::PREPROCESSOR}}, |
| 87 | + {"-Xoption,fpp", {MatchInstruction::PREFIX, CompilerFlagType::PREPROCESSOR}}, |
| 88 | + {"-fpp", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR}}, |
| 89 | + {"-nofpp", {MatchInstruction::EXACTLY, CompilerFlagType::PREPROCESSOR}}, |
| 90 | + {"-Wp", {MatchInstruction::PREFIX, CompilerFlagType::PREPROCESSOR}}, |
| 91 | + {"-I", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 92 | + {"-iquote", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 93 | + {"-isystem", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 94 | + {"-isysroot", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 95 | + {"-L", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::DIRECTORY_SEARCH_LINKER}}, |
| 96 | + {"--sysroot", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_WITH_EQ, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 97 | + {"-X", {MatchInstruction::EXACTLY, CompilerFlagType::DIRECTORY_SEARCH}}, |
| 98 | + {"-l", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_OR_SEP, CompilerFlagType::LINKER}}, |
| 99 | + {"-nostartfiles", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 100 | + {"-nodefaultlibs", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 101 | + {"-nostdlib", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 102 | + {"-r", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 103 | + {"-s", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 104 | + {"-shared-intel", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 105 | + {"shared-libgcc", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 106 | + {"-static", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 107 | + {"-static-intel", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 108 | + {"-static-libgcc", {MatchInstruction::EXACTLY, CompilerFlagType::LINKER}}, |
| 109 | + {"-T", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::LINKER}}, |
| 110 | + {"-Xlinker", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::LINKER}}, |
| 111 | + {"-Xoption,link", {MatchInstruction::PREFIX, CompilerFlagType::LINKER}}, |
| 112 | + {"-u", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::LINKER}}, |
| 113 | + {"-Wl", {MatchInstruction::PREFIX, CompilerFlagType::LINKER}}, |
| 114 | + {"-Xoption,asm", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 115 | + {"-std", {MatchInstruction::EXACTLY_WITH_1_OPT_GLUED_WITH_EQ, CompilerFlagType::OTHER}}, |
| 116 | + {"-O", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 117 | + {"-g", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 118 | + {"-f", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 119 | + {"-m", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 120 | + {"-x", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 121 | + {"-diag-", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 122 | + {"-no", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 123 | + {"-gen-interfaces", {MatchInstruction::EXACTLY_WITH_1_OPT_SEP, CompilerFlagType::OTHER}}, |
| 124 | + {"-nogen-interfaces", {MatchInstruction::EXACTLY, CompilerFlagType::OTHER}}, |
| 125 | + {"--", {MatchInstruction::PREFIX, CompilerFlagType::OTHER}}, |
| 126 | + }; |
| 127 | + |
| 128 | + rust::Result<SemanticPtr> ToolIntelFortran::recognize(const Execution& execution) const |
| 129 | + { |
| 130 | + if (is_compiler_call(execution.executable)) { |
| 131 | + return compilation_impl(FLAG_DEFINITION, execution, create_argument_list, is_preprocessor); |
| 132 | + } |
| 133 | + return rust::Ok(SemanticPtr()); |
| 134 | + } |
| 135 | + |
| 136 | + bool ToolIntelFortran::is_compiler_call(const fs::path& program) const |
| 137 | + { |
| 138 | + static const auto pattern = std::regex(R"(^([^-]*-)*(ifx|ifort)(-?\w+(\.\d+){0,2})?$)"); |
| 139 | + |
| 140 | + std::cmatch m; |
| 141 | + return std::regex_match(program.filename().c_str(), m, pattern); |
| 142 | + } |
| 143 | +} |
0 commit comments