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-11-11 13:07:22 +03:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/PreprocessorOutputOptions.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/Pragma.h"
|
2009-11-11 13:07:22 +03:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-02-13 03:46:04 +03:00
|
|
|
#include "clang/Lex/TokenConcatenation.h"
|
2007-07-24 10:57:14 +04:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-01-19 20:42:20 +03:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2010-06-26 21:11:39 +04:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2008-08-17 05:47:12 +04:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-06-22 23:41:48 +04:00
|
|
|
#include "llvm/Support/ErrorHandling.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,
|
2011-07-23 14:55:15 +04:00
|
|
|
Preprocessor &PP, raw_ostream &OS) {
|
2009-04-12 05:56:53 +04:00
|
|
|
OS << "#define " << II.getName();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
if (MI.isFunctionLike()) {
|
|
|
|
OS << '(';
|
2009-12-09 05:08:14 +03:00
|
|
|
if (!MI.arg_empty()) {
|
2009-04-12 05:56:53 +04:00
|
|
|
MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
|
2009-12-09 05:08:14 +03:00
|
|
|
for (; AI+1 != E; ++AI) {
|
|
|
|
OS << (*AI)->getName();
|
2009-04-12 05:56:53 +04:00
|
|
|
OS << ',';
|
2009-12-07 04:58:34 +03:00
|
|
|
}
|
2009-12-09 05:08:14 +03:00
|
|
|
|
|
|
|
// Last argument.
|
|
|
|
if ((*AI)->getName() == "__VA_ARGS__")
|
|
|
|
OS << "...";
|
|
|
|
else
|
|
|
|
OS << (*AI)->getName();
|
2009-04-12 05:56:53 +04:00
|
|
|
}
|
2009-12-07 04:58:34 +03:00
|
|
|
|
|
|
|
if (MI.isGNUVarargs())
|
|
|
|
OS << "..."; // #define foo(x...)
|
2010-03-07 10:30:06 +03:00
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
OS << ')';
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
// 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 << ' ';
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2012-02-05 06:13:05 +04:00
|
|
|
SmallString<128> SpellingBuffer;
|
2009-04-12 05:56:53 +04:00
|
|
|
for (MacroInfo::tokens_iterator I = MI.tokens_begin(), E = MI.tokens_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->hasLeadingSpace())
|
|
|
|
OS << ' ';
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-27 16:44:12 +03:00
|
|
|
OS << PP.getSpelling(*I, SpellingBuffer);
|
2009-04-12 05:56:53 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessed token printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class PrintPPOutputPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
2010-04-13 04:06:42 +04:00
|
|
|
SourceManager &SM;
|
2009-02-13 03:46:04 +03:00
|
|
|
TokenConcatenation ConcatInfo;
|
2008-08-17 07:12:02 +04:00
|
|
|
public:
|
2011-07-23 14:55:15 +04:00
|
|
|
raw_ostream &OS;
|
2008-08-17 07:12:02 +04:00
|
|
|
private:
|
2007-07-11 21:01:13 +04:00
|
|
|
unsigned CurLine;
|
2010-08-25 02:44:13 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
bool EmittedTokensOnThisLine;
|
2009-06-02 11:55:39 +04:00
|
|
|
bool EmittedMacroOnThisLine;
|
2008-10-27 04:19:25 +03:00
|
|
|
SrcMgr::CharacteristicKind FileType;
|
2012-02-05 06:13:05 +04:00
|
|
|
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;
|
2009-12-07 04:42:56 +03:00
|
|
|
bool UseLineDirective;
|
2007-07-11 21:01:13 +04:00
|
|
|
public:
|
2011-07-23 14:55:15 +04:00
|
|
|
PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os,
|
2011-02-02 18:41:17 +03:00
|
|
|
bool lineMarkers, bool defines)
|
2010-04-13 04:06:42 +04:00
|
|
|
: PP(pp), SM(PP.getSourceManager()),
|
|
|
|
ConcatInfo(PP), OS(os), DisableLineMarkers(lineMarkers),
|
2011-02-02 18:41:17 +03:00
|
|
|
DumpDefines(defines) {
|
|
|
|
CurLine = 0;
|
2007-07-24 10:57:14 +04:00
|
|
|
CurFilename += "<uninit>";
|
2007-07-11 21:01:13 +04:00
|
|
|
EmittedTokensOnThisLine = false;
|
2009-06-02 11:55:39 +04:00
|
|
|
EmittedMacroOnThisLine = false;
|
2008-09-27 01:18:42 +04:00
|
|
|
FileType = SrcMgr::C_User;
|
2008-09-05 07:22:57 +04:00
|
|
|
Initialized = false;
|
2010-03-07 10:30:06 +03:00
|
|
|
|
2009-12-07 04:42:56 +03:00
|
|
|
// If we're in microsoft mode, use normal #line instead of line markers.
|
2012-03-11 11:00:24 +04:00
|
|
|
UseLineDirective = PP.getLangOpts().MicrosoftExt;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
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; }
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-09-10 02:45:38 +04:00
|
|
|
bool StartNewLineIfNeeded();
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
2011-10-11 21:29:44 +04:00
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID);
|
2007-07-11 21:01:13 +04:00
|
|
|
virtual void Ident(SourceLocation Loc, const std::string &str);
|
2009-09-09 19:08:12 +04:00
|
|
|
virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
|
2009-01-16 22:25:54 +03:00
|
|
|
const std::string &Str);
|
2011-07-23 14:55:15 +04:00
|
|
|
virtual void PragmaMessage(SourceLocation Loc, StringRef Str);
|
2011-06-22 23:41:48 +04:00
|
|
|
virtual void PragmaDiagnosticPush(SourceLocation Loc,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Namespace);
|
2011-06-22 23:41:48 +04:00
|
|
|
virtual void PragmaDiagnosticPop(SourceLocation Loc,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Namespace);
|
|
|
|
virtual void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
|
2012-02-08 02:29:24 +04:00
|
|
|
diag::Mapping Map, StringRef Str);
|
2007-07-11 21:01:13 +04:00
|
|
|
|
2007-12-10 00:11:08 +03:00
|
|
|
bool HandleFirstTokOnLine(Token &Tok);
|
2010-04-13 04:01:41 +04:00
|
|
|
bool MoveToLine(SourceLocation Loc) {
|
2010-11-12 10:15:47 +03:00
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
if (PLoc.isInvalid())
|
|
|
|
return false;
|
|
|
|
return MoveToLine(PLoc.getLine());
|
2010-04-13 04:01:41 +04:00
|
|
|
}
|
|
|
|
bool MoveToLine(unsigned LineNo);
|
|
|
|
|
2010-04-14 07:57:19 +04:00
|
|
|
bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
|
|
|
|
const Token &Tok) {
|
|
|
|
return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
|
2009-02-13 03:46:04 +03:00
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
void WriteLineInfo(unsigned LineNo, const char *Extra=0, unsigned ExtraLen=0);
|
2010-09-10 02:45:38 +04:00
|
|
|
bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
|
2009-06-15 05:25:23 +04:00
|
|
|
void HandleNewlinesInToken(const char *TokStr, unsigned Len);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
2010-11-20 00:33:15 +03:00
|
|
|
void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-08-08 02:27:00 +04:00
|
|
|
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
|
2010-11-20 00:33:15 +03:00
|
|
|
void MacroUndefined(const Token &MacroNameTok, 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) {
|
2009-06-02 11:55:39 +04:00
|
|
|
if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
|
2008-09-05 07:22:57 +04:00
|
|
|
OS << '\n';
|
|
|
|
EmittedTokensOnThisLine = false;
|
2009-06-02 11:55:39 +04:00
|
|
|
EmittedMacroOnThisLine = false;
|
2008-09-05 07:22:57 +04:00
|
|
|
}
|
|
|
|
|
2009-12-07 04:42:56 +03:00
|
|
|
// Emit #line directives or GNU line markers depending on what mode we're in.
|
|
|
|
if (UseLineDirective) {
|
|
|
|
OS << "#line" << ' ' << LineNo << ' ' << '"';
|
2010-09-17 04:41:18 +04:00
|
|
|
OS.write(CurFilename.data(), CurFilename.size());
|
2009-12-07 04:42:56 +03:00
|
|
|
OS << '"';
|
|
|
|
} else {
|
|
|
|
OS << '#' << ' ' << LineNo << ' ' << '"';
|
2010-09-17 04:41:18 +04:00
|
|
|
OS.write(CurFilename.data(), CurFilename.size());
|
2009-12-07 04:42:56 +03:00
|
|
|
OS << '"';
|
2010-03-07 10:30:06 +03:00
|
|
|
|
2009-12-06 04:02:14 +03:00
|
|
|
if (ExtraLen)
|
|
|
|
OS.write(Extra, ExtraLen);
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2009-12-06 04:02:14 +03:00
|
|
|
if (FileType == SrcMgr::C_System)
|
|
|
|
OS.write(" 3", 2);
|
|
|
|
else if (FileType == SrcMgr::C_ExternCSystem)
|
|
|
|
OS.write(" 3 4", 4);
|
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
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.
|
2010-04-13 04:01:41 +04:00
|
|
|
bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
|
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)
|
2011-07-14 20:14:52 +04:00
|
|
|
return false; // Spelling line moved, but expansion 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
|
|
|
}
|
2010-06-12 20:20:56 +04:00
|
|
|
} else if (!DisableLineMarkers) {
|
|
|
|
// Emit a #line or line marker.
|
2008-09-05 07:22:57 +04:00
|
|
|
WriteLineInfo(LineNo, 0, 0);
|
2010-06-12 20:20:56 +04:00
|
|
|
} else {
|
|
|
|
// Okay, we're in -P mode, which turns off line markers. However, we still
|
|
|
|
// need to emit a newline between tokens on different lines.
|
|
|
|
if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
|
|
|
|
OS << '\n';
|
|
|
|
EmittedTokensOnThisLine = false;
|
|
|
|
EmittedMacroOnThisLine = false;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
}
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2009-09-09 19:08:12 +04:00
|
|
|
CurLine = LineNo;
|
2007-12-10 00:11:08 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2010-09-10 02:45:38 +04:00
|
|
|
bool PrintPPOutputPPCallbacks::StartNewLineIfNeeded() {
|
|
|
|
if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) {
|
|
|
|
OS << '\n';
|
|
|
|
EmittedTokensOnThisLine = false;
|
|
|
|
EmittedMacroOnThisLine = false;
|
|
|
|
++CurLine;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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,
|
2011-10-11 21:29:44 +04:00
|
|
|
SrcMgr::CharacteristicKind NewFileType,
|
|
|
|
FileID PrevFID) {
|
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.
|
2010-04-13 04:06:42 +04:00
|
|
|
SourceManager &SourceMgr = SM;
|
2010-04-13 04:01:41 +04:00
|
|
|
|
|
|
|
PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
|
2010-11-12 10:15:47 +03:00
|
|
|
if (UserLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
2010-04-14 08:28:50 +04:00
|
|
|
unsigned NewLine = UserLoc.getLine();
|
2010-08-25 02:44:13 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
2010-11-12 10:15:47 +03:00
|
|
|
SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
|
2009-01-30 21:44:17 +03:00
|
|
|
if (IncludeLoc.isValid())
|
|
|
|
MoveToLine(IncludeLoc);
|
2007-07-11 21:01:13 +04:00
|
|
|
} else if (Reason == PPCallbacks::SystemHeaderPragma) {
|
2010-04-13 04:01:41 +04:00
|
|
|
MoveToLine(NewLine);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04: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.
|
|
|
|
}
|
2010-04-13 04:01:41 +04:00
|
|
|
|
|
|
|
CurLine = NewLine;
|
2008-09-05 07:22:57 +04:00
|
|
|
|
2007-07-24 10:57:14 +04:00
|
|
|
CurFilename.clear();
|
2010-04-13 04:01:41 +04:00
|
|
|
CurFilename += UserLoc.getFilename();
|
2007-07-24 10:57:14 +04:00
|
|
|
Lexer::Stringify(CurFilename);
|
2008-09-05 07:22:57 +04:00
|
|
|
FileType = NewFileType;
|
|
|
|
|
2010-08-25 02:44:13 +04:00
|
|
|
if (DisableLineMarkers) return;
|
|
|
|
|
2008-09-05 07:22:57 +04:00
|
|
|
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;
|
2009-09-09 19:08:12 +04:00
|
|
|
case PPCallbacks::SystemHeaderPragma:
|
|
|
|
case PPCallbacks::RenameFile:
|
2008-09-05 07:22:57 +04:00
|
|
|
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);
|
2009-09-09 19:08:12 +04:00
|
|
|
|
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.
|
2010-11-20 00:33:15 +03:00
|
|
|
void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
|
2009-04-12 05:56:53 +04:00
|
|
|
const MacroInfo *MI) {
|
|
|
|
// Only print out macro definitions in -dD mode.
|
|
|
|
if (!DumpDefines ||
|
|
|
|
// Ignore __FILE__ etc.
|
|
|
|
MI->isBuiltinMacro()) return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-04-12 05:56:53 +04:00
|
|
|
MoveToLine(MI->getDefinitionLoc());
|
2010-11-20 00:33:15 +03:00
|
|
|
PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
|
2009-06-02 11:55:39 +04:00
|
|
|
EmittedMacroOnThisLine = true;
|
2009-04-12 05:56:53 +04:00
|
|
|
}
|
|
|
|
|
2010-11-20 00:33:15 +03:00
|
|
|
void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
|
2010-08-08 02:27:00 +04:00
|
|
|
const MacroInfo *MI) {
|
|
|
|
// Only print out macro definitions in -dD mode.
|
|
|
|
if (!DumpDefines) return;
|
|
|
|
|
2010-11-20 00:33:15 +03:00
|
|
|
MoveToLine(MacroNameTok.getLocation());
|
|
|
|
OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
|
2010-08-08 02:27:00 +04:00
|
|
|
EmittedMacroOnThisLine = true;
|
|
|
|
}
|
2009-04-12 05:56:53 +04:00
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
|
2009-09-09 19:08:12 +04:00
|
|
|
const IdentifierInfo *Kind,
|
2009-01-16 22:25:54 +03:00
|
|
|
const std::string &Str) {
|
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma comment(" << Kind->getName();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
if (!Str.empty()) {
|
|
|
|
OS << ", \"";
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
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 << '"';
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-01-16 22:25:54 +03:00
|
|
|
OS << ')';
|
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
|
|
|
|
2010-06-26 21:11:39 +04:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
|
2011-07-23 14:55:15 +04:00
|
|
|
StringRef Str) {
|
2010-06-26 21:11:39 +04:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma message(";
|
|
|
|
|
|
|
|
OS << '"';
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
|
|
|
|
unsigned char Char = Str[i];
|
|
|
|
if (isprint(Char) && Char != '\\' && Char != '"')
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-22 23:41:48 +04:00
|
|
|
void PrintPPOutputPPCallbacks::
|
2011-07-23 14:55:15 +04:00
|
|
|
PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
|
2011-06-22 23:41:48 +04:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic push";
|
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPPOutputPPCallbacks::
|
2011-07-23 14:55:15 +04:00
|
|
|
PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
|
2011-06-22 23:41:48 +04:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic pop";
|
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPPOutputPPCallbacks::
|
2011-07-23 14:55:15 +04:00
|
|
|
PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
|
2012-02-08 02:29:24 +04:00
|
|
|
diag::Mapping Map, StringRef Str) {
|
2011-06-22 23:41:48 +04:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic ";
|
2012-02-08 02:29:24 +04:00
|
|
|
switch (Map) {
|
2011-06-22 23:41:48 +04:00
|
|
|
case diag::MAP_WARNING:
|
|
|
|
OS << "warning";
|
|
|
|
break;
|
|
|
|
case diag::MAP_ERROR:
|
|
|
|
OS << "error";
|
|
|
|
break;
|
|
|
|
case diag::MAP_IGNORE:
|
|
|
|
OS << "ignored";
|
|
|
|
break;
|
|
|
|
case diag::MAP_FATAL:
|
|
|
|
OS << "fatal";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
OS << " \"" << Str << '"';
|
|
|
|
EmittedTokensOnThisLine = true;
|
|
|
|
}
|
2009-01-16 22:25:54 +03:00
|
|
|
|
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"
|
2011-07-14 20:14:52 +04:00
|
|
|
/// marker is set for spelling lines, not expansion 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;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
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.
|
2011-07-26 00:57:57 +04:00
|
|
|
unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
|
2009-09-09 19:08:12 +04:00
|
|
|
|
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 << ' ';
|
2009-09-09 19:08:12 +04:00
|
|
|
|
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 << ' ';
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-12-10 00:11:08 +03:00
|
|
|
return true;
|
2007-07-11 21:01:13 +04:00
|
|
|
}
|
|
|
|
|
2009-06-15 05:25:23 +04:00
|
|
|
void PrintPPOutputPPCallbacks::HandleNewlinesInToken(const char *TokStr,
|
|
|
|
unsigned Len) {
|
|
|
|
unsigned NumNewlines = 0;
|
|
|
|
for (; Len; --Len, ++TokStr) {
|
|
|
|
if (*TokStr != '\n' &&
|
|
|
|
*TokStr != '\r')
|
|
|
|
continue;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-15 05:25:23 +04:00
|
|
|
++NumNewlines;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-15 05:25:23 +04:00
|
|
|
// If we have \n\r or \r\n, skip both and count as one line.
|
|
|
|
if (Len != 1 &&
|
|
|
|
(TokStr[1] == '\n' || TokStr[1] == '\r') &&
|
|
|
|
TokStr[0] != TokStr[1])
|
|
|
|
++TokStr, --Len;
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-15 05:25:23 +04:00
|
|
|
if (NumNewlines == 0) return;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-06-15 08:08:28 +04:00
|
|
|
CurLine += NumNewlines;
|
2009-06-15 05:25:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
namespace {
|
|
|
|
struct UnknownPragmaHandler : public PragmaHandler {
|
|
|
|
const char *Prefix;
|
|
|
|
PrintPPOutputPPCallbacks *Callbacks;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
|
2010-07-13 13:07:17 +04:00
|
|
|
: Prefix(prefix), Callbacks(callbacks) {}
|
2010-09-10 02:45:38 +04:00
|
|
|
virtual void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
|
|
|
|
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.
|
2010-09-11 02:27:29 +04:00
|
|
|
Callbacks->StartNewLineIfNeeded();
|
2007-07-11 21:01:13 +04:00
|
|
|
Callbacks->MoveToLine(PragmaTok.getLocation());
|
2008-08-17 07:12:02 +04:00
|
|
|
Callbacks->OS.write(Prefix, strlen(Prefix));
|
2010-09-10 02:45:38 +04:00
|
|
|
Callbacks->SetEmittedTokensOnThisLine();
|
2007-07-11 21:01:13 +04:00
|
|
|
// Read and print all of the pragma tokens.
|
2011-02-28 05:37:51 +03:00
|
|
|
while (PragmaTok.isNot(tok::eod)) {
|
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);
|
|
|
|
}
|
2010-09-10 02:45:38 +04:00
|
|
|
Callbacks->StartNewLineIfNeeded();
|
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,
|
2011-07-23 14:55:15 +04:00
|
|
|
raw_ostream &OS) {
|
2010-02-27 21:02:51 +03:00
|
|
|
char Buffer[256];
|
2010-06-16 01:06:38 +04:00
|
|
|
Token PrevPrevTok, PrevTok;
|
|
|
|
PrevPrevTok.startToken();
|
|
|
|
PrevTok.startToken();
|
2009-02-06 08:56:11 +03:00
|
|
|
while (1) {
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-06 08:56:11 +03:00
|
|
|
// If this token is at the start of a line, emit newlines if needed.
|
|
|
|
if (Tok.isAtStartOfLine() && Callbacks->HandleFirstTokOnLine(Tok)) {
|
|
|
|
// done.
|
2009-09-09 19:08:12 +04:00
|
|
|
} else if (Tok.hasLeadingSpace() ||
|
2009-02-06 08:56:11 +03: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.
|
|
|
|
(Callbacks->hasEmittedTokensOnThisLine() &&
|
|
|
|
// Don't print "-" next to "-", it would form "--".
|
2010-04-14 07:57:19 +04:00
|
|
|
Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
|
2009-02-06 08:56:11 +03:00
|
|
|
OS << ' ';
|
|
|
|
}
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-02-27 21:02:51 +03:00
|
|
|
if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
|
|
|
OS << II->getName();
|
|
|
|
} 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);
|
|
|
|
|
|
|
|
// Tokens that can contain embedded newlines need to adjust our current
|
|
|
|
// line number.
|
|
|
|
if (Tok.getKind() == tok::comment)
|
|
|
|
Callbacks->HandleNewlinesInToken(TokPtr, Len);
|
|
|
|
} else {
|
|
|
|
std::string S = PP.getSpelling(Tok);
|
|
|
|
OS.write(&S[0], S.size());
|
|
|
|
|
|
|
|
// Tokens that can contain embedded newlines need to adjust our current
|
|
|
|
// line number.
|
|
|
|
if (Tok.getKind() == tok::comment)
|
|
|
|
Callbacks->HandleNewlinesInToken(&S[0], S.size());
|
|
|
|
}
|
2009-02-06 08:56:11 +03:00
|
|
|
Callbacks->SetEmittedTokensOnThisLine();
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2009-02-06 08:56:11 +03:00
|
|
|
if (Tok.is(tok::eof)) break;
|
2009-09-09 19:08:12 +04:00
|
|
|
|
2010-04-14 07:57:19 +04:00
|
|
|
PrevPrevTok = PrevTok;
|
2009-02-06 08:56:11 +03:00
|
|
|
PrevTok = Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-19 20:42:20 +03:00
|
|
|
typedef std::pair<IdentifierInfo*, MacroInfo*> id_macro_pair;
|
|
|
|
static int MacroIDCompare(const void* a, const void* b) {
|
|
|
|
const id_macro_pair *LHS = static_cast<const id_macro_pair*>(a);
|
|
|
|
const id_macro_pair *RHS = static_cast<const id_macro_pair*>(b);
|
|
|
|
return LHS->first->getName().compare(RHS->first->getName());
|
2009-02-11 01:28:19 +03:00
|
|
|
}
|
2009-02-06 08:56:11 +03:00
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
|
2010-06-12 00:10:12 +04:00
|
|
|
// Ignore unknown pragmas.
|
2010-07-13 13:07:17 +04:00
|
|
|
PP.AddPragmaHandler(new EmptyPragmaHandler());
|
2010-06-12 00:10:12 +04:00
|
|
|
|
2009-05-19 05:32:34 +04:00
|
|
|
// -dM mode just scans and ignores all tokens in the files, then dumps out
|
|
|
|
// the macro table at the end.
|
2010-04-21 00:35:58 +04:00
|
|
|
PP.EnterMainSourceFile();
|
2009-05-19 05:32:34 +04:00
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
do PP.Lex(Tok);
|
|
|
|
while (Tok.isNot(tok::eof));
|
|
|
|
|
2011-07-23 14:55:15 +04:00
|
|
|
SmallVector<id_macro_pair, 128>
|
2010-01-19 20:42:20 +03:00
|
|
|
MacrosByID(PP.macro_begin(), PP.macro_end());
|
|
|
|
llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
|
2009-05-19 05:32:34 +04:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
|
|
|
|
MacroInfo &MI = *MacrosByID[i].second;
|
2009-09-09 19:08:12 +04:00
|
|
|
// Ignore computed macros like __LINE__ and friends.
|
2009-05-19 05:32:34 +04:00
|
|
|
if (MI.isBuiltinMacro()) continue;
|
|
|
|
|
|
|
|
PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
|
2010-01-19 20:42:20 +03:00
|
|
|
*OS << '\n';
|
2009-05-19 05:32:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-11 21:01:13 +04:00
|
|
|
/// DoPrintPreprocessedInput - This implements -E mode.
|
|
|
|
///
|
2011-07-23 14:55:15 +04:00
|
|
|
void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
|
2009-11-11 13:07:44 +03:00
|
|
|
const PreprocessorOutputOptions &Opts) {
|
2009-11-11 13:07:22 +03:00
|
|
|
// Show macros with no output is handled specially.
|
|
|
|
if (!Opts.ShowCPP) {
|
|
|
|
assert(Opts.ShowMacros && "Not yet implemented!");
|
|
|
|
DoPrintMacros(PP, OS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
2009-11-11 13:07:22 +03:00
|
|
|
PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
|
2009-05-19 05:02:07 +04:00
|
|
|
|
2009-05-19 07:06:47 +04:00
|
|
|
PrintPPOutputPPCallbacks *Callbacks =
|
2009-11-11 13:07:22 +03:00
|
|
|
new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers,
|
2011-02-02 18:41:17 +03:00
|
|
|
Opts.ShowMacros);
|
2010-07-13 13:07:17 +04:00
|
|
|
PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks));
|
2010-11-10 04:00:49 +03:00
|
|
|
PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
|
|
|
|
PP.AddPragmaHandler("clang",
|
|
|
|
new UnknownPragmaHandler("#pragma clang", Callbacks));
|
2009-05-19 05:32:34 +04:00
|
|
|
|
2010-03-07 10:30:06 +03:00
|
|
|
PP.addPPCallbacks(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.
|
2010-04-21 00:35:58 +04:00
|
|
|
PP.EnterMainSourceFile();
|
2009-05-19 05:32:34 +04: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();
|
|
|
|
Token Tok;
|
2010-11-12 10:15:47 +03:00
|
|
|
do {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
if (Tok.is(tok::eof) || !Tok.getLocation().isFileID())
|
|
|
|
break;
|
|
|
|
|
|
|
|
PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation());
|
|
|
|
if (PLoc.isInvalid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (strcmp(PLoc.getFilename(), "<built-in>"))
|
|
|
|
break;
|
|
|
|
} while (true);
|
2009-05-19 05:32:34 +04:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|