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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 44x 44x 44x 44x 43x 44x 44x 44x 44x 29x 43x 29x 29x 17x 29x 8x 8x 8x 29x 8x 29x 18x 43x 43x 43x 34x 51x 34x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 51x 28x | /**
* StructCollector - Collects struct and union symbols from C parse trees.
*/
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { StructOrUnionSpecifierContext } from "../../../parser/c/grammar/CParser";
import type ICStructSymbol from "../../../../types/symbols/c/ICStructSymbol";
import type ICFieldInfo from "../../../../types/symbols/c/ICFieldInfo";
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import SymbolTable from "../../SymbolTable";
import SymbolUtils from "../../SymbolUtils";
import DeclaratorUtils from "../utils/DeclaratorUtils";
/**
* Options for struct collection.
* Consolidates optional parameters to avoid exceeding 7-parameter limit.
*/
interface ICollectOptions {
/** Optional typedef name for anonymous structs */
typedefName?: string;
/** Whether this is part of a typedef declaration */
isTypedef?: boolean;
/** Array to collect warnings */
warnings?: string[];
/** Issue #957: True if typedef has pointer declarator (typedef struct X *Y) */
isPointerTypedef?: boolean;
}
/** Options for symbol table updates */
interface IUpdateOptions {
needsStructKeyword: boolean;
hasBody: boolean;
isTypedef?: boolean;
typedefName?: string;
structTag?: string;
/** Issue #957: True if typedef has pointer declarator */
isPointerTypedef?: boolean;
}
class StructCollector {
/**
* Collect a struct or union symbol from a specifier context.
*
* @param structSpec The struct/union specifier context
* @param sourceFile Source file path
* @param line Source line number
* @param symbolTable Optional symbol table for field tracking
* @param options Optional collection options (typedef info, warnings)
*/
static collect(
structSpec: StructOrUnionSpecifierContext,
sourceFile: string,
line: number,
symbolTable: SymbolTable | null,
options: ICollectOptions = {},
): ICStructSymbol | null {
const { typedefName, isTypedef, warnings, isPointerTypedef } = options;
const identifier = structSpec.Identifier();
// Use typedef name for anonymous structs (e.g., typedef struct { ... } AppConfig;)
const name = identifier?.getText() || typedefName;
if (!name) return null; // Skip if no name available
const isUnion = structSpec.structOrUnion()?.getText() === "union";
// Issue #948: Detect forward declaration (struct with no body)
const hasBody = structSpec.structDeclarationList() !== null;
// Extract fields if struct has a body
const fields = StructCollector.collectFields(
structSpec,
name,
symbolTable,
warnings,
);
// Mark named structs that are not typedef'd - they need 'struct' keyword
// Example: "struct NamedPoint { ... };" -> needs "struct NamedPoint var"
// But "typedef struct { ... } Rectangle;" -> just "Rectangle var"
const needsStructKeyword = Boolean(identifier && !isTypedef);
if (symbolTable) {
StructCollector.updateSymbolTable(symbolTable, name, sourceFile, {
needsStructKeyword,
hasBody,
isTypedef,
typedefName,
structTag: identifier?.getText(),
isPointerTypedef,
});
}
return {
kind: "struct",
name,
sourceFile,
sourceLine: line,
sourceLanguage: ESourceLanguage.C,
isExported: true,
isUnion,
needsStructKeyword,
fields: fields.size > 0 ? fields : undefined,
};
}
/**
* Update symbol table with struct metadata.
* Extracted to reduce cognitive complexity of collect().
*/
private static updateSymbolTable(
symbolTable: SymbolTable,
name: string,
sourceFile: string,
options: IUpdateOptions,
): void {
const {
needsStructKeyword,
hasBody,
isTypedef,
typedefName,
structTag,
isPointerTypedef,
} = options;
if (needsStructKeyword) {
symbolTable.markNeedsStructKeyword(name);
}
// Issue #948: Track opaque types (forward-declared typedef structs)
// Issue #957: Don't mark pointer typedefs as opaque.
// For "typedef struct X *Y", Y is already a pointer type, not an opaque struct.
if (isTypedef && !hasBody && typedefName && !isPointerTypedef) {
symbolTable.markOpaqueType(typedefName);
Eif (structTag) {
symbolTable.registerStructTagAlias(structTag, typedefName);
}
}
// Issue #958: Track forward-declared typedef struct types (no body).
// These always need pointer semantics (ADR-006). Inline typedefs with
// bodies (typedef struct { ... } X;) are concrete value types.
// Issue #957: Don't track pointer typedefs - they're already pointers.
if (isTypedef && !hasBody && typedefName && !isPointerTypedef) {
symbolTable.markTypedefStructType(typedefName, sourceFile);
}
// Issue #958: Record struct tag body for query-time opaque resolution
if (hasBody && structTag) {
symbolTable.markStructTagHasBody(structTag);
}
}
/**
* Collect fields from a struct/union definition.
*/
private static collectFields(
structSpec: StructOrUnionSpecifierContext,
structName: string,
symbolTable: SymbolTable | null,
warnings?: string[],
): ReadonlyMap<string, ICFieldInfo> {
const fields = new Map<string, ICFieldInfo>();
const declList = structSpec.structDeclarationList();
if (!declList) return fields;
for (const structDecl of declList.structDeclaration()) {
StructCollector.collectFieldsFromDecl(
structDecl,
structName,
fields,
symbolTable,
warnings,
);
}
return fields;
}
/**
* Collect fields from a single struct declaration.
*/
private static collectFieldsFromDecl(
structDecl: any,
structName: string,
fields: Map<string, ICFieldInfo>,
symbolTable: SymbolTable | null,
warnings?: string[],
): void {
const specQualList = structDecl.specifierQualifierList?.();
Iif (!specQualList) return;
const fieldType = DeclaratorUtils.extractTypeFromSpecQualList(specQualList);
const structDeclList = structDecl.structDeclaratorList?.();
Iif (!structDeclList) return;
for (const structDeclarator of structDeclList.structDeclarator()) {
StructCollector.processFieldDeclarator(
structDeclarator,
structName,
fieldType,
fields,
symbolTable,
warnings,
);
}
}
/**
* Process a single field declarator and add to fields map.
*/
private static processFieldDeclarator(
structDeclarator: any,
structName: string,
fieldType: string,
fields: Map<string, ICFieldInfo>,
symbolTable: SymbolTable | null,
warnings?: string[],
): void {
const declarator = structDeclarator.declarator?.();
Iif (!declarator) return;
const fieldName = DeclaratorUtils.extractDeclaratorName(declarator);
Iif (!fieldName) return;
Iif (warnings && SymbolUtils.isReservedFieldName(fieldName)) {
warnings.push(
SymbolUtils.getReservedFieldWarning("C", structName, fieldName),
);
}
const arrayDimensions = DeclaratorUtils.extractArrayDimensions(declarator);
const fieldInfo: ICFieldInfo = {
name: fieldName,
type: fieldType,
arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
};
fields.set(fieldName, fieldInfo);
if (symbolTable) {
symbolTable.addStructField(
structName,
fieldName,
fieldType,
arrayDimensions.length > 0 ? arrayDimensions : undefined,
);
}
}
}
export default StructCollector;
|