All files / transpiler/output MisraSuppressionUtils.ts

100% Statements 12/12
100% Branches 6/6
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                      22x                   22x               5x 5x 3x                 85x 85x 22x 22x     22x            
/**
 * MISRA Suppression Utilities
 *
 * Issue #850: Shared helpers for emitting MISRA inline suppression comments.
 * Used by both CodeGenerator (for .c files) and HeaderGeneratorUtils (for .h files).
 */
 
/**
 * Headers that violate MISRA C:2012 rules and need inline suppression.
 * Maps header name to the MISRA rule it violates.
 */
const MISRA_BANNED_HEADERS: ReadonlyMap<string, string> = new Map([
  // MISRA Rule 21.6: Standard library I/O functions shall not be used
  ["stdio.h", "misra-c2012-21.6"],
]);
 
/**
 * Regex to extract header name from angle-bracket includes.
 * Uses possessive matching via atomic group simulation to avoid backtracking.
 * Matches: #include <header.h> -> captures "header.h"
 */
const ANGLE_BRACKET_INCLUDE_REGEX = /<([^<>]+)>/;
 
/**
 * Check if an include directive needs MISRA suppression.
 * @param includeText The full include directive (e.g., "#include <stdio.h>")
 * @returns true if suppression is needed
 */
function needsMisraSuppression(includeText: string): boolean {
  const match = ANGLE_BRACKET_INCLUDE_REGEX.exec(includeText);
  if (!match) return false;
  return MISRA_BANNED_HEADERS.has(match[1]);
}
 
/**
 * Get the MISRA suppression comment for an include directive.
 * @param includeText The full include directive (e.g., "#include <stdio.h>")
 * @returns The suppression comment, or null if not needed
 */
function getMisraSuppressionComment(includeText: string): string | null {
  const match = ANGLE_BRACKET_INCLUDE_REGEX.exec(includeText);
  if (!match) return null;
  const rule = MISRA_BANNED_HEADERS.get(match[1]);
  return rule ? `// cppcheck-suppress ${rule}` : null;
}
 
const MisraSuppressionUtils = {
  needsMisraSuppression,
  getMisraSuppressionComment,
};
 
export default MisraSuppressionUtils;