History

tccpp: Implement __BASE_FILE__ macro

Like __FILE__ but always refers to the command line file name also from inside headers.

Michael Matz committed 10 years ago

Commit b7e0b691 parent 8a1a2a63 files · +22 −0Browse files

Unified · Split

3 files
  1. tccpp.c +6 −0
  2. tests/tcctest.c +7 −0
  3. tests/tcctest.h +9 −0
tccpp.c+6−0
@@ -3465,6 +3465,7 @@ ST_INLN void unget_tok(int last_tok) · 3464 unmodified lines
34653465
34663466 ST_FUNC void preprocess_start(TCCState *s1)
34673467 {
3468+ char *buf;
34683469 s1->include_stack_ptr = s1->include_stack;
34693470 /* XXX: move that before to avoid having to initialize
34703471 file->ifdef_stack_ptr ? */
@@ -3480,6 +3481,11 @@ ST_FUNC void preprocess_start(TCCState *s1) · 9 unmodified lines
34803481 s1->dollars_in_identifiers ? IS_ID : 0;
34813482 isidnum_table['.' - CH_EOF] =
34823483 (parse_flags & PARSE_FLAG_ASM_FILE) ? IS_ID : 0;
3484+ buf = tcc_malloc(3 + strlen(file->filename));
3485+ sprintf(buf, "\"%s\"", file->filename);
3486+ tcc_undefine_symbol(s1, "__BASE_FILE__");
3487+ tcc_define_symbol(s1, "__BASE_FILE__", buf);
3488+ tcc_free(buf);
34833489 if (s1->nb_cmd_include_files) {
34843490 CString cstr;
34853491 int i;
tests/tcctest.c+7−0
@@ -59,6 +59,8 @@ · 58 unmodified lines
5959
6060 #include "tcclib.h"
6161
62+#include "tcctest.h"
63+
6264 void intdiv_test();
6365 void string_test();
6466 void expr_test();
@@ -389,6 +391,11 @@ comment · 324 unmodified lines
389391 /* And again when the name and parenthes are separated by a
390392 comment. */
391393 TEST2 /* the comment */ ();
394+
395+ printf("%s\n", get_basefile_from_header());
396+ printf("%s\n", __BASE_FILE__);
397+ printf("%s\n", get_file_from_header());
398+ printf("%s\n", __FILE__);
392399 }
393400
394401
tests/tcctest.h+9−0
@@ -0,0 +1,9 @@
1+static inline const char *get_basefile_from_header(void)
2+{
3+ return __BASE_FILE__;
4+}
5+
6+static inline const char *get_file_from_header(void)
7+{
8+ return __FILE__;
9+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t