|
| 1 | +/* |
| 2 | + * Sonar C++ Plugin (Community) |
| 3 | + * Copyright (C) 2010-2018 SonarOpenCommunity |
| 4 | + * http://github.com/SonarOpenCommunity/sonar-cxx |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program 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 GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.cxx.preprocessor; |
| 21 | + |
| 22 | +import static org.sonar.cxx.api.CxxTokenType.STRING; |
| 23 | + |
| 24 | +import java.net.URI; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +import javax.annotation.Nullable; |
| 33 | + |
| 34 | +import com.sonar.sslr.api.Token; |
| 35 | + |
| 36 | +public final class Macro { |
| 37 | + |
| 38 | + public final String name; |
| 39 | + public final List<Token> params; |
| 40 | + public final List<Token> body; |
| 41 | + public final boolean isVariadic; |
| 42 | + |
| 43 | + public Macro(String name, @Nullable List<Token> params, @Nullable List<Token> body, boolean variadic) { |
| 44 | + this.name = name; |
| 45 | + if (params == null) { |
| 46 | + this.params = null; |
| 47 | + } else { |
| 48 | + this.params = new ArrayList<>(params); |
| 49 | + } |
| 50 | + if (body == null) { |
| 51 | + this.body = null; |
| 52 | + } else { |
| 53 | + this.body = new ArrayList<>(body); |
| 54 | + } |
| 55 | + this.isVariadic = variadic; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Constructor for standard (predefined) macros |
| 60 | + * |
| 61 | + * @param name |
| 62 | + * @param body |
| 63 | + */ |
| 64 | + public Macro(String name, String body) { |
| 65 | + this.name = name; |
| 66 | + this.params = null; |
| 67 | + this.body = Collections.singletonList(Token.builder() |
| 68 | + .setLine(1) |
| 69 | + .setColumn(0) |
| 70 | + .setURI(URI.create("")) |
| 71 | + .setValueAndOriginalValue(body) |
| 72 | + .setType(STRING) |
| 73 | + .build()); |
| 74 | + this.isVariadic = false; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String toString() { |
| 79 | + String paramsStr = ""; |
| 80 | + if (params != null) { |
| 81 | + final String joinedParams = params.stream().map(Token::getValue).collect(Collectors.joining(", ")); |
| 82 | + paramsStr = "(" + joinedParams + (isVariadic ? "..." : "") + ")"; |
| 83 | + } |
| 84 | + String bodyStr = ""; |
| 85 | + if (body != null) { |
| 86 | + bodyStr = body.stream().map(Token::getValue).collect(Collectors.joining(" ")); |
| 87 | + } |
| 88 | + return name + paramsStr + " -> '" + bodyStr + "'"; |
| 89 | + } |
| 90 | + |
| 91 | + public boolean checkArgumentsCount(int count) { |
| 92 | + return isVariadic ? count >= params.size() - 1 : count == params.size(); |
| 93 | + } |
| 94 | + |
| 95 | + private static void add(Map<String, Macro> map, String name, String body) { |
| 96 | + map.put(name, new Macro(name, body)); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * This is a collection of standard macros according to |
| 101 | + * http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html |
| 102 | + */ |
| 103 | + private static final Map<String, Macro> STANDARD_MACROS_IMPL = new HashMap<>(); |
| 104 | + static { |
| 105 | + |
| 106 | + add(STANDARD_MACROS_IMPL, "__FILE__", "\"file\""); |
| 107 | + add(STANDARD_MACROS_IMPL, "__LINE__", "1"); |
| 108 | + // indicates 'date unknown'. should suffice |
| 109 | + add(STANDARD_MACROS_IMPL, "__DATE__", "\"??? ?? ????\""); |
| 110 | + // indicates 'time unknown'. should suffice |
| 111 | + add(STANDARD_MACROS_IMPL, "__TIME__", "\"??:??:??\""); |
| 112 | + add(STANDARD_MACROS_IMPL, "__STDC__", "1"); |
| 113 | + add(STANDARD_MACROS_IMPL, "__STDC_HOSTED__", "1"); |
| 114 | + add(STANDARD_MACROS_IMPL, "__cplusplus", "201103L"); |
| 115 | + // __has_include support (C++17) |
| 116 | + add(STANDARD_MACROS_IMPL, "__has_include", "1"); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Smaller set of defines as rest is provides by compilation unit settings |
| 121 | + */ |
| 122 | + private static final Map<String, Macro> UNIT_MACROS_IMPL = new HashMap<>(); |
| 123 | + static { |
| 124 | + add(UNIT_MACROS_IMPL, "__FILE__", "\"file\""); |
| 125 | + add(UNIT_MACROS_IMPL, "__LINE__", "1"); |
| 126 | + add(UNIT_MACROS_IMPL, "__DATE__", "\"??? ?? ????\""); |
| 127 | + add(UNIT_MACROS_IMPL, "__TIME__", "\"??:??:??\""); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Macros to replace C++ keywords when parsing C files |
| 132 | + */ |
| 133 | + private static Map<String, Macro> COMPATIBILITY_MACROS_IMPL = new HashMap<>(); |
| 134 | + static { |
| 135 | + // This is a collection of macros used to let C code be parsed by C++ parser |
| 136 | + add(COMPATIBILITY_MACROS_IMPL, "alignas", "__alignas"); |
| 137 | + add(COMPATIBILITY_MACROS_IMPL, "alignof", "__alignof"); |
| 138 | + add(COMPATIBILITY_MACROS_IMPL, "catch", "__catch"); |
| 139 | + add(COMPATIBILITY_MACROS_IMPL, "class", "__class"); |
| 140 | + add(COMPATIBILITY_MACROS_IMPL, "constexpr", "__constexpr"); |
| 141 | + add(COMPATIBILITY_MACROS_IMPL, "const_cast", "__const_cast"); |
| 142 | + add(COMPATIBILITY_MACROS_IMPL, "decltype", "__decltype"); |
| 143 | + add(COMPATIBILITY_MACROS_IMPL, "delete", "__delete"); |
| 144 | + add(COMPATIBILITY_MACROS_IMPL, "dynamic_cast", "__dynamic_cast"); |
| 145 | + add(COMPATIBILITY_MACROS_IMPL, "explicit", "__explicit"); |
| 146 | + add(COMPATIBILITY_MACROS_IMPL, "export", "__export"); |
| 147 | + add(COMPATIBILITY_MACROS_IMPL, "final", "__final"); |
| 148 | + add(COMPATIBILITY_MACROS_IMPL, "friend", "__friend"); |
| 149 | + add(COMPATIBILITY_MACROS_IMPL, "mutable", "__mutable"); |
| 150 | + add(COMPATIBILITY_MACROS_IMPL, "namespace", "__namespace"); |
| 151 | + add(COMPATIBILITY_MACROS_IMPL, "new", "__new"); |
| 152 | + add(COMPATIBILITY_MACROS_IMPL, "noexcept", "__noexcept"); |
| 153 | + add(COMPATIBILITY_MACROS_IMPL, "nullptr", "__nullptr"); |
| 154 | + add(COMPATIBILITY_MACROS_IMPL, "operator", "__operator"); |
| 155 | + add(COMPATIBILITY_MACROS_IMPL, "override", "__override"); |
| 156 | + add(COMPATIBILITY_MACROS_IMPL, "private", "__private"); |
| 157 | + add(COMPATIBILITY_MACROS_IMPL, "protected", "__protected"); |
| 158 | + add(COMPATIBILITY_MACROS_IMPL, "public", "__public"); |
| 159 | + add(COMPATIBILITY_MACROS_IMPL, "reinterpret_cast", "__reinterpret_cast"); |
| 160 | + add(COMPATIBILITY_MACROS_IMPL, "static_assert", "__static_assert"); |
| 161 | + add(COMPATIBILITY_MACROS_IMPL, "static_cast", "__static_cast"); |
| 162 | + add(COMPATIBILITY_MACROS_IMPL, "thread_local", "__thread_local"); |
| 163 | + add(COMPATIBILITY_MACROS_IMPL, "throw", "__throw"); |
| 164 | + add(COMPATIBILITY_MACROS_IMPL, "try", "__try"); |
| 165 | + add(COMPATIBILITY_MACROS_IMPL, "typeid", "__typeid"); |
| 166 | + add(COMPATIBILITY_MACROS_IMPL, "typename", "__typename"); |
| 167 | + add(COMPATIBILITY_MACROS_IMPL, "using", "__using"); |
| 168 | + add(COMPATIBILITY_MACROS_IMPL, "template", "__template"); |
| 169 | + add(COMPATIBILITY_MACROS_IMPL, "virtual", "__virtual"); |
| 170 | + } |
| 171 | + |
| 172 | + public static final Map<String, Macro> STANDARD_MACROS = Collections.unmodifiableMap(STANDARD_MACROS_IMPL); |
| 173 | + public static final Map<String, Macro> UNIT_MACROS = Collections.unmodifiableMap(UNIT_MACROS_IMPL); |
| 174 | + public static final Map<String, Macro> COMPATIBILITY_MACROS = Collections.unmodifiableMap(COMPATIBILITY_MACROS_IMPL); |
| 175 | + |
| 176 | +} |
0 commit comments