Commit Graph

213 Commits (6c468c10f70d74e962591a0584765e42ce1bcaf2)

Author SHA1 Message Date
grischka 6c468c10f7 tccpp: allow "#define X defined Y ..."
That means: we do not macro-expand the argument of 'defined'

Also: store the last token position into the TokenString
structure in order to get rid of the tok_last function.
2017-07-09 11:46:14 +02:00
Michael Matz 824dcebe59 tccpp: Implement __COUNTER__
This requires one more change in how macro arguments are expanded:
the standard requires that macro args are expanded before substituting
into the replacement list.  This implies expanding them only once
even when they occur multiple times in the list.  TCC expanded
them repeatedly in that case.  Without __COUNTER__ that's harmless.

So, simply always expand arguments (when used without # and ##) once
and store the resulting tokens.
2017-07-09 05:30:47 +02:00
Michael Matz 1f3d3957c4 tccpp: Cleanup
With last change the can_read_stream flag is unneeded, so tidy this
a bit.
2017-07-09 04:53:24 +02:00
Michael Matz d8fdd380f3 tccpp: Fix corner case
See added testcase 19.c from a bug report.  The problem is reading outside
the arguments buffers, even though we aren't allowed to.  The single
can_read_stream variable is not enough, sometimes we need to be able
to pop into outer contexts but not into arbitrarily outside contexts.

The trick is to terminate argument tokens with a EOF (and not just with
0 that makes us pop contexts), and deal with that in the few places
we have to,

This enables some cleanups of the can_read_stream variable use.
2017-07-09 04:38:56 +02:00
grischka 28435ec58c configure: --config-musl/-uClibc switch & misc cleanups
- configure:
  - add --config-uClibc,-musl switch and suggest to use
    it if uClibc/musl is detected
  - make warning options magic clang compatible
  - simplify (use $confvars instead of individual options)
- Revert "Remove some unused-parameter lint"
  7443db0d5f
  rather use -Wno-unused-parameter (or just not -Wextra)
- #ifdef functions that are unused on some targets
- tccgen.c: use PTR_SIZE==8 instead of (X86_64 || ARM64)
- tccpe.c: fix some warnings
- integrate dummy arm-asm better
2017-05-13 08:59:06 +02:00
Larry Doolittle 7443db0d5f Remove some unused-parameter lint
Mark TCCState parameter as unused in tcc_undefine_symbol(), tcc_add_symbol(),
tcc_print_stats(), asm_get_local_label_name(), use_section1(), tccpp_delete(),
tcc_tool_ar(), tcc_tool_impdef(), and tcc_tool_cross().
Also mark it unused in tcc_add_bcheck() unless CONFIG_TCC_BCHECK.
Remove it entirely in ld_next().
2017-05-08 08:30:43 -07:00
Larry Doolittle 19d8b8a173 Spelling fixes in C comments only 2017-05-07 21:38:09 -07:00
Michael Matz 8ca98e23c4 Tidy unary() a bit
factor code a bit for transforming tokens into SValues.  This revealed
a bug in TOK_GET (see testcase), which happened to be harmless before.
So fix that as well.
2017-05-02 03:07:37 +02:00
Larry Doolittle 37999a4fbf Simple-minded fix for bug #50847
bug #50847: #line directive corrupts #include search path
Keep a second copy of the filename, that isn't changed by the #line directive,
and use that on the initial search path for #include files.
2017-04-25 12:32:41 -07:00
grischka 7acf9aa862 final adjustments for release
- configure/Makefiles: minor adjustments

- build-tcc.bat: add -static to gcc options
  (avoids libgcc_s*.dll dependency with some mingw versions)

- tccpe.c/tcctools.c: eliminate MAX_PATH
  (not available for cross compilers)

- tccasm.c: use uint64_t/strtoull in unary()
  (unsigned long sometimes is only uint32_t, as always on windows)

- tccgen.c: Revert (f077d16c) "tccgen: gen_cast: cast FLOAT to DOUBLE"
  Was a rather experimental, tentative commit, not really necessary
  and somewhat ugly too.

- cleanup recent osx support:
  - Makefile/libtcc.c: cleanup copy&paste code
  - tccpp.c: restore deleted function
2017-04-25 21:01:54 +02:00
Andrei Warkentin 63b2f907bd tcc: fixup clang warnings
The O(xxx) stuff in i386-asm.c had me scratching my head. Extracting
the macro and trying it out in a separate program doesn't give
me any warnings, so I'm confused about what could be going on there.
Any cast will make things happy. I used a uint64_t to catch actual
cases of overflow, which will still cause a -Wconstant-conversion
warning.

Signed-off-by: Andrei Warkentin <andrey.warkentin@gmail.com>
2017-04-25 13:55:18 +03:00
Michael Matz 328b826e8a tccpp: Fix corner case of fnlike macro invocation
Arg substitution leaves placeholder marker in the stream for
empty arguments.  Those need to be skipped when searching for
a fnlike macro invocation in the replacement list itself.  See
testcase.
2017-04-15 19:34:55 +02:00
grischka 13056da039 mems & leaks
- define_start: set above preprocess_start because now
  preprocess_start is defining macros.

- free "cmd_include_files"
- free defines always (after error-longjmps)
- close all files (after error-longjmps)
- tccpe.c: free imports always
- libtcc.c: call tcc_memstats only after all states have
  been deleted.
2017-02-13 18:23:55 +01:00
grischka a4a20360e9 fixes & cleanups
- tccgen.c/tcc.h: allow function declaration after use:
      int f() { return g(); }
      int g() { return 1; }
  may be a warning but not an error
  see also 76cb1144ef

- tccgen.c: redundant code related to inline functions removed
  (functions used anywhere have sym->c set automatically)

- tccgen.c: make 32bit llop non-equal test portable
  (probably not on C67)

- dynarray_add: change prototype to possibly avoid aliasing
  problems or at least warnings

- lib/alloca*.S: ".section .note.GNU-stack,"",%progbits" removed
  (has no effect)

- tccpe: set SizeOfCode field (for correct upx decompression)

- libtcc.c: fixed alternative -run invocation
      tcc "-run -lxxx ..." file.c
  (meant to load the library after file).
  Also supported now:
      tcc files ... options ... -run @ arguments ...
2017-02-13 18:23:43 +01:00
Michael Matz 4beb469c91 Fix pseudo leak
Once-allocated buffers (here a string) that aren't explicitely freed
at program end but rather freed at _exit about 1 nanosecond later
are regarded a leak with MEM_DEBUG, so explicitely free it.  Blaeh :-/
2016-12-20 05:42:25 +01:00
grischka d2332396e4 libtcc.c: -m option cleanup
handle mms-bitfields as sub-options of -m. (-mfloat-abi
is still special because it requires arguments)

tcc.c: help():
- list -mms-bitfields under 'Target specific options'

libtcc.c/MEM_DEBUG
- add check for past buffer writes
2016-12-18 22:57:03 +01:00
grischka a1c12b9fb9 tests: add memory leak test
Also ...

tcctest.c:
- exclude stuff that gcc doesn't compile on windows.

libtcc.c/tccpp.c:
- use unsigned for memory sizes to avoid printf format warnings
- use "file:line: message" to make IDE error parsers happy.

tccgen.c: fix typo
2016-12-18 22:05:42 +01:00
Michael Matz 3e77bfb6e9 tccpp: Fix token pasting
See testcase.  We must always paste tokens (at least if not
currently substing a normal argument, which is a speed optimization
only now) but at the same time must not regard a ## token
coming from argument expansion as the token-paste operator, nor
if we constructed a ## token due to pasting itself (that was already
checked by pp/01.c).
2016-12-15 17:49:56 +01:00
Michael Matz dd57a34866 tccasm: Don't ignore # in preprocessor directives
Our preprocessor throws away # line-comments in asm mode.
It did so also inside preprocessor directives, thereby
removing stringification.  Parse defines in non-asm mode (but
retain '.' as identifier character inside macro definitions).
2016-12-15 17:47:11 +01:00
Michael Matz d0d25ec7df tccpp: Allow computed include like 42.h
The include directive needs to be parsed as pp-tokens, not
as token (i.e. no conversion to TOK_STR or TOK_NUM).  Also fix
parsing computed includes using quoted strings.
2016-12-15 17:47:10 +01:00
Michael Matz b7e0b693a6 tccpp: Implement __BASE_FILE__ macro
Like __FILE__ but always refers to the command line file name also
from inside headers.
2016-12-15 17:47:08 +01:00
Michael Matz 9285149548 Implement -include cmdline option
This option includes a file as if '#include "file"' is the first
line of compiled files.  It's processed after all -D/-U options
and is processed per input file.
2016-12-15 17:47:06 +01:00
Michael Matz 38e5cf0983 tccpp: Fix macro_is_equal
When tokens in macro definitions need cstr_buf inside get_tok_str,
the second might overwrite the first (happens when tokens are
multi-character non-identifiers, see testcase) in macro_is_equal,
failing to diagnose a difference.  Use a real local buffer.
2016-12-15 17:47:05 +01:00
Michael Matz 8080401ab0 tccpp: free defines also with PP_BENCH
When benchmarking preprocessing of multiple files we need to
free the defines like when not benchmarking.
2016-12-15 17:47:05 +01:00
grischka 59216d3db0 tccgen: fix inline_functions double free fix 2016-11-11 20:25:13 +01:00
grischka 7c28c9b13f tccgen: inline_functions double free fix
Fix double free of the inline function token_string which
could happen when an error/longjmp occurred while compiling
the inline function.
2016-11-11 18:29:45 +01:00
grischka 0be098929a tccpp_new/delete and other cleanups 2016-10-17 23:24:01 +02:00
grischka 71b16f4e18 tccpp : "tcc -E -P" : suppress empty lines
Also:
- regenerate all tests/pp/*.expect with gcc
- test "insert one space" feature
- test "0x1E-1" in asm mode case
- PARSE_FLAG_SPACES: ignore \f\v\r better
- tcc.h: move some things
2016-10-09 20:33:14 +02:00
grischka 1c4cf18556 tccpp: no cache for include if #elif seen
#ifndef guards are cached, however after #elif on the
same level, the file must be re-read.

Also: preprocess asm as such even when there is no
assembler (arm).
2016-10-04 17:31:40 +02:00
grischka 332cf5327f tccpp: parse_line_comment: fix possible buffer overrun 2016-10-01 22:01:25 +02:00
grischka 5a23a72aed tccpp: allow "0x1e+1" in asm 2016-10-01 21:58:02 +02:00
grischka 8afb8ccba2 tccpp: token ## pasting: preserve parts if pasting fails
for example
    #define LBL(name) _ ## name ## :
    LBL(main)
will give two tokens
    '_main' and ':'
and the warning
2016-10-01 21:57:40 +02:00
grischka c5b9ae4e3f Revert "-fnormalize-inc-dirs"
Too much code. gcc 3.x doesn't have that either.

This reverts commit 41785a0bf9.
This reverts commit 21665f4338.
2016-10-01 21:57:22 +02:00
grischka 643a1b8848 tcc -E: add one space in cases: tiny solution
replaces f5f82abc99

Also: fix tcc flags in Makefile, fix tcc -E
2016-10-01 21:52:11 +02:00
grischka 766ba3694d tccpp: cleanup
- "utf8 in identifiers"
  from 936819a1b9

- CValue: remove member str.data_allocated
- make tiny allocator private to tccpp

- allocate macro_stack objects on heap
  because otherwise it could crash after error/setjmp
  in preprocess_delete():end_macro()

- mov "TinyAlloc" defs to tccpp.c

- define_push: take int* str again
2016-10-01 20:26:50 +02:00
grischka eacdc426d7 libtcc: cleanup @listfile
Also:
- allow more than one item per line
- respect "quoted items" and escaped quotes \"
  (also for LIBTCCAPI tcc_setoptions)

- cleanup some copy & paste
2016-10-01 20:19:37 +02:00
grischka 3ddbfe1a82 tccpp: #pragma once: make it work
after several "fixes" and "improvements"
   b3782c3cf5
   5fb57bead4
feature did not work at all

- Use 'once' flag, not 'ifndef_macro'
- Ignore filename letter case on _WIN32
- Increment global pp_once for each compilation
2016-10-01 20:03:48 +02:00
grischka 07e47b3dd6 tccpp: restore -D symbols for multiple sources
... also for built-in defines

The case:
    $ tcc -D FOO a.c b.c
with
    // a.c
    #undef FOO

    // b.c
    #ifndef FOO
    # error -D FOO has been lost
    #endif
2016-10-01 19:58:13 +02:00
Jean-Claude Beaudoin ff158bffe6 Rein in unintended external functions. 2016-09-25 22:32:41 -04:00
David Mertens 0373fe0e2a Remove vestiges of earlier type merger 2016-08-16 15:25:09 -04:00
grischka 41349948f8 win64: fix va_arg
fixes 5c35ba66c5

Implementation was consistent within tcc but incompatible
with the ABI (for example library functions vprintf etc)

Also:
- tccpp.c/get_tok_str() : avoid "unknown format "%llu" warning
- x86_64_gen.c/gen_vla_alloc() : fix vstack leak
2016-07-10 20:44:49 +02:00
grischka 9e0e05eb4e Redo "fix line number in macro redefined message"
Smaller change to fix more cases

This reverts commit 0f36f60faa.
2016-05-25 18:51:36 +02:00
seyko 0f36f60faa fix line number in macro redefined message 2016-05-16 03:21:26 +03:00
grischka 5e4d0718ff tcc -E -P10 : output all numbers as decimals
This may be used to preprocess Fabrice Bellards initial revision
in this repository to demonstrate its capability to compile and
run itself (on i386 32-bit linux or windows).

Initial revision: 27f6e16bae

Also needed:
* an empty stdio.h
* a wrapper named tc.c with

  void expr(void);
  void decl(int);
  void next(void);
  #include "tcc.c"

* an hello.c such as

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

All files with unix LF only lines.  Then ...

* preprocess the source
  $ tcc -E -P10 -I. tcc.c -o tc1.c
* compile the compiler
  $ tcc -w -I. tc.c -o tc -ldl
* run it to compile and
   run itself to compile and
    run itself to compile and
     run itself to compile and
      run hello.c
$ ./tc tc1.c tc1.c tc1.c hello.c

--> Hello World!

------------------------------------------------------
* On i386 windows this may be added to the tc.c wrapper

  #ifdef _WIN32
  #include <windows.h>
  void *dlsym(int x, const char *func)
  {
      if (0 == strcmp(func, "dlsym"))
          return &dlsym;
      return GetProcAddress(LoadLibrary("msvcrt"), func);
  }
  #endif
2016-05-12 10:25:50 +02:00
Edmund Grimley Evans f5f82abc99 Insert spaces between certain tokens when tcc is invoked with -E.
Insert a space when it is required to prevent mistokenisation of
the output, and also in a few cases where it is not strictly
required, imitating GCC's behaviour.
2016-05-09 19:27:31 +01:00
seyko 75243f744c TOK_PPNUM in asm (Edmund Grimley Evans version) 2016-05-08 05:14:03 +03:00
grischka fe845cf53d tccpp: cleanup options -dD -dM, remove -C
The lexer is for reading files, not for writing.

Also :
- macro_is_equal(): avoid crash if redefining __FILE__
2016-05-05 14:12:53 +02:00
Edmund Grimley Evans a348513569 Revert 78e4ee5. 2016-05-04 20:27:39 +01:00
Edmund Grimley Evans 6015840583 Revert 3283c26 and a1c1390 in tccpp.c. 2016-05-04 20:14:39 +01:00
seyko 78e4ee55b7 PP_NUM in ASM mode
oxe+1 is parsed as 0xe +1 if (parse_flags & PARSE_FLAG_ASM_FILE)
        Helps to compile a code:
        __asm__("mov $0xe" "+1", "%eax\n")
2016-05-04 16:54:40 +03:00