[cfe-commits] r38601 - in /cfe/cfe/trunk: Lex/Preprocessor.cpp include/clang/Basic/DiagnosticKinds.def include/clang/Lex/Preprocessor.h
sabre at cs.uiuc.edu
sabre at cs.uiuc.edu
Wed Jul 11 09:23:05 PDT 2007
Author: sabre
Date: Wed Jul 11 11:23:05 2007
New Revision: 38601
URL: http://llvm.org/viewvc/llvm-project?rev=38601&view=rev
Log:
Implement __INCLUDE_LEVEL__ and __BASE_FILE__
Modified:
cfe/cfe/trunk/Lex/Preprocessor.cpp
cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
Modified: cfe/cfe/trunk/Lex/Preprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Lex/Preprocessor.cpp?rev=38601&r1=38600&r2=38601&view=diff
==============================================================================
--- cfe/cfe/trunk/Lex/Preprocessor.cpp (original)
+++ cfe/cfe/trunk/Lex/Preprocessor.cpp Wed Jul 11 11:23:05 2007
@@ -31,7 +31,7 @@
//
// TODO: Implement the include guard optimization.
//
-// Predefined Macros: _Pragma, __TIMESTAMP__, __TIME__, ...
+// Predefined Macros: _Pragma, __TIMESTAMP__, ...
//
//===----------------------------------------------------------------------===//
@@ -437,6 +437,18 @@
Ident__FILE__ = RegisterBuiltinMacro("__FILE__");
Ident__DATE__ = RegisterBuiltinMacro("__DATE__");
Ident__TIME__ = RegisterBuiltinMacro("__TIME__");
+
+ // GCC Extensions.
+ Ident__BASE_FILE__ = RegisterBuiltinMacro("__BASE_FILE__");
+ Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro("__INCLUDE_LEVEL__");
+ // __TIMESTAMP__
+ // _Pragma
+
+//Pseudo #defines.
+ // __STDC__ 1 if !stdc_0_in_system_headers and "std"
+ // __STDC_VERSION__
+ // __STDC_HOSTED__
+ // __OBJC__
}
@@ -558,9 +570,19 @@
Tok.SetKind(tok::numeric_constant);
Tok.SetLength(Length);
Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
- } else if (ITI == Ident__FILE__) {
- // FIXME: Escape this correctly.
- std::string FN = '"' + SourceMgr.getSourceName(Tok.getLocation()) + '"';
+ } else if (ITI == Ident__FILE__ || ITI == Ident__BASE_FILE__) {
+ SourceLocation Loc = Tok.getLocation();
+ if (ITI == Ident__BASE_FILE__) {
+ Diag(Tok, diag::ext_pp_base_file);
+ SourceLocation NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
+ while (NextLoc.getFileID() != 0) {
+ Loc = NextLoc;
+ NextLoc = SourceMgr.getIncludeLoc(Loc.getFileID());
+ }
+ }
+
+ // FIXME: Escape this filename correctly.
+ std::string FN = '"' + SourceMgr.getSourceName(Loc) + '"';
Tok.SetKind(tok::string_literal);
Tok.SetLength(FN.size());
Tok.SetLocation(ScratchBuf->getToken(&FN[0], FN.size(), Tok.getLocation()));
@@ -576,6 +598,21 @@
Tok.SetKind(tok::string_literal);
Tok.SetLength(strlen("\"hh:mm:ss\""));
Tok.SetLocation(SourceMgr.getInstantiationLoc(TIMELoc, Tok.getLocation()));
+ } else if (ITI == Ident__INCLUDE_LEVEL__) {
+ Diag(Tok, diag::ext_pp_include_level);
+
+ // Compute the include depth of this token.
+ unsigned Depth = 0;
+ SourceLocation Loc = SourceMgr.getIncludeLoc(Tok.getLocation().getFileID());
+ for (; Loc.getFileID() != 0; ++Depth)
+ Loc = SourceMgr.getIncludeLoc(Loc.getFileID());
+
+ // __INCLUDE_LEVEL__ expands to a simple numeric value.
+ sprintf(TmpBuffer, "%u", Depth);
+ unsigned Length = strlen(TmpBuffer);
+ Tok.SetKind(tok::numeric_constant);
+ Tok.SetLength(Length);
+ Tok.SetLocation(ScratchBuf->getToken(TmpBuffer, Length, Tok.getLocation()));
} else {
assert(0 && "Unknown identifier!");
}
Modified: cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=38601&r1=38600&r2=38601&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/cfe/trunk/include/clang/Basic/DiagnosticKinds.def Wed Jul 11 11:23:05 2007
@@ -109,6 +109,11 @@
"extra tokens at end of %s directive")
DIAG(ext_pp_comma_expr, EXTENSION,
"comma operator in operand of #if")
+
+DIAG(ext_pp_base_file, EXTENSION,
+ "__BASE_FILE__ is a language extension")
+DIAG(ext_pp_include_level, EXTENSION,
+ "__INCLUDE_LEVEL__ is a language extension")
DIAG(err_pp_invalid_directive, ERROR,
"invalid preprocessing directive")
Modified: cfe/cfe/trunk/include/clang/Lex/Preprocessor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/Lex/Preprocessor.h?rev=38601&r1=38600&r2=38601&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/Lex/Preprocessor.h (original)
+++ cfe/cfe/trunk/include/clang/Lex/Preprocessor.h Wed Jul 11 11:23:05 2007
@@ -95,6 +95,9 @@
/// Identifiers for builtin macros.
IdentifierTokenInfo *Ident__LINE__, *Ident__FILE__; // __LINE__, __FILE__
IdentifierTokenInfo *Ident__DATE__, *Ident__TIME__; // __DATE__, __TIME__
+ IdentifierTokenInfo *Ident__INCLUDE_LEVEL__; // __INCLUDE_LEVEL__
+ IdentifierTokenInfo *Ident__BASE_FILE__; // __BASE_FILE__
+
SourceLocation DATELoc, TIMELoc;
public:
enum FileChangeReason {
More information about the cfe-commits
mailing list