Skip to content

Commit

Permalink
[GNU] Add "#pragma once"
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent d48d9e5 commit a6c6622
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
11 changes: 11 additions & 0 deletions preprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct Hideset {

static HashMap macros;
static CondIncl *cond_incl;
static HashMap pragma_once;

static Token *preprocess2(Token *tok);
static Macro *find_macro(Token *tok);
Expand Down Expand Up @@ -781,6 +782,10 @@ static char *detect_include_guard(Token *tok) {
}

static Token *include_file(Token *tok, char *path, Token *filename_tok) {
// Check for "#pragma once"
if (hashmap_get(&pragma_once, path))
return tok;

// If we read the same file before, and if the file was guarded
// by the usual #ifndef ... #endif pattern, we may be able to
// skip the file without opening it.
Expand Down Expand Up @@ -939,6 +944,12 @@ static Token *preprocess2(Token *tok) {
continue;
}

if (equal(tok, "pragma") && equal(tok->next, "once")) {
hashmap_put(&pragma_once, tok->file->name, (void *)1);
tok = skip_line(tok->next->next);
continue;
}

if (equal(tok, "pragma")) {
do {
tok = tok->next;
Expand Down
10 changes: 10 additions & 0 deletions test/pragma-once.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "test.h"

#pragma once

#include "test/pragma-once.c"

int main() {
printf("OK\n");
return 0;
}

0 comments on commit a6c6622

Please sign in to comment.