2006-07-05 03:04:05 +08:00
|
|
|
//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-07-05 03:04:05 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang.h"
|
2006-11-21 12:09:30 +08:00
|
|
|
#include "clang/Lex/PPCallbacks.h"
|
2006-07-05 03:04:05 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
|
|
|
#include "clang/Lex/Pragma.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-04-08 12:16:20 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2007-07-24 14:57:14 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2006-07-05 06:19:33 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-04-08 12:16:20 +08:00
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-07-05 06:19:33 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2008-08-17 09:47:12 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-07-05 03:24:06 +08:00
|
|
|
#include <cstdio>
|
2006-07-05 03:04:05 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2006-07-05 06:19:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessed token printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
EnableMacroCommentOutput("CC",
|
|
|
|
llvm::cl::desc("Enable comment output in -E mode, "
|
2006-07-29 14:30:25 +08:00
|
|
|
"even from macro expansions"));
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2006-11-21 13:02:33 +08:00
|
|
|
namespace {
|
|
|
|
class PrintPPOutputPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
2008-08-17 11:12:02 +08:00
|
|
|
public:
|
|
|
|
llvm::raw_ostream &OS;
|
|
|
|
private:
|
2006-11-21 13:02:33 +08:00
|
|
|
unsigned CurLine;
|
|
|
|
bool EmittedTokensOnThisLine;
|
2008-09-27 05:18:42 +08:00
|
|
|
SrcMgr::Characteristic_t FileType;
|
2007-07-24 14:57:14 +08:00
|
|
|
llvm::SmallString<512> CurFilename;
|
2008-09-05 11:22:57 +08:00
|
|
|
bool Initialized;
|
2006-11-21 13:02:33 +08:00
|
|
|
public:
|
2008-08-17 11:12:02 +08:00
|
|
|
PrintPPOutputPPCallbacks(Preprocessor &pp, llvm::raw_ostream &os)
|
|
|
|
: PP(pp), OS(os) {
|
2006-11-21 13:02:33 +08:00
|
|
|
CurLine = 0;
|
2007-07-24 14:57:14 +08:00
|
|
|
CurFilename += "<uninit>";
|
2006-11-21 13:02:33 +08:00
|
|
|
EmittedTokensOnThisLine = false;
|
2008-09-27 05:18:42 +08:00
|
|
|
FileType = SrcMgr::C_User;
|
2008-09-05 11:22:57 +08:00
|
|
|
Initialized = false;
|
2006-11-21 13:02:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
|
2007-07-23 14:09:34 +08:00
|
|
|
bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
|
2006-11-21 13:02:33 +08:00
|
|
|
|
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2008-09-27 05:18:42 +08:00
|
|
|
SrcMgr::Characteristic_t FileType);
|
2006-11-21 13:02:33 +08:00
|
|
|
virtual void Ident(SourceLocation Loc, const std::string &str);
|
|
|
|
|
|
|
|
|
2007-12-10 05:11:08 +08:00
|
|
|
bool HandleFirstTokOnLine(Token &Tok);
|
|
|
|
bool MoveToLine(SourceLocation Loc);
|
2007-07-21 00:59:19 +08:00
|
|
|
bool AvoidConcat(const Token &PrevTok, const Token &Tok);
|
2008-09-05 11:22:57 +08:00
|
|
|
void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
|
2006-11-21 13:02:33 +08:00
|
|
|
};
|
2008-04-08 12:16:20 +08:00
|
|
|
} // end anonymous namespace
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2008-09-05 11:22:57 +08: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 05:18:42 +08:00
|
|
|
if (FileType == SrcMgr::C_System)
|
2008-09-05 11:22:57 +08:00
|
|
|
OS.write(" 3", 2);
|
2008-09-27 05:18:42 +08:00
|
|
|
else if (FileType == SrcMgr::C_ExternCSystem)
|
2008-09-05 11:22:57 +08:00
|
|
|
OS.write(" 3 4", 4);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2006-07-05 05:28:37 +08: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 05:11:08 +08:00
|
|
|
/// #line directive. This returns false if already at the specified line, true
|
|
|
|
/// if some newlines were emitted.
|
|
|
|
bool PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
|
2008-09-05 11:22:57 +08:00
|
|
|
unsigned LineNo = PP.getSourceManager().getLogicalLineNumber(Loc);
|
|
|
|
|
2006-07-05 06:19:33 +08:00
|
|
|
if (DisableLineMarkers) {
|
2007-12-10 05:11:08 +08:00
|
|
|
if (LineNo == CurLine) return false;
|
|
|
|
|
|
|
|
CurLine = LineNo;
|
|
|
|
|
|
|
|
if (!EmittedTokensOnThisLine)
|
|
|
|
return true;
|
|
|
|
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << '\n';
|
2007-12-10 05:11:08 +08:00
|
|
|
EmittedTokensOnThisLine = false;
|
|
|
|
return true;
|
2006-07-05 06:19:33 +08:00
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
// If this line is "close enough" to the original line, just print newlines,
|
|
|
|
// otherwise print a #line directive.
|
2008-09-26 09:13:35 +08:00
|
|
|
if (LineNo-CurLine <= 8) {
|
2007-07-23 13:14:05 +08:00
|
|
|
if (LineNo-CurLine == 1)
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << '\n';
|
2007-12-10 05:11:08 +08:00
|
|
|
else if (LineNo == CurLine)
|
|
|
|
return false; // Phys line moved, but logical line didn't.
|
2007-07-23 13:14:05 +08:00
|
|
|
else {
|
|
|
|
const char *NewLines = "\n\n\n\n\n\n\n\n";
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write(NewLines, LineNo-CurLine);
|
2007-07-23 13:14:05 +08:00
|
|
|
}
|
2006-07-05 03:04:05 +08:00
|
|
|
} else {
|
2008-09-05 11:22:57 +08:00
|
|
|
WriteLineInfo(LineNo, 0, 0);
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
|
|
|
|
CurLine = LineNo;
|
2007-12-10 05:11:08 +08:00
|
|
|
return true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2006-11-21 12:09:30 +08: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-09-27 05:18:42 +08:00
|
|
|
SrcMgr::Characteristic_t NewFileType) {
|
2006-07-05 03:04:05 +08:00
|
|
|
// Unless we are exiting a #include, make sure to skip ahead to the line the
|
|
|
|
// #include directive was at.
|
2006-11-21 13:02:33 +08:00
|
|
|
SourceManager &SourceMgr = PP.getSourceManager();
|
2006-11-21 12:09:30 +08:00
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
2007-07-21 00:37:10 +08:00
|
|
|
MoveToLine(SourceMgr.getIncludeLoc(Loc));
|
2006-11-21 12:09:30 +08:00
|
|
|
} else if (Reason == PPCallbacks::SystemHeaderPragma) {
|
2006-07-05 05:19:39 +08:00
|
|
|
MoveToLine(Loc);
|
2006-07-05 03:04:05 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
Loc = SourceMgr.getLogicalLoc(Loc);
|
2006-11-21 13:02:33 +08:00
|
|
|
CurLine = SourceMgr.getLineNumber(Loc);
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2007-12-10 05:11:08 +08:00
|
|
|
if (DisableLineMarkers) return;
|
|
|
|
|
2007-07-24 14:57:14 +08:00
|
|
|
CurFilename.clear();
|
|
|
|
CurFilename += SourceMgr.getSourceName(Loc);
|
|
|
|
Lexer::Stringify(CurFilename);
|
2008-09-05 11:22:57 +08:00
|
|
|
FileType = NewFileType;
|
|
|
|
|
|
|
|
if (!Initialized) {
|
|
|
|
WriteLineInfo(CurLine);
|
|
|
|
Initialized = true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
switch (Reason) {
|
2006-11-21 12:09:30 +08:00
|
|
|
case PPCallbacks::EnterFile:
|
2008-09-05 11:22:57 +08:00
|
|
|
WriteLineInfo(CurLine, " 1", 2);
|
2006-07-05 05:19:39 +08:00
|
|
|
break;
|
2006-11-21 12:09:30 +08:00
|
|
|
case PPCallbacks::ExitFile:
|
2008-09-05 11:22:57 +08:00
|
|
|
WriteLineInfo(CurLine, " 2", 2);
|
|
|
|
break;
|
|
|
|
case PPCallbacks::SystemHeaderPragma:
|
|
|
|
case PPCallbacks::RenameFile:
|
|
|
|
WriteLineInfo(CurLine);
|
2006-07-05 05:19:39 +08:00
|
|
|
break;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 05:28:37 +08:00
|
|
|
/// HandleIdent - Handle #ident directives when read by the preprocessor.
|
|
|
|
///
|
2006-11-21 12:09:30 +08:00
|
|
|
void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
|
2006-07-05 05:19:39 +08:00
|
|
|
MoveToLine(Loc);
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write("#ident ", strlen("#ident "));
|
|
|
|
OS.write(&S[0], S.size());
|
2006-11-21 13:02:33 +08:00
|
|
|
EmittedTokensOnThisLine = true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
|
2007-12-10 05:11:08 +08: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"
|
|
|
|
/// marker is set for physical lines, not logical ones.
|
|
|
|
bool PrintPPOutputPPCallbacks::HandleFirstTokOnLine(Token &Tok) {
|
2006-07-05 03:04:05 +08:00
|
|
|
// Figure out what line we went to and insert the appropriate number of
|
|
|
|
// newline characters.
|
2007-12-10 05:11:08 +08:00
|
|
|
if (!MoveToLine(Tok.getLocation()))
|
|
|
|
return false;
|
2006-07-05 03:04:05 +08:00
|
|
|
|
|
|
|
// Print out space characters so that the first token on a line is
|
|
|
|
// indented for easy reading.
|
2007-07-21 00:37:10 +08:00
|
|
|
const SourceManager &SourceMgr = PP.getSourceManager();
|
|
|
|
unsigned ColNo = SourceMgr.getLogicalColumnNumber(Tok.getLocation());
|
2006-07-05 03:04:05 +08: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-10 02:03:42 +08:00
|
|
|
if (ColNo <= 1 && Tok.is(tok::hash))
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << ' ';
|
2006-07-05 03:04:05 +08:00
|
|
|
|
|
|
|
// Otherwise, indent the appropriate number of spaces.
|
|
|
|
for (; ColNo > 1; --ColNo)
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << ' ';
|
2007-12-10 05:11:08 +08:00
|
|
|
|
|
|
|
return true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2006-07-05 03:04:44 +08:00
|
|
|
namespace {
|
2006-07-05 03:04:05 +08:00
|
|
|
struct UnknownPragmaHandler : public PragmaHandler {
|
|
|
|
const char *Prefix;
|
2006-11-21 13:02:33 +08:00
|
|
|
PrintPPOutputPPCallbacks *Callbacks;
|
|
|
|
|
|
|
|
UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
|
|
|
|
: PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
|
2007-07-21 00:59:19 +08:00
|
|
|
virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) {
|
2006-07-05 03:04:05 +08:00
|
|
|
// Figure out what line we went to and insert the appropriate number of
|
|
|
|
// newline characters.
|
2006-11-21 13:02:33 +08:00
|
|
|
Callbacks->MoveToLine(PragmaTok.getLocation());
|
2008-08-17 11:12:02 +08:00
|
|
|
Callbacks->OS.write(Prefix, strlen(Prefix));
|
2006-07-05 03:04:05 +08:00
|
|
|
|
|
|
|
// Read and print all of the pragma tokens.
|
2007-10-10 02:03:42 +08:00
|
|
|
while (PragmaTok.isNot(tok::eom)) {
|
2006-07-05 03:04:05 +08:00
|
|
|
if (PragmaTok.hasLeadingSpace())
|
2008-08-17 11:12:02 +08:00
|
|
|
Callbacks->OS << ' ';
|
2006-07-05 06:19:33 +08:00
|
|
|
std::string TokSpell = PP.getSpelling(PragmaTok);
|
2008-08-17 11:12:02 +08:00
|
|
|
Callbacks->OS.write(&TokSpell[0], TokSpell.size());
|
2006-07-05 03:04:05 +08:00
|
|
|
PP.LexUnexpandedToken(PragmaTok);
|
|
|
|
}
|
2008-08-17 11:12:02 +08:00
|
|
|
Callbacks->OS << '\n';
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
};
|
2006-07-05 03:04:44 +08:00
|
|
|
} // end anonymous namespace
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2007-07-23 14:09:34 +08:00
|
|
|
|
|
|
|
enum AvoidConcatInfo {
|
|
|
|
/// By default, a token never needs to avoid concatenation. Most tokens (e.g.
|
|
|
|
/// ',', ')', etc) don't cause a problem when concatenated.
|
|
|
|
aci_never_avoid_concat = 0,
|
|
|
|
|
|
|
|
/// aci_custom_firstchar - AvoidConcat contains custom code to handle this
|
|
|
|
/// token's requirements, and it needs to know the first character of the
|
|
|
|
/// token.
|
|
|
|
aci_custom_firstchar = 1,
|
|
|
|
|
|
|
|
/// aci_custom - AvoidConcat contains custom code to handle this token's
|
|
|
|
/// requirements, but it doesn't need to know the first character of the
|
|
|
|
/// token.
|
|
|
|
aci_custom = 2,
|
|
|
|
|
|
|
|
/// aci_avoid_equal - Many tokens cannot be safely followed by an '='
|
|
|
|
/// character. For example, "<<" turns into "<<=" when followed by an =.
|
|
|
|
aci_avoid_equal = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This array contains information for each token on what action to take when
|
|
|
|
/// avoiding concatenation of tokens in the AvoidConcat method.
|
|
|
|
static char TokenInfo[tok::NUM_TOKENS];
|
|
|
|
|
|
|
|
/// InitAvoidConcatTokenInfo - Tokens that must avoid concatenation should be
|
|
|
|
/// marked by this function.
|
|
|
|
static void InitAvoidConcatTokenInfo() {
|
|
|
|
// These tokens have custom code in AvoidConcat.
|
|
|
|
TokenInfo[tok::identifier ] |= aci_custom;
|
|
|
|
TokenInfo[tok::numeric_constant] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::period ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::amp ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::plus ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::minus ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::slash ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::less ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::greater ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::pipe ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::percent ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::colon ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::hash ] |= aci_custom_firstchar;
|
|
|
|
TokenInfo[tok::arrow ] |= aci_custom_firstchar;
|
|
|
|
|
|
|
|
// These tokens change behavior if followed by an '='.
|
|
|
|
TokenInfo[tok::amp ] |= aci_avoid_equal; // &=
|
|
|
|
TokenInfo[tok::plus ] |= aci_avoid_equal; // +=
|
|
|
|
TokenInfo[tok::minus ] |= aci_avoid_equal; // -=
|
|
|
|
TokenInfo[tok::slash ] |= aci_avoid_equal; // /=
|
|
|
|
TokenInfo[tok::less ] |= aci_avoid_equal; // <=
|
|
|
|
TokenInfo[tok::greater ] |= aci_avoid_equal; // >=
|
|
|
|
TokenInfo[tok::pipe ] |= aci_avoid_equal; // |=
|
|
|
|
TokenInfo[tok::percent ] |= aci_avoid_equal; // %=
|
|
|
|
TokenInfo[tok::star ] |= aci_avoid_equal; // *=
|
|
|
|
TokenInfo[tok::exclaim ] |= aci_avoid_equal; // !=
|
|
|
|
TokenInfo[tok::lessless ] |= aci_avoid_equal; // <<=
|
|
|
|
TokenInfo[tok::greaterequal] |= aci_avoid_equal; // >>=
|
|
|
|
TokenInfo[tok::caret ] |= aci_avoid_equal; // ^=
|
|
|
|
TokenInfo[tok::equal ] |= aci_avoid_equal; // ==
|
|
|
|
}
|
|
|
|
|
2008-01-15 13:22:14 +08:00
|
|
|
/// StartsWithL - Return true if the spelling of this token starts with 'L'.
|
2008-01-15 13:14:19 +08:00
|
|
|
static bool StartsWithL(const Token &Tok, Preprocessor &PP) {
|
|
|
|
if (!Tok.needsCleaning()) {
|
|
|
|
SourceManager &SrcMgr = PP.getSourceManager();
|
|
|
|
return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
|
|
|
|
== 'L';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.getLength() < 256) {
|
2008-01-15 13:22:14 +08:00
|
|
|
char Buffer[256];
|
2008-01-15 13:14:19 +08:00
|
|
|
const char *TokPtr = Buffer;
|
|
|
|
PP.getSpelling(Tok, TokPtr);
|
|
|
|
return TokPtr[0] == 'L';
|
|
|
|
}
|
|
|
|
|
|
|
|
return PP.getSpelling(Tok)[0] == 'L';
|
|
|
|
}
|
|
|
|
|
2008-01-15 13:22:14 +08:00
|
|
|
/// IsIdentifierL - Return true if the spelling of this token is literally 'L'.
|
|
|
|
static bool IsIdentifierL(const Token &Tok, Preprocessor &PP) {
|
|
|
|
if (!Tok.needsCleaning()) {
|
|
|
|
if (Tok.getLength() != 1)
|
|
|
|
return false;
|
|
|
|
SourceManager &SrcMgr = PP.getSourceManager();
|
|
|
|
return *SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()))
|
|
|
|
== 'L';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Tok.getLength() < 256) {
|
|
|
|
char Buffer[256];
|
|
|
|
const char *TokPtr = Buffer;
|
|
|
|
if (PP.getSpelling(Tok, TokPtr) != 1)
|
|
|
|
return false;
|
|
|
|
return TokPtr[0] == 'L';
|
|
|
|
}
|
|
|
|
|
|
|
|
return PP.getSpelling(Tok) == "L";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-28 14:56:01 +08:00
|
|
|
/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
|
|
|
|
/// the two individual tokens to be lexed as a single token, return true (which
|
|
|
|
/// causes a space to be printed between them). This allows the output of -E
|
|
|
|
/// mode to be lexed to the same token stream as lexing the input directly
|
|
|
|
/// would.
|
|
|
|
///
|
|
|
|
/// This code must conservatively return true if it doesn't want to be 100%
|
|
|
|
/// accurate. This will cause the output to include extra space characters, but
|
|
|
|
/// the resulting output won't have incorrect concatenations going on. Examples
|
|
|
|
/// include "..", which we print with a space between, because we don't want to
|
|
|
|
/// track enough to tell "x.." from "...".
|
2007-07-21 00:59:19 +08:00
|
|
|
bool PrintPPOutputPPCallbacks::AvoidConcat(const Token &PrevTok,
|
|
|
|
const Token &Tok) {
|
2006-07-28 14:56:01 +08:00
|
|
|
char Buffer[256];
|
|
|
|
|
2007-07-23 14:09:34 +08:00
|
|
|
tok::TokenKind PrevKind = PrevTok.getKind();
|
|
|
|
if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
|
|
|
|
PrevKind = tok::identifier;
|
|
|
|
|
|
|
|
// Look up information on when we should avoid concatenation with prevtok.
|
|
|
|
unsigned ConcatInfo = TokenInfo[PrevKind];
|
|
|
|
|
|
|
|
// If prevtok never causes a problem for anything after it, return quickly.
|
|
|
|
if (ConcatInfo == 0) return false;
|
2006-07-28 14:56:01 +08:00
|
|
|
|
2007-07-23 14:09:34 +08:00
|
|
|
if (ConcatInfo & aci_avoid_equal) {
|
|
|
|
// If the next token is '=' or '==', avoid concatenation.
|
2007-10-10 02:03:42 +08:00
|
|
|
if (Tok.is(tok::equal) || Tok.is(tok::equalequal))
|
2007-07-23 14:09:34 +08:00
|
|
|
return true;
|
2007-07-24 07:21:34 +08:00
|
|
|
ConcatInfo &= ~aci_avoid_equal;
|
2007-07-23 14:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ConcatInfo == 0) return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-07-28 14:56:01 +08:00
|
|
|
// Basic algorithm: we look at the first character of the second token, and
|
|
|
|
// determine whether it, if appended to the first token, would form (or would
|
|
|
|
// contribute) to a larger token if concatenated.
|
2007-07-23 14:09:34 +08:00
|
|
|
char FirstChar = 0;
|
|
|
|
if (ConcatInfo & aci_custom) {
|
|
|
|
// If the token does not need to know the first character, don't get it.
|
|
|
|
} else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
2006-07-28 14:56:01 +08:00
|
|
|
// Avoid spelling identifiers, the most common form of token.
|
|
|
|
FirstChar = II->getName()[0];
|
2007-07-23 13:18:42 +08:00
|
|
|
} else if (!Tok.needsCleaning()) {
|
|
|
|
SourceManager &SrcMgr = PP.getSourceManager();
|
|
|
|
FirstChar =
|
|
|
|
*SrcMgr.getCharacterData(SrcMgr.getPhysicalLoc(Tok.getLocation()));
|
2006-07-28 14:56:01 +08:00
|
|
|
} else if (Tok.getLength() < 256) {
|
2006-10-18 14:06:41 +08:00
|
|
|
const char *TokPtr = Buffer;
|
|
|
|
PP.getSpelling(Tok, TokPtr);
|
|
|
|
FirstChar = TokPtr[0];
|
2006-07-28 14:56:01 +08:00
|
|
|
} else {
|
|
|
|
FirstChar = PP.getSpelling(Tok)[0];
|
|
|
|
}
|
2007-07-23 14:09:34 +08:00
|
|
|
|
2006-07-28 14:56:01 +08:00
|
|
|
switch (PrevKind) {
|
2007-07-23 14:09:34 +08:00
|
|
|
default: assert(0 && "InitAvoidConcatTokenInfo built wrong");
|
2006-07-28 14:56:01 +08:00
|
|
|
case tok::identifier: // id+id or id+number or id+L"foo".
|
2007-10-10 02:03:42 +08:00
|
|
|
if (Tok.is(tok::numeric_constant) || Tok.getIdentifierInfo() ||
|
|
|
|
Tok.is(tok::wide_string_literal) /* ||
|
|
|
|
Tok.is(tok::wide_char_literal)*/)
|
2007-07-23 14:09:34 +08:00
|
|
|
return true;
|
2008-01-15 13:14:19 +08:00
|
|
|
|
|
|
|
// If this isn't identifier + string, we're done.
|
|
|
|
if (Tok.isNot(tok::char_constant) && Tok.isNot(tok::string_literal))
|
2007-07-23 14:09:34 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: need a wide_char_constant!
|
2008-01-15 13:14:19 +08:00
|
|
|
|
|
|
|
// If the string was a wide string L"foo" or wide char L'f', it would concat
|
|
|
|
// with the previous identifier into fooL"bar". Avoid this.
|
|
|
|
if (StartsWithL(Tok, PP))
|
|
|
|
return true;
|
|
|
|
|
2008-01-15 13:22:14 +08:00
|
|
|
// Otherwise, this is a narrow character or string. If the *identifier* is
|
|
|
|
// a literal 'L', avoid pasting L "foo" -> L"foo".
|
|
|
|
return IsIdentifierL(PrevTok, PP);
|
2006-07-28 14:56:01 +08:00
|
|
|
case tok::numeric_constant:
|
2007-10-10 02:03:42 +08:00
|
|
|
return isalnum(FirstChar) || Tok.is(tok::numeric_constant) ||
|
2006-07-28 14:56:01 +08:00
|
|
|
FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
|
|
|
|
case tok::period: // ..., .*, .1234
|
|
|
|
return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
|
2007-07-23 14:09:34 +08:00
|
|
|
case tok::amp: // &&
|
|
|
|
return FirstChar == '&';
|
|
|
|
case tok::plus: // ++
|
|
|
|
return FirstChar == '+';
|
|
|
|
case tok::minus: // --, ->, ->*
|
|
|
|
return FirstChar == '-' || FirstChar == '>';
|
|
|
|
case tok::slash: //, /*, //
|
|
|
|
return FirstChar == '*' || FirstChar == '/';
|
|
|
|
case tok::less: // <<, <<=, <:, <%
|
|
|
|
return FirstChar == '<' || FirstChar == ':' || FirstChar == '%';
|
|
|
|
case tok::greater: // >>, >>=
|
|
|
|
return FirstChar == '>';
|
|
|
|
case tok::pipe: // ||
|
|
|
|
return FirstChar == '|';
|
|
|
|
case tok::percent: // %>, %:
|
|
|
|
return FirstChar == '>' || FirstChar == ':';
|
2006-07-28 14:56:01 +08:00
|
|
|
case tok::colon: // ::, :>
|
|
|
|
return FirstChar == ':' || FirstChar == '>';
|
|
|
|
case tok::hash: // ##, #@, %:%:
|
|
|
|
return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
|
|
|
|
case tok::arrow: // ->*
|
|
|
|
return FirstChar == '*';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
/// DoPrintPreprocessedInput - This implements -E mode.
|
2006-07-05 05:28:37 +08:00
|
|
|
///
|
2008-04-08 12:16:20 +08:00
|
|
|
void clang::DoPrintPreprocessedInput(Preprocessor &PP,
|
|
|
|
const std::string &OutFile) {
|
2006-11-21 14:17:10 +08:00
|
|
|
// Inform the preprocessor whether we want it to retain comments or not, due
|
|
|
|
// to -C or -CC.
|
|
|
|
PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
|
2007-07-23 14:09:34 +08:00
|
|
|
InitAvoidConcatTokenInfo();
|
2008-08-17 11:12:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Open the output buffer.
|
2008-08-17 11:54:39 +08:00
|
|
|
std::string Err;
|
2008-08-17 15:09:08 +08:00
|
|
|
llvm::raw_fd_ostream OS(OutFile.empty() ? "-" : OutFile.c_str(), Err);
|
2008-08-17 11:54:39 +08:00
|
|
|
if (!Err.empty()) {
|
|
|
|
fprintf(stderr, "%s\n", Err.c_str());
|
|
|
|
exit(1);
|
2008-08-17 11:12:02 +08:00
|
|
|
}
|
|
|
|
|
2008-08-17 11:54:39 +08:00
|
|
|
OS.SetBufferSize(64*1024);
|
|
|
|
|
2006-07-05 06:19:33 +08:00
|
|
|
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok, PrevTok;
|
2006-07-05 03:04:05 +08:00
|
|
|
char Buffer[256];
|
2008-08-17 11:12:02 +08:00
|
|
|
PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, OS);
|
2006-11-21 13:02:33 +08:00
|
|
|
PP.setPPCallbacks(Callbacks);
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2006-11-21 13:02:33 +08:00
|
|
|
PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
|
|
|
|
PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
|
2006-07-29 14:35:08 +08:00
|
|
|
|
|
|
|
// After we have configured the preprocessor, enter the main file.
|
|
|
|
|
|
|
|
// Start parsing the specified input file.
|
2007-12-20 06:51:13 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2007-10-11 04:45:16 +08:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
do PP.Lex(Tok);
|
2007-10-11 07:31:03 +08:00
|
|
|
while (Tok.isNot(tok::eof) && Tok.getLocation().isFileID() &&
|
2007-10-11 04:45:16 +08:00
|
|
|
!strcmp(SourceMgr.getSourceName(Tok.getLocation()), "<predefines>"));
|
|
|
|
|
|
|
|
while (1) {
|
2006-07-05 03:04:05 +08:00
|
|
|
|
2006-07-05 07:24:26 +08:00
|
|
|
// If this token is at the start of a line, emit newlines if needed.
|
2007-12-10 05:11:08 +08:00
|
|
|
if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
|
|
|
|
// done.
|
2006-07-28 14:56:01 +08:00
|
|
|
} else if (Tok.hasLeadingSpace() ||
|
2007-07-23 14:09:34 +08:00
|
|
|
// If we haven't emitted a token on this line yet, PrevTok isn't
|
|
|
|
// useful to look at and no concatenation could happen anyway.
|
2007-07-24 07:21:34 +08:00
|
|
|
(Callbacks->hasEmittedTokensOnThisLine() &&
|
2007-07-23 14:09:34 +08:00
|
|
|
// Don't print "-" next to "-", it would form "--".
|
|
|
|
Callbacks->AvoidConcat(PrevTok, Tok))) {
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << ' ';
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2007-07-23 14:14:36 +08:00
|
|
|
if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
|
|
|
const char *Str = II->getName();
|
|
|
|
unsigned Len = Tok.needsCleaning() ? strlen(Str) : Tok.getLength();
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write(Str, Len);
|
2007-07-23 14:14:36 +08:00
|
|
|
} else if (Tok.getLength() < 256) {
|
2006-07-05 06:33:12 +08:00
|
|
|
const char *TokPtr = Buffer;
|
|
|
|
unsigned Len = PP.getSpelling(Tok, TokPtr);
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write(TokPtr, Len);
|
2006-07-05 03:04:05 +08:00
|
|
|
} else {
|
2006-07-05 03:24:06 +08:00
|
|
|
std::string S = PP.getSpelling(Tok);
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write(&S[0], S.size());
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2006-11-21 13:02:33 +08:00
|
|
|
Callbacks->SetEmittedTokensOnThisLine();
|
2007-10-11 04:45:16 +08:00
|
|
|
|
|
|
|
if (Tok.is(tok::eof)) break;
|
|
|
|
|
|
|
|
PrevTok = Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
2008-08-17 11:12:02 +08:00
|
|
|
OS << '\n';
|
2006-07-05 06:19:33 +08:00
|
|
|
|
2008-08-17 15:07:01 +08:00
|
|
|
// Flush the ostream.
|
|
|
|
OS.flush();
|
2008-08-17 11:12:02 +08:00
|
|
|
|
|
|
|
// If an error occurred, remove the output file.
|
|
|
|
if (PP.getDiagnostics().hasErrorOccurred() && !OutFile.empty())
|
|
|
|
llvm::sys::Path(OutFile).eraseFromDisk();
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|