2007-07-11 21:01:13 +04:00
|
|
|
//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 22:59:25 +03:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-11 21:01:13 +04:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This code simply runs the preprocessor on the input file and prints out the
|
|
|
|
// result. This is the traditional behavior of the -E option.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 08:14:29 +04:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2009-02-06 09:45:26 +03:00
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Lex/Pragma.h"
|
2009-02-13 03:46:04 +03:00
|
|
|
#include "clang/Lex/TokenConcatenation.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-04-08 08:16:20 +04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-07-24 10:57:14 +04:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Config/config.h"
|
2008-08-17 05:47:12 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2007-07-11 21:01:13 +04:00
|
|
|
#include <cstdio>
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
/// PrintMacroDefinition - Print a macro definition in a form that will be
|
|
|
|
/// properly accepted back as a definition.
|
|
|
|
static void PrintMacroDefinition(const IdentifierInfo &II, const MacroInfo &MI,
|
|
|
|
Preprocessor &PP, llvm::raw_ostream &OS) {
|
|
|
|
OS << "#define " << II.getName();
|
|
|
|
|
|
|
|
if (MI.isFunctionLike()) {
|
|
|
|
OS << '(';
|
|
|
|
if (MI.arg_empty())
|
|
|
|
;
|
|
|
|
else if (MI.getNumArgs() == 1)
|
|
|
|
OS << (*MI.arg_begin())->getName();
|
|
|
|
else {
|
|
|
|
MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
|
|
|
|
OS << (*AI++)->getName();
|
|
|
|
while (AI != E)
|
|
|
|
OS << ',' << (*AI++)->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MI.isVariadic()) {
|
|
|
|
if (!MI.arg_empty())
|
|
|
|
OS << ',';
|
|
|
|
OS << "...";
|
|
|
|
}
|
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
// GCC always emits a space, even if the macro body is empty. However, do not
|
|
|
|
// want to emit two spaces if the first token has a leading space.
|
|
|
|
if (MI.tokens_empty() || !MI.tokens_begin()->hasLeadingSpace())
|
|
|
|
OS << ' ';
|
|
|
|
|
|
|
|
llvm::SmallVector<char, 128> SpellingBuffer;
|
|
|
|
for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->hasLeadingSpace())
|
|
|
|
OS << ' ';
|
|
|
|
|
|
|
|
// Make sure we have enough space in the spelling buffer.
|
|
|
|
if (I->getLength() < SpellingBuffer.size())
|
|
|
|
SpellingBuffer.resize(I->getLength());
|
2009-05-21 13:52:38 +04:00
|
|
|
const char *Buffer = SpellingBuffer.data();
|
2009-04-12 05:56:53 +04:00
|
|
|
unsigned SpellingLen = PP.getSpelling(*I, Buffer);
|
|
|
|
OS.write(Buffer, SpellingLen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessed token printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PrintPPOutputPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
2009-02-13 03:46:04 +03:00
|
|
|
TokenConcatenation ConcatInfo;
|
2008-08-17 07:12:02 +04:00
|
|
|
public:
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
private:
|
2007-07-11 21:01:13 +04:00
|
|
|
unsigned CurLine;
|
|
|
|
bool EmittedTokensOnThisLine;
|
2008-10-27 04:19:25 +03:00
|
|
|
SrcMgr::CharacteristicKind FileType;
|
2007-07-24 10:57:14 +04:00
|
|
|
llvm::SmallString<512> CurFilename;
|
2008-09-05 07:22:57 +04:00
|
|
|
bool Initialized;
|
2009-05-19 07:06:47 +04:00
|
|
|
bool DisableLineMarkers;
|
|
|
|
bool DumpDefines;
|
2007-07-11 21:01:13 +04:00
|
|
|
public:
|
2009-05-19 07:06:47 +04:00
|
|
|
PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os,
|
|
|
|
bool lineMarkers, bool defines)
|
|
|
|
: PP(pp), ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
|
|
|
|
DumpDefines(defines) {
|
2007-07-11 21:01:13 +04:00
|
|
|
CurLine = 0;
|
2007-07-24 10:57:14 +04:00
|
|
|
CurFilename += "<uninit>";
|
2007-07-11 21:01:13 +04:00
|
|
|
EmittedTokensOnThisLine = false;
|
2008-09-27 01:18:42 +04:00
|
|
|
FileType = SrcMgr::C_User;
|
2008-09-05 07:22:57 +04:00
|
|
|
Initialized = false;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
|
2007-07-23 10:09:34 +04:00
|
|
|
bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2008-10-27 04:19:25 +03:00
|
|
|
SrcMgr::CharacteristicKind FileType);
|
2007-07-11 21:01:13 +04:00
|
|
|
virtual void Ident(SourceLocation Loc, const std::string &str);
|
2009-01-16 22:25:54 +03:00
|
|
|
virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
|
|
|
|
const std::string &Str);
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-12-10 00:11:08 +03:00
|
|
|
bool HandleFirstTokOnLine(Token &Tok);
|
|
|
|
bool MoveToLine(SourceLocation Loc);
|
2009-02-13 03:46:04 +03:00
|
|
|
bool AvoidConcat(const Token &PrevTok, const Token &Tok) {
|
|
|
|
return ConcatInfo.AvoidConcat(PrevTok, Tok);
|
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
|
2009-04-12 05:56:53 +04:00
|
|
|
|
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
|
|
|
void MacroDefined(const IdentifierInfo *II, const MacroInfo *MI);
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
};
|
2008-04-08 08:16:20 +04:00
|
|
|
} // end anonymous namespace
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2008-09-05 07:22:57 +04:00
|
|
|
void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo,
|
|
|
|
const char *Extra,
|
|
|
|
unsigned ExtraLen) {
|
|
|
|
if (EmittedTokensOnThisLine) {
|
|
|
|
OS << '\n';
|
|
|
|
EmittedTokensOnThisLine = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << '#' << ' ' << LineNo << ' ' << '"';
|
|
|
|
OS.write(&CurFilename[0], CurFilename.size());
|
|
|
|
OS << '"';
|
|
|
|
|
|
|
|
if (ExtraLen)
|
|
|
|
OS.write(Extra, ExtraLen);
|
|
|
|
|
2008-09-27 01:18:42 +04:00
|
|
|
if (FileType == SrcMgr::C_System)
|
2008-09-05 07:22:57 +04:00
|
|
|
OS.write(" 3", 2);
|
2008-09-27 01:18:42 +04:00
|
|
|
else if (FileType == SrcMgr::C_ExternCSystem)
|
2008-09-05 07:22:57 +04:00
|
|
|
OS.write(" 3 4", 4);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// MoveToLine - Move the output to the source line specified by the location
|
|
|
|
/// object. We can do this by emitting some number of \n's, or be emitting a
|
2007-12-10 00:11:08 +03:00
|
|
|
/// #line directive. This returns false if already at the specified line, true
|
|
|
|
/// if some newlines were emitted.
|
|
|
|
bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
|
2009-01-16 10:36:28 +03:00
|
|
|
unsigned LineNo = PP.getSourceManager().getInstantiationLineNumber(Loc);
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (DisableLineMarkers) {
|
2007-12-10 00:11:08 +03:00
|
|
|
if (LineNo == CurLine) return false;
|
|
|
|
|
|
|
|
CurLine = LineNo;
|
|
|
|
|
|
|
|
if (!EmittedTokensOnThisLine)
|
|
|
|
return true;
|
|
|
|
|
2008-08-17 07:12:02 +04:00
|
|
|
OS << '\n';
|
2007-12-10 00:11:08 +03:00
|
|
|
EmittedTokensOnThisLine = false;
|
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
// If this line is "close enough" to the original line, just print newlines,
|
|
|
|
// otherwise print a #line directive.
|
2008-09-26 05:13:35 +04:00
|
|
|
if (LineNo-CurLine <= 8) {
|
2007-07-23 09:14:05 +04:00
|
|
|
if (LineNo-CurLine == 1)
|
2008-08-17 07:12:02 +04:00
|
|
|
OS << '\n';
|
2007-12-10 00:11:08 +03:00
|
|
|
else if (LineNo == CurLine)
|
2009-01-16 10:36:28 +03:00
|
|
|
return false; // Spelling line moved, but instantiation line didn't.
|
2007-07-23 09:14:05 +04:00
|
|
|
else {
|
|
|
|
const char *NewLines = "\n\n\n\n\n\n\n\n";
|
2008-08-17 07:12:02 +04:00
|
|
|
OS.write(NewLines, LineNo-CurLine);
|
2007-07-23 09:14:05 +04:00
|
|
|
}
|
2007-07-11 21:01:13 +04:00
|
|
|
} else {
|
2008-09-05 07:22:57 +04:00
|
|
|
WriteLineInfo(LineNo, 0, 0);
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
|
|
|
|
CurLine = LineNo;
|
2007-12-10 00:11:08 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// FileChanged - Whenever the preprocessor enters or exits a #include file
|
|
|
|
/// it invokes this handler. Update our conception of the current source
|
|
|
|
/// position.
|
|
|
|
void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc,
|
|
|
|
FileChangeReason Reason,
|
2008-10-27 04:19:25 +03:00
|
|
|
SrcMgr::CharacteristicKind NewFileType) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Unless we are exiting a #include, make sure to skip ahead to the line the
|
|
|
|
// #include directive was at.
|
|
|
|
SourceManager &SourceMgr = PP.getSourceManager();
|
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
2009-01-30 21:44:17 +03:00
|
|
|
SourceLocation IncludeLoc = SourceMgr.getPresumedLoc(Loc).getIncludeLoc();
|
|
|
|
if (IncludeLoc.isValid())
|
|
|
|
MoveToLine(IncludeLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else if (Reason == PPCallbacks::SystemHeaderPragma) {
|
|
|
|
MoveToLine(Loc);
|
|
|
|
|
|
|
|
// TODO GCC emits the # directive for this directive on the line AFTER the
|
|
|
|
// directive and emits a bunch of spaces that aren't needed. Emulate this
|
|
|
|
// strange behavior.
|
|
|
|
}
|
|
|
|
|
2009-01-16 10:36:28 +03:00
|
|
|
Loc = SourceMgr.getInstantiationLoc(Loc);
|
2009-03-27 20:13:49 +03:00
|
|
|
// FIXME: Should use presumed line #!
|
2009-02-04 04:06:56 +03:00
|
|
|
CurLine = SourceMgr.getInstantiationLineNumber(Loc);
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2007-12-10 00:11:08 +03:00
|
|
|
if (DisableLineMarkers) return;
|
|
|
|
|
2007-07-24 10:57:14 +04:00
|
|
|
CurFilename.clear();
|
2009-01-27 10:57:44 +03:00
|
|
|
CurFilename += SourceMgr.getPresumedLoc(Loc).getFilename();
|
2007-07-24 10:57:14 +04:00
|
|
|
Lexer::Stringify(CurFilename);
|
2008-09-05 07:22:57 +04:00
|
|
|
FileType = NewFileType;
|
|
|
|
|
|
|
|
if (!Initialized) {
|
|
|
|
WriteLineInfo(CurLine);
|
|
|
|
Initialized = true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
switch (Reason) {
|
|
|
|
case PPCallbacks::EnterFile:
|
2008-09-05 07:22:57 +04:00
|
|
|
WriteLineInfo(CurLine, " 1", 2);
|
2007-07-11 21:01:13 +04:00
|
|
|
break;
|
|
|
|
case PPCallbacks::ExitFile:
|
2008-09-05 07:22:57 +04:00
|
|
|
WriteLineInfo(CurLine, " 2", 2);
|
|
|
|
break;
|
|
|
|
case PPCallbacks::SystemHeaderPragma:
|
|
|
|
case PPCallbacks::RenameFile:
|
|
|
|
WriteLineInfo(CurLine);
|
2007-07-11 21:01:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
/// Ident - Handle #ident directives when read by the preprocessor.
|
2007-07-11 21:01:13 +04:00
|
|
|
///
|
|
|
|
void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
|
|
|
|
MoveToLine(Loc);
|
|
|
|
|
2008-08-17 07:12:02 +04:00
|
|
|
OS.write("#ident ", strlen("#ident "));
|
|
|
|
OS.write(&S[0], S.size());
|
2007-07-11 21:01:13 +04:00
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
|
|
|
void PrintPPOutputPPCallbacks::MacroDefined(const IdentifierInfo *II,
|
|
|
|
const MacroInfo *MI) {
|
|
|
|
// Only print out macro definitions in -dD mode.
|
|
|
|
if (!DumpDefines ||
|
|
|
|
// Ignore __FILE__ etc.
|
|
|
|
MI->isBuiltinMacro()) return;
|
|
|
|
|
|
|
|
MoveToLine(MI->getDefinitionLoc());
|
|
|
|
PrintMacroDefinition(*II, *MI, PP, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
|
|
|
|
const IdentifierInfo *Kind,
|
|
|
|
const std::string &Str) {
|
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma comment(" << Kind->getName();
|
|
|
|
|
|
|
|
if (!Str.empty()) {
|
|
|
|
OS << ", \"";
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
|
|
|
|
unsigned char Char = Str[i];
|
2009-01-17 01:13:37 +03:00
|
|
|
if (isprint(Char) && Char != '\\' && Char != '"')
|
2009-01-16 22:25:54 +03:00
|
|
|
OS << (char)Char;
|
|
|
|
else // Output anything hard as an octal escape.
|
|
|
|
OS << '\\'
|
|
|
|
<< (char)('0'+ ((Char >> 6) & 7))
|
|
|
|
<< (char)('0'+ ((Char >> 3) & 7))
|
|
|
|
<< (char)('0'+ ((Char >> 0) & 7));
|
|
|
|
}
|
|
|
|
OS << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ')';
|
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
|
2007-12-10 00:11:08 +03:00
|
|
|
/// is called for the first token on each new line. If this really is the start
|
|
|
|
/// of a new logical line, handle it and return true, otherwise return false.
|
|
|
|
/// This may not be the start of a logical line because the "start of line"
|
2009-01-16 10:36:28 +03:00
|
|
|
/// marker is set for spelling lines, not instantiation ones.
|
2007-12-10 00:11:08 +03:00
|
|
|
bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Figure out what line we went to and insert the appropriate number of
|
|
|
|
// newline characters.
|
2007-12-10 00:11:08 +03:00
|
|
|
if (!MoveToLine(Tok.getLocation()))
|
|
|
|
return false;
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Print out space characters so that the first token on a line is
|
|
|
|
// indented for easy reading.
|
2007-07-20 20:37:10 +04:00
|
|
|
const SourceManager &SourceMgr = PP.getSourceManager();
|
2009-01-16 10:36:28 +03:00
|
|
|
unsigned ColNo = SourceMgr.getInstantiationColumnNumber(Tok.getLocation());
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// This hack prevents stuff like:
|
|
|
|
// #define HASH #
|
|
|
|
// HASH define foo bar
|
|
|
|
// From having the # character end up at column 1, which makes it so it
|
|
|
|
// is not handled as a #define next time through the preprocessor if in
|
|
|
|
// -fpreprocessed mode.
|
2007-10-09 22:03:42 +04:00
|
|
|
if (ColNo <= 1 && Tok.is(tok::hash))
|
2008-08-17 07:12:02 +04:00
|
|
|
OS << ' ';
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Otherwise, indent the appropriate number of spaces.
|
|
|
|
for (; ColNo > 1; --ColNo)
|
2008-08-17 07:12:02 +04:00
|
|
|
OS << ' ';
|
2007-12-10 00:11:08 +03:00
|
|
|
|
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct UnknownPragmaHandler : public PragmaHandler {
|
|
|
|
const char *Prefix;
|
|
|
|
PrintPPOutputPPCallbacks *Callbacks;
|
|
|
|
|
|
|
|
UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
|
|
|
|
: PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
|
2007-07-20 20:59:19 +04:00
|
|
|
virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Figure out what line we went to and insert the appropriate number of
|
|
|
|
// newline characters.
|
|
|
|
Callbacks->MoveToLine(PragmaTok.getLocation());
|
2008-08-17 07:12:02 +04:00
|
|
|
Callbacks->OS.write(Prefix, strlen(Prefix));
|
2007-07-11 21:01:13 +04:00
|
|
|
|
|
|
|
// Read and print all of the pragma tokens.
|
2007-10-09 22:03:42 +04:00
|
|
|
while (PragmaTok.isNot(tok::eom)) {
|
2007-07-11 21:01:13 +04:00
|
|
|
if (PragmaTok.hasLeadingSpace())
|
2008-08-17 07:12:02 +04:00
|
|
|
Callbacks->OS << ' ';
|
2007-07-11 21:01:13 +04:00
|
|
|
std::string TokSpell = PP.getSpelling(PragmaTok);
|
2008-08-17 07:12:02 +04:00
|
|
|
Callbacks->OS.write(&TokSpell[0], TokSpell.size());
|
2007-07-11 21:01:13 +04:00
|
|
|
PP.LexUnexpandedToken(PragmaTok);
|
|
|
|
}
|
2008-08-17 07:12:02 +04:00
|
|
|
Callbacks->OS << '\n';
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2007-07-23 10:09:34 +04:00
|
|
|
|
2009-02-06 08:56:11 +03:00
|
|
|
static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
|
|
|
|
PrintPPOutputPPCallbacks *Callbacks,
|
|
|
|
llvm::raw_ostream &OS) {
|
|
|
|
char Buffer[256];
|
|
|
|
Token PrevTok;
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
// If this token is at the start of a line, emit newlines if needed.
|
|
|
|
if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
|
|
|
|
// done.
|
|
|
|
} else if (Tok.hasLeadingSpace() ||
|
|
|
|
// If we haven't emitted a token on this line yet, PrevTok isn't
|
|
|
|
// useful to look at and no concatenation could happen anyway.
|
|
|
|
(Callbacks->hasEmittedTokensOnThisLine() &&
|
|
|
|
// Don't print "-" next to "-", it would form "--".
|
|
|
|
Callbacks->AvoidConcat(PrevTok, Tok))) {
|
|
|
|
OS << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
|
|
|
OS.write(II->getName(), II->getLength());
|
|
|
|
} else if (Tok.isLiteral() && !Tok.needsCleaning() &&
|
|
|
|
Tok.getLiteralData()) {
|
|
|
|
OS.write(Tok.getLiteralData(), Tok.getLength());
|
|
|
|
} else if (Tok.getLength() < 256) {
|
|
|
|
const char *TokPtr = Buffer;
|
|
|
|
unsigned Len = PP.getSpelling(Tok, TokPtr);
|
|
|
|
OS.write(TokPtr, Len);
|
|
|
|
} else {
|
|
|
|
std::string S = PP.getSpelling(Tok);
|
|
|
|
OS.write(&S[0], S.size());
|
|
|
|
}
|
|
|
|
Callbacks->SetEmittedTokensOnThisLine();
|
|
|
|
|
|
|
|
if (Tok.is(tok::eof)) break;
|
|
|
|
|
|
|
|
PrevTok = Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-11 01:28:19 +03:00
|
|
|
namespace {
|
|
|
|
struct SortMacrosByID {
|
|
|
|
typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
|
|
|
|
bool operator()(const id_macro_pair &LHS, const id_macro_pair &RHS) const {
|
|
|
|
return strcmp(LHS.first->getName(), RHS.first->getName()) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2009-02-06 08:56:11 +03:00
|
|
|
|
2009-05-19 05:32:34 +04:00
|
|
|
void clang::DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) {
|
|
|
|
// -dM mode just scans and ignores all tokens in the files, then dumps out
|
|
|
|
// the macro table at the end.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
do PP.Lex(Tok);
|
|
|
|
while (Tok.isNot(tok::eof));
|
|
|
|
|
|
|
|
std::vector<std::pair<IdentifierInfo*, MacroInfo*> > MacrosByID;
|
|
|
|
for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
|
|
|
|
I != E; ++I)
|
|
|
|
MacrosByID.push_back(*I);
|
|
|
|
std::sort(MacrosByID.begin(), MacrosByID.end(), SortMacrosByID());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
|
|
|
|
MacroInfo &MI = *MacrosByID[i].second;
|
|
|
|
// Ignore computed macros like __LINE__ and friends.
|
|
|
|
if (MI.isBuiltinMacro()) continue;
|
|
|
|
|
|
|
|
PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
|
|
|
|
*OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// DoPrintPreprocessedInput - This implements -E mode.
|
|
|
|
///
|
2009-05-19 07:06:47 +04:00
|
|
|
void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS,
|
|
|
|
bool EnableCommentOutput,
|
|
|
|
bool EnableMacroCommentOutput,
|
|
|
|
bool DisableLineMarkers,
|
|
|
|
bool DumpDefines) {
|
2007-07-11 21:01:13 +04:00
|
|
|
// Inform the preprocessor whether we want it to retain comments or not, due
|
|
|
|
// to -C or -CC.
|
|
|
|
PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
|
2009-05-19 05:02:07 +04:00
|
|
|
|
|
|
|
OS->SetBufferSize(64*1024);
|
2009-04-12 05:56:53 +04:00
|
|
|
|
2009-05-19 07:06:47 +04:00
|
|
|
PrintPPOutputPPCallbacks *Callbacks =
|
|
|
|
new PrintPPOutputPPCallbacks(PP, *OS, DisableLineMarkers, DumpDefines);
|
2009-05-19 05:32:34 +04:00
|
|
|
PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
|
|
|
|
PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",
|
|
|
|
Callbacks));
|
|
|
|
|
|
|
|
PP.setPPCallbacks(Callbacks);
|
2009-05-19 05:02:07 +04:00
|
|
|
|
2009-05-19 05:32:34 +04:00
|
|
|
// After we have configured the preprocessor, enter the main file.
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
|
|
|
|
// Consume all of the tokens that come from the predefines buffer. Those
|
|
|
|
// should not be emitted into the output and are guaranteed to be at the
|
|
|
|
// start.
|
|
|
|
const SourceManager &SourceMgr = PP.getSourceManager();
|
|
|
|
Token Tok;
|
|
|
|
do PP.Lex(Tok);
|
|
|
|
while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
|
|
|
|
!strcmp(SourceMgr.getPresumedLoc(Tok.getLocation()).getFilename(),
|
|
|
|
"<built-in>"));
|
|
|
|
|
|
|
|
// Read all the preprocessed tokens, printing them out to the stream.
|
|
|
|
PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
|
|
|
|
*OS << '\n';
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|