|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# |
| 4 | +# Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +# See https://llvm.org/LICENSE.txt for license information. |
| 6 | +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +# |
| 8 | +# Copyright (c) 2024 Alan de Freitas ([email protected]) |
| 9 | +# |
| 10 | +# Official repository: https://github.com/cppalliance/mrdocs |
| 11 | +# |
| 12 | + |
| 13 | +# |
| 14 | +# This script is used to check that all Info nodes defined in |
| 15 | +# include/Metadata/InfoNodes.inc are supported in all steps |
| 16 | +# of MrDocs. |
| 17 | +# |
| 18 | +# Although most of that requirement is enforced in C++ source |
| 19 | +# code, it is useful to check if these node types are also being |
| 20 | +# included in templates and tests. |
| 21 | +# |
| 22 | + |
| 23 | +# Path of the current script |
| 24 | +SCRIPT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")" |
| 25 | + |
| 26 | +# Directory of the current script |
| 27 | +SCRIPT_DIR="$(dirname "$SCRIPT_PATH")" |
| 28 | + |
| 29 | +# Parent directory of the current script |
| 30 | +MRDOCS_ROOT="$(dirname "$SCRIPT_DIR")" |
| 31 | + |
| 32 | +# InfoNodes.inc path |
| 33 | +INFO_NODES_INC="$MRDOCS_ROOT/include/mrdocs/Metadata/InfoNodes.inc" |
| 34 | + |
| 35 | +# Determine if we should use github actions |
| 36 | +if [ -n "$GITHUB_ACTIONS" ]; then |
| 37 | + GITHUB_ACTIONS=true |
| 38 | +else |
| 39 | + GITHUB_ACTIONS=false |
| 40 | +fi |
| 41 | + |
| 42 | +# Function to print a message |
| 43 | +# If GITHUB_ACTIONS is true, print the message in directed to >> $GITHUB_STEP_SUMMARY |
| 44 | +# Otherwise, print the message to stdout |
| 45 | +function print_message() { |
| 46 | + if $GITHUB_ACTIONS; then |
| 47 | + echo "$1" >> "$GITHUB_STEP_SUMMARY" |
| 48 | + else |
| 49 | + echo "$1" |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +# A function that calls print_message if print_message hasn't been called |
| 54 | +# with that message already |
| 55 | +declare -A print_message_once_messages |
| 56 | +function print_message_once() { |
| 57 | + local message="$1" |
| 58 | + if [[ -z "${print_message_once_messages[$message]}" ]]; then |
| 59 | + print_message_once_messages[$message]=1 |
| 60 | + echo "$message" |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +# Parse InfoNodes.inc |
| 65 | +table_content=$(grep -E '^INFO\(' "$INFO_NODES_INC" | sed -E 's/^INFO\(([^)]*)\).*$/\1/') |
| 66 | +# echo "$table_content" |
| 67 | + |
| 68 | +print_message "# Info Nodes Support report" |
| 69 | +while IFS= read -r line; do |
| 70 | + # Split the line into components using ',' as delimiter |
| 71 | + IFS=',' read -r -a components <<< "$line" |
| 72 | + |
| 73 | + # Access individual components |
| 74 | + name="${components[0]}" |
| 75 | + plural="${components[1]}" |
| 76 | + uppercase="${components[2]}" |
| 77 | + lowercase="${components[3]}" |
| 78 | + lowercase_plural="${components[4]}" |
| 79 | + description="${components[5]}" |
| 80 | + |
| 81 | + # Trim leading and trailing spaces from each component |
| 82 | + name=$(echo "$name" | xargs) |
| 83 | + plural=$(echo "$plural" | xargs) |
| 84 | + uppercase=$(echo "$uppercase" | xargs) |
| 85 | + lowercase=$(echo "$lowercase" | xargs) |
| 86 | + lowercase_plural=$(echo "$lowercase_plural" | xargs) |
| 87 | + description=$(echo "$description" | xargs) |
| 88 | + |
| 89 | + # Do something with the components |
| 90 | + # print_message "## Name: $name" |
| 91 | + # print_message "Plural: $plural" |
| 92 | + # print_message "Uppercase: $uppercase" |
| 93 | + # print_message "Lowercase: $lowercase" |
| 94 | + # print_message "Lowercase plural: $lowercase_plural" |
| 95 | + # print_message "Description: $description" |
| 96 | + |
| 97 | + # `include/mrdocs/Metadata/X.h` and `src/mrdocs/Metadata/X.cpp` should |
| 98 | + # be defined |
| 99 | + METADATA_INCLUDE_DIR="$MRDOCS_ROOT/include/mrdocs/Metadata" |
| 100 | + if [ ! -f "$METADATA_INCLUDE_DIR/$name.hpp" ]; then |
| 101 | + print_message_once "## ${name}Info" |
| 102 | + print_message "* include/mrdocs/Metadata/$name.h not found" |
| 103 | + fi |
| 104 | + |
| 105 | + # src/lib/AST/ASTVisitor.cpp should have a `build$name()` function |
| 106 | + # Look for the string `build$name(` in the file |
| 107 | + if ! grep -q "build$name(" "$MRDOCS_ROOT/src/lib/AST/ASTVisitor.cpp"; then |
| 108 | + print_message_once "## ${name}Info" |
| 109 | + print_message "* \`build$name()\` not found in \`src/lib/AST/ASTVisitor.cpp\`" |
| 110 | + fi |
| 111 | + |
| 112 | + # `src/lib/AST/AnyBlock.hpp` should define the `${name}Block` type |
| 113 | + # Look for the string `${name}Block` in the file |
| 114 | + if ! grep -q "${name}Block" "$MRDOCS_ROOT/src/lib/AST/AnyBlock.hpp"; then |
| 115 | + print_message_once "## ${name}Info" |
| 116 | + print_message "* \`${name}Block\` not found in \`src/lib/AST/AnyBlock.hpp\`" |
| 117 | + fi |
| 118 | + |
| 119 | + # `src/lib/AST/BitcodeWriter.hpp` should define `void emitBlock(X const& I);` |
| 120 | + # Look for the string `void emitBlock(${name}Info const& I);` in the file |
| 121 | + # When looking for the string `void emitBlock(${name}Info const& I);` in the file, |
| 122 | + # we need to use regex to account for the following: |
| 123 | + # 1) Remove the leading and trailing spaces |
| 124 | + # 2) Consecutive spaces should be replaced with a single space |
| 125 | + # 3) Newlines should be removed |
| 126 | + # 4) The variable name `I` can be anything |
| 127 | + BITCODE_WRITE_REL_PATH="src/lib/AST/BitcodeWriter.hpp" |
| 128 | + BITCODE_WRITE_PATH="$MRDOCS_ROOT/$BITCODE_WRITE_REL_PATH" |
| 129 | + regex="void[[:space:]]+emitBlock[[:space:]]*\(${name}Info[[:space:]]*const[[:space:]]*&[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*\);" |
| 130 | + if ! grep -Pzo "$regex" "$BITCODE_WRITE_PATH" > /dev/null; then |
| 131 | + print_message_once "## ${name}Info" |
| 132 | + print_message "The function \`void emitBlock(${name}Info const& I);\` is not defined in \`$BITCODE_WRITE_REL_PATH\`." |
| 133 | + fi |
| 134 | + |
| 135 | + # `src/lib/Gen/xml/XMLWriter.cpp` should define `XMLWriter::writeX()` |
| 136 | + # Just look for the string `write$name` in this file |
| 137 | + if ! grep -q "write$name" "$MRDOCS_ROOT/src/lib/Gen/xml/XMLWriter.cpp"; then |
| 138 | + print_message_once "## ${name}Info" |
| 139 | + print_message "* \`write$name\` not found in \`src/lib/Gen/xml/XMLWriter.cpp\`" |
| 140 | + fi |
| 141 | + |
| 142 | + # Function `void merge(${name}Info& I, ${name}Info& Other)` should be defined in |
| 143 | + # `src/lib/Metadata/DomMetadata.cpp`. |
| 144 | + # Like with the other function, we just use regex to look for this function, |
| 145 | + # ignoring consecutive whitespaces, newlines, leading and trailing spaces, and variable names. |
| 146 | + MERGE_REL_PATH="src/lib/Metadata/Reduce.cpp" |
| 147 | + MERGE_PATH="$MRDOCS_ROOT/$MERGE_REL_PATH" |
| 148 | + regex="void[[:space:]]+merge[[:space:]]*\(${name}Info[[:space:]]*&[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*[[:space:]]*,[[:space:]]*${name}Info[[:space:]]*&&[[:space:]]*[a-zA-Z_][a-zA-Z0-9_]*\)" |
| 149 | + if ! grep -Pzo "$regex" "$MERGE_PATH" > /dev/null; then |
| 150 | + print_message_once "## ${name}Info" |
| 151 | + print_message "The function \`void merge(${name}Info& I, ${name}Info&& Other)\` is not defined in \`$MERGE_REL_PATH\`." |
| 152 | + fi |
| 153 | + |
| 154 | + # `src/lib/Support/SafeNames.cpp` should have safe name support for $name |
| 155 | + # Look the string matching the regex `"\d+$lowercase"` in the file. |
| 156 | + # Note: the regex is looking for a string that literally quoted. |
| 157 | + SAFE_NAMES_REL_PATH="src/lib/Support/SafeNames.cpp" |
| 158 | + SAFE_NAMES_PATH="$MRDOCS_ROOT/$SAFE_NAMES_REL_PATH" |
| 159 | + regex="\"[0-9]+${lowercase}\"" |
| 160 | + if ! grep -qE "$regex" "$SAFE_NAMES_PATH"; then |
| 161 | + print_message_once "## ${name}Info" |
| 162 | + print_message "* \`$regex\` not found in \`$SAFE_NAMES_REL_PATH\`" |
| 163 | + fi |
| 164 | + |
| 165 | + # `include/mrdocs/mrdocs.natvis` should include the ${name}Info type |
| 166 | + # We look for the string `<AlternativeType Name="clang::mrdocs::${name}Info"/>` in the file |
| 167 | + if ! grep -q "<AlternativeType Name=\"clang::mrdocs::${name}Info\"/>" "$MRDOCS_ROOT/include/mrdocs/mrdocs.natvis"; then |
| 168 | + print_message_once "## ${name}Info" |
| 169 | + print_message "* \`<AlternativeType Name=\"clang::mrdocs::${name}Info\"/>\` not found in \`include/mrdocs/mrdocs.natvis\`" |
| 170 | + fi |
| 171 | + |
| 172 | + # `mrdocs.rnc` should include the type ${name} in multiple places |
| 173 | + # At least look for the string ${name} in the file |
| 174 | + if ! grep -q "${name}" "$MRDOCS_ROOT/mrdocs.rnc"; then |
| 175 | + print_message_once "## ${name}Info" |
| 176 | + print_message "* \`${name}\` not found in \`mrdocs.rnc\`" |
| 177 | + fi |
| 178 | + |
| 179 | + # There should be template files for each info type |
| 180 | + # `share/mrdocs/addons/generator/asciidoc/partials/signature/${lowercase}.adoc.hbs` |
| 181 | + GENERATOR_REL_DIR="share/mrdocs/addons/generator" |
| 182 | + ASCIIDOC_REL_DIR="$GENERATOR_REL_DIR/asciidoc" |
| 183 | + ASCIIDOC_SIGNATURE_REL_DIR="$ASCIIDOC_REL_DIR/partials/signature" |
| 184 | + ASCIIDOC_SIGNATURE_DIR="$MRDOCS_ROOT/$ASCIIDOC_SIGNATURE_REL_DIR" |
| 185 | + if [ ! -f "$ASCIIDOC_SIGNATURE_DIR/${lowercase}.adoc.hbs" ]; then |
| 186 | + print_message_once "## ${name}Info" |
| 187 | + print_message "* \`$ASCIIDOC_SIGNATURE_REL_DIR/${lowercase}.adoc.hbs\` not found" |
| 188 | + fi |
| 189 | + # `share/mrdocs/addons/generator/asciidoc/partials/symbols/${lowercase}.adoc.hbs` |
| 190 | + ASCIIDOC_SYMBOLS_REL_DIR="$ASCIIDOC_REL_DIR/partials/symbols" |
| 191 | + ASCIIDOC_SYMBOLS_DIR="$MRDOCS_ROOT/$ASCIIDOC_SYMBOLS_REL_DIR" |
| 192 | + if [ ! -f "$ASCIIDOC_SYMBOLS_DIR/${lowercase}.adoc.hbs" ]; then |
| 193 | + print_message_once "## ${name}Info" |
| 194 | + print_message "* \`$ASCIIDOC_SYMBOLS_REL_DIR/${lowercase}.adoc.hbs\` not found" |
| 195 | + fi |
| 196 | + # `share/mrdocs/addons/generator/html/partials/signature/${lowercase}.html.hbs` |
| 197 | + HTML_REL_DIR="$GENERATOR_REL_DIR/html" |
| 198 | + HTML_SIGNATURE_REL_DIR="$HTML_REL_DIR/partials/signature" |
| 199 | + HTML_SIGNATURE_DIR="$MRDOCS_ROOT/$HTML_SIGNATURE_REL_DIR" |
| 200 | + if [ ! -f "$HTML_SIGNATURE_DIR/${lowercase}.html.hbs" ]; then |
| 201 | + print_message_once "## ${name}Info" |
| 202 | + print_message "* \`$HTML_SIGNATURE_REL_DIR/${lowercase}.html.hbs\` not found" |
| 203 | + fi |
| 204 | + # `share/mrdocs/addons/generator/html/partials/symbols/${lowercase}.html.hbs` |
| 205 | + HTML_SYMBOLS_REL_DIR="$HTML_REL_DIR/partials/symbols" |
| 206 | + HTML_SYMBOLS_DIR="$MRDOCS_ROOT/$HTML_SYMBOLS_REL_DIR" |
| 207 | + if [ ! -f "$HTML_SYMBOLS_DIR/${lowercase}.html.hbs" ]; then |
| 208 | + print_message_once "## ${name}Info" |
| 209 | + print_message "* \`$HTML_SYMBOLS_REL_DIR/${lowercase}.html.hbs\` not found" |
| 210 | + fi |
| 211 | + |
| 212 | + # We want to know if |
| 213 | + # `share/mrdocs/addons/generator/asciidoc/partials/symbols/tranche.adoc.hbs` |
| 214 | + # includes the ${name}Info type somewhere as `members=tranche.${lowercase_plural}` |
| 215 | + ASCIIDOC_PARTIALS_REL_DIR="$ASCIIDOC_REL_DIR/partials" |
| 216 | + ASCIIDOC_PARTIALS_DIR="$MRDOCS_ROOT/$ASCIIDOC_PARTIALS_REL_DIR" |
| 217 | + if ! grep -q "tranche.${lowercase_plural}" "$ASCIIDOC_PARTIALS_DIR/tranche.adoc.hbs"; then |
| 218 | + INCLUDED_IN_ASCIIDOC_TRANCHE="true" |
| 219 | + else |
| 220 | + INCLUDED_IN_ASCIIDOC_TRANCHE="false" |
| 221 | + fi |
| 222 | + # We want to know if |
| 223 | + # `share/mrdocs/addons/generator/html/partials/symbols/tranche.html.hbs` |
| 224 | + # includes the ${name}Info type somewhere as `{{>info-list tranche.${lowercase_plural}}}` |
| 225 | + HTML_PARTIALS_REL_DIR="$HTML_REL_DIR/partials" |
| 226 | + HTML_PARTIALS_DIR="$MRDOCS_ROOT/$HTML_PARTIALS_REL_DIR" |
| 227 | + if ! grep -q "{{>info-list tranche.${lowercase_plural}}" "$HTML_PARTIALS_DIR/tranche.html.hbs"; then |
| 228 | + INCLUDED_IN_HTML_TRANCHE="true" |
| 229 | + else |
| 230 | + INCLUDED_IN_HTML_TRANCHE="false" |
| 231 | + fi |
| 232 | + |
| 233 | + # Check if it's included in one tranche but not the other |
| 234 | + if [ "$INCLUDED_IN_ASCIIDOC_TRANCHE" = "true" ] && [ "$INCLUDED_IN_HTML_TRANCHE" = "false" ]; then |
| 235 | + print_message_once "## ${name}Info" |
| 236 | + print_message "* \`members=tranche.${lowercase_plural}\` not included in \`$ASCIIDOC_PARTIALS_REL_DIR/tranche.adoc.hbs\` but included in \`tranche.html.hbs\`" |
| 237 | + fi |
| 238 | + if [ "$INCLUDED_IN_HTML_TRANCHE" = "true" ] && [ "$INCLUDED_IN_ASCIIDOC_TRANCHE" = "false" ]; then |
| 239 | + print_message_once "## ${name}Info" |
| 240 | + print_message "* \`{{>info-list tranche.${lowercase_plural}}\` not included in \`$HTML_PARTIALS_REL_DIR/tranche.html.hbs\` but included in \`tranche.adoc.hbs\`" |
| 241 | + fi |
| 242 | + |
| 243 | +done <<< "$table_content" |
| 244 | + |
| 245 | +# Check if $print_message_once_messages is empty |
| 246 | +if [ ${#print_message_once_messages[@]} -eq 0 ]; then |
| 247 | + print_message_once "No issues found" |
| 248 | +fi |
0 commit comments