New user self-registration is currently disabled. Please email llvm-admin@lists.llvm.org if you need an account.

Bug 12035 - Preprocessor inserts spaces in macro expansions
: Preprocessor inserts spaces in macro expansions
Status: NEW
Product: clang
Classification: Unclassified
Component: -New Bugs
: trunk
: Macintosh MacOS X
: P enhancement
Assigned To: Unassigned Clang Bugs
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-02-19 02:20 CST by Nikita Zhuk
Modified: 2016-01-11 10:10 CST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Zhuk 2012-02-19 02:20:13 CST
clang 3.1 which is shipping with Xcode 4.3 ("based on LLVM 3.1svn") seems to insert extra spaces in macro expansions.

A test case:

$ cat foo.h 
#define VALUE 2012-02-19
Output: VALUE

$ clang -E -P foo.h 

Output: 2012 -02 -19

$ gcc -E -P foo.h 



Output: 2012-02-19


$ clang --version
Apple clang version 3.1 (tags/Apple/clang-318.0.45) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.3.0
Thread model: posix


Notes:
- Apple Radar #10883862
Comment 1 Eli Friedman 2012-02-19 03:01:00 CST
clang's preprocessor is a C preprocessor; the primary point is that the output parses into the same C tokens as the input.  We don't make any guarantees about preserving whitespace or the lack of whitespace between tokens in preprocessed source.

That said, clang could probably do better in this particular case.
Comment 2 Nikita Zhuk 2012-02-19 04:57:04 CST
(In reply to comment #1)
> clang's preprocessor is a C preprocessor; the primary point is that the output
> parses into the same C tokens as the input.  We don't make any guarantees about
> preserving whitespace or the lack of whitespace between tokens in preprocessed
> source.

Just to provide some context to how this issue was discovered: clang's preprocessor is used by Xcode 4.3 to preprocess Info.plist files. Some Xcode users use this functionality to define macros in Info.plist file which then expand to URL values. In fact, Apple acknowledges this use case by mentioning it in their documentation: https://developer.apple.com/library/mac/#technotes/tn2175/_index.html

So while current functionality of clang preprocessor might be correct from C preprocessor point of view, it currently causes problems in those use cases in which it's widely used as Info.plist preprocessor and as promoted by Apple. From that perspective this might be more of Apple's documentation issue (updating TN2175 to take clang preprocessor's implementation details into account), which rdar://10883862 will hopefully help to address.
Comment 3 Daniel Goldman 2014-12-25 21:35:41 CST
This seems fixed now, which I am very glad to see. 

This was a total bug. In cpp, the replacement is EXACTLY the same. Other than something like substituting "one space" for "multiple spaces", cpp does not "clean up" or "tidy" code. It was totally wrong to insert a space willy-nilly like it was previously doing, arbitrarily changing "2012-02-19" to "2012 -02 -19".

Anyway, here is the current (correct) behavior:

$ clang --version
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix

$ cat clang-2.h
#define DATE 2012-02-19   X
DATE

$ gcc -E -P clang-2.h
2012-02-19 X

$ clang -E -P clang-2.h

2012-02-19 X

Thanks very much to whoever fixed this.
Comment 4 Michael Tsai 2016-01-11 10:10:54 CST
As of Xcode 7.2, the -traditional flag is not working as described here:

https://developer.apple.com/library/mac/#technotes/tn2175/_index.html

I'm trying to construct a version number for my Info.plist as described in the last example, and it is putting spaces before and after each period.