LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 12146 - clang's NULL definition in stddef.h clashes with msvc's crtdefs.h
Summary: clang's NULL definition in stddef.h clashes with msvc's crtdefs.h
Status: RESOLVED FIXED
Alias: None
Product: clang
Classification: Unclassified
Component: Headers (show other bugs)
Version: unspecified
Hardware: PC All
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks: 13707
  Show dependency tree
 
Reported: 2012-02-29 20:13 PST by Nico Weber
Modified: 2014-09-24 14:30 PDT (History)
3 users (show)

See Also:
Fixed By Commit(s):


Attachments
__clang_null patch (1.90 KB, patch)
2014-09-24 14:30 PDT, Nico Weber
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nico Weber 2012-02-29 20:13:48 PST
Compiling

#include <stddef.h>
#include <crtdefs.h>
int* a = NULL

without -nobuiltininc yields:

test.cc:5:16: error: expected expression
int* a = __null;


That's because clang's stddef.h defines NULL to __null, but crtdefs.h apparently defines __null to something else.


Not sure what the right fix is. Make __clang_null an alternate spelling of __null and define NULL to __clang_null in stddef.h?
Comment 1 Nico Weber 2012-02-29 20:15:15 PST
Eli, since you said you'd rather not support -nobuiltininc -fsyntax-only: Do you have any opinion which direction to take in this bug?
Comment 2 Eli Friedman 2012-02-29 20:59:06 PST
How about just using "0" when -fms-compatibility is on?  It's equivalent except that you'll get slightly worse warnings.

__clang_null is ugly, but making it an alias for __null is okay if necessary.
Comment 3 Nico Weber 2012-04-24 14:11:03 PDT
The reverse is an issue too: clang's stddef.h does "#undef __null". However, some ms analysis tool uses __null as an annotation and has a header that defines it to nothing in normal builds. With clang's current stddef, this code breaks:

#define __null  // Emulate ms header
#include <stddef.h>

void f(__null void* p) {
}
Comment 4 Nico Weber 2012-04-24 16:27:36 PDT
r155490 fixes this by defining NULL to 0. Maybe I'll look into having a __clang_null another day.
Comment 5 Nico Weber 2014-09-24 14:30:05 PDT
Created attachment 13084 [details]
__clang_null patch

Here's how __clang_null would look, it's very simple.

One side effect of this is that clang starts warning on many things where existing code passes NULL for some HANDLE and the HANDLE is an int behind the scenes – but even MSDN docs say to use NULL (e.g. http://msdn.microsoft.com/en-us/library/windows/desktop/aa363686(v=vs.85).aspx)