| 995 | } |
| 996 | |
| 997 | static void tm_directive(tm_scan_t *sc, const tm_tok_t *tok) { |
| 998 | tm_tok_t word; |
| 999 | tm_lex_t rest; |
| 1000 | tm_directive_word(tok, &word, &rest, sc->lx.incomplete); |
| 1001 | if (tm_tok_is(&word, "if") || tm_tok_is(&word, "ifdef") || tm_tok_is(&word, "ifndef")) { |
| 1002 | bool guard = false; |
| 1003 | tm_tok_t condition; |
| 1004 | tm_next(&rest, &condition); |
| 1005 | if (condition.kind == TM_TOK_EOF || |
| 1006 | (!tm_tok_is(&word, "if") && condition.kind != TM_TOK_IDENT)) { |
| 1007 | sc->m->incomplete = true; |
| 1008 | } |
| 1009 | if (tm_tok_is(&word, "ifndef")) { |
| 1010 | /* Only a guard that starts the file at outermost scope conditions |
| 1011 | * nothing. The same pair anywhere else is the define-if-undefined |
| 1012 | * idiom: an ordinary condition, so what it encloses is conditional. */ |
| 1013 | guard = condition.kind == TM_TOK_IDENT && !sc->seen_token && !sc->depth && |
| 1014 | sc->open == TM_OPEN_NONE && !sc->cond_count && |
| 1015 | tm_is_include_guard(sc, &condition); |
| 1016 | } |
| 1017 | if (sc->cond_count < TM_COND_MAX) { |
| 1018 | sc->cond[sc->cond_count] = (tm_cond_t){.depth = sc->depth, .guard = guard}; |
| 1019 | } else { |
| 1020 | sc->m->incomplete = true; |
| 1021 | } |
| 1022 | sc->cond_count++; |
| 1023 | sc->conditions += guard ? 0 : 1; |
| 1024 | } else if (tm_tok_is(&word, "elif") || tm_tok_is(&word, "else")) { |
| 1025 | /* Every branch starts from the brace depth the #if stood at. */ |
| 1026 | if (tm_tok_is(&word, "elif")) { |
| 1027 | tm_tok_t expression; |
| 1028 | tm_next(&rest, &expression); |
| 1029 | if (expression.kind == TM_TOK_EOF) { |
| 1030 | sc->m->incomplete = true; |
| 1031 | } |
| 1032 | } |
| 1033 | if (sc->cond_count > 0 && sc->cond_count <= TM_COND_MAX) { |
| 1034 | tm_cond_t *condition = &sc->cond[sc->cond_count - 1]; |
| 1035 | if (condition->seen_else || condition->guard) { |
| 1036 | /* A guard-like conditional with alternatives is not an |
| 1037 | * unconditional include guard. Its old rows are diagnostic. */ |
| 1038 | sc->m->incomplete = true; |
| 1039 | } |
| 1040 | condition->seen_else = tm_tok_is(&word, "else"); |
| 1041 | sc->depth = condition->depth; |
| 1042 | } else { |
| 1043 | sc->m->incomplete = true; |
| 1044 | } |
| 1045 | } else if (tm_tok_is(&word, "endif")) { |
| 1046 | if (sc->cond_count > 0) { |
| 1047 | sc->cond_count--; |
| 1048 | bool guard = sc->cond_count < TM_COND_MAX && sc->cond[sc->cond_count].guard; |
| 1049 | sc->conditions -= guard ? 0 : 1; |
| 1050 | if (guard && sc->cond_count == 0) { |
| 1051 | sc->guard_closed = true; |
| 1052 | } |
| 1053 | } else { |
| 1054 | sc->m->incomplete = true; |
no test coverage detected