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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-05-19 12:14:29 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2009-11-11 18:07:22 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Frontend/PreprocessorOutputOptions.h"
|
2009-02-06 14:45:26 +08:00
|
|
|
#include "clang/Lex/MacroInfo.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/Pragma.h"
|
2009-11-11 18:07:22 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2009-02-13 08:46:04 +08:00
|
|
|
#include "clang/Lex/TokenConcatenation.h"
|
2010-01-20 01:42:20 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-06-27 01:11:39 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2011-06-23 03:41:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-04 17:13:33 +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;
|
|
|
|
|
2009-04-12 09:56:53 +08: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 18:55:15 +08:00
|
|
|
Preprocessor &PP, raw_ostream &OS) {
|
2009-04-12 09:56:53 +08:00
|
|
|
OS << "#define " << II.getName();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 09:56:53 +08:00
|
|
|
if (MI.isFunctionLike()) {
|
|
|
|
OS << '(';
|
2009-12-09 10:08:14 +08:00
|
|
|
if (!MI.arg_empty()) {
|
2009-04-12 09:56:53 +08:00
|
|
|
MacroInfo::arg_iterator AI = MI.arg_begin(), E = MI.arg_end();
|
2009-12-09 10:08:14 +08:00
|
|
|
for (; AI+1 != E; ++AI) {
|
|
|
|
OS << (*AI)->getName();
|
2009-04-12 09:56:53 +08:00
|
|
|
OS << ',';
|
2009-12-07 09:58:34 +08:00
|
|
|
}
|
2009-12-09 10:08:14 +08:00
|
|
|
|
|
|
|
// Last argument.
|
|
|
|
if ((*AI)->getName() == "__VA_ARGS__")
|
|
|
|
OS << "...";
|
|
|
|
else
|
|
|
|
OS << (*AI)->getName();
|
2009-04-12 09:56:53 +08:00
|
|
|
}
|
2009-12-07 09:58:34 +08:00
|
|
|
|
|
|
|
if (MI.isGNUVarargs())
|
|
|
|
OS << "..."; // #define foo(x...)
|
2010-03-07 15:30:06 +08:00
|
|
|
|
2009-04-12 09:56:53 +08:00
|
|
|
OS << ')';
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 09:56:53 +08: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 23:08:12 +08:00
|
|
|
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> SpellingBuffer;
|
2015-05-11 16:25:54 +08:00
|
|
|
for (const auto &T : MI.tokens()) {
|
|
|
|
if (T.hasLeadingSpace())
|
2009-04-12 09:56:53 +08:00
|
|
|
OS << ' ';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-05-11 16:25:54 +08:00
|
|
|
OS << PP.getSpelling(T, SpellingBuffer);
|
2009-04-12 09:56:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 06:19:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessed token printer
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-21 13:02:33 +08:00
|
|
|
namespace {
|
|
|
|
class PrintPPOutputPPCallbacks : public PPCallbacks {
|
|
|
|
Preprocessor &PP;
|
2010-04-13 08:06:42 +08:00
|
|
|
SourceManager &SM;
|
2009-02-13 08:46:04 +08:00
|
|
|
TokenConcatenation ConcatInfo;
|
2008-08-17 11:12:02 +08:00
|
|
|
public:
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &OS;
|
2008-08-17 11:12:02 +08:00
|
|
|
private:
|
2006-11-21 13:02:33 +08:00
|
|
|
unsigned CurLine;
|
2010-08-25 06:44:13 +08:00
|
|
|
|
2006-11-21 13:02:33 +08:00
|
|
|
bool EmittedTokensOnThisLine;
|
2012-06-16 07:33:51 +08:00
|
|
|
bool EmittedDirectiveOnThisLine;
|
2008-10-27 09:19:25 +08:00
|
|
|
SrcMgr::CharacteristicKind FileType;
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<512> CurFilename;
|
2008-09-05 11:22:57 +08:00
|
|
|
bool Initialized;
|
2009-05-19 11:06:47 +08:00
|
|
|
bool DisableLineMarkers;
|
|
|
|
bool DumpDefines;
|
2015-02-26 08:17:25 +08:00
|
|
|
bool UseLineDirectives;
|
2012-11-16 09:51:11 +08:00
|
|
|
bool IsFirstFileEntered;
|
2006-11-21 13:02:33 +08:00
|
|
|
public:
|
2015-02-26 08:17:25 +08:00
|
|
|
PrintPPOutputPPCallbacks(Preprocessor &pp, raw_ostream &os, bool lineMarkers,
|
|
|
|
bool defines, bool UseLineDirectives)
|
|
|
|
: PP(pp), SM(PP.getSourceManager()), ConcatInfo(PP), OS(os),
|
|
|
|
DisableLineMarkers(lineMarkers), DumpDefines(defines),
|
|
|
|
UseLineDirectives(UseLineDirectives) {
|
2011-02-02 23:41:17 +08:00
|
|
|
CurLine = 0;
|
2007-07-24 14:57:14 +08:00
|
|
|
CurFilename += "<uninit>";
|
2006-11-21 13:02:33 +08:00
|
|
|
EmittedTokensOnThisLine = false;
|
2012-06-16 07:33:51 +08:00
|
|
|
EmittedDirectiveOnThisLine = false;
|
2008-09-27 05:18:42 +08:00
|
|
|
FileType = SrcMgr::C_User;
|
2008-09-05 11:22:57 +08:00
|
|
|
Initialized = false;
|
2012-11-16 09:51:11 +08:00
|
|
|
IsFirstFileEntered = false;
|
2006-11-21 13:02:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-16 07:33:51 +08:00
|
|
|
void setEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
|
2007-07-23 14:09:34 +08:00
|
|
|
bool hasEmittedTokensOnThisLine() const { return EmittedTokensOnThisLine; }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-06-16 07:33:51 +08:00
|
|
|
void setEmittedDirectiveOnThisLine() { EmittedDirectiveOnThisLine = true; }
|
|
|
|
bool hasEmittedDirectiveOnThisLine() const {
|
|
|
|
return EmittedDirectiveOnThisLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startNewLineIfNeeded(bool ShouldUpdateCurrentLine = true);
|
2014-03-13 14:07:04 +08:00
|
|
|
|
|
|
|
void FileChanged(SourceLocation Loc, FileChangeReason Reason,
|
|
|
|
SrcMgr::CharacteristicKind FileType,
|
|
|
|
FileID PrevFID) override;
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
|
|
|
const Module *Imported) override;
|
2015-06-02 04:00:16 +08:00
|
|
|
void Ident(SourceLocation Loc, StringRef str) override;
|
2014-03-13 14:07:04 +08:00
|
|
|
void PragmaMessage(SourceLocation Loc, StringRef Namespace,
|
|
|
|
PragmaMessageKind Kind, StringRef Str) override;
|
|
|
|
void PragmaDebug(SourceLocation Loc, StringRef DebugType) override;
|
|
|
|
void PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) override;
|
|
|
|
void PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) override;
|
|
|
|
void PragmaDiagnostic(SourceLocation Loc, StringRef Namespace,
|
2014-06-10 17:31:37 +08:00
|
|
|
diag::Severity Map, StringRef Str) override;
|
2014-03-13 14:07:04 +08:00
|
|
|
void PragmaWarning(SourceLocation Loc, StringRef WarningSpec,
|
|
|
|
ArrayRef<int> Ids) override;
|
|
|
|
void PragmaWarningPush(SourceLocation Loc, int Level) override;
|
|
|
|
void PragmaWarningPop(SourceLocation Loc) override;
|
2006-11-21 13:02:33 +08:00
|
|
|
|
2007-12-10 05:11:08 +08:00
|
|
|
bool HandleFirstTokOnLine(Token &Tok);
|
2013-01-28 12:37:37 +08:00
|
|
|
|
|
|
|
/// Move to the line of the provided source location. This will
|
|
|
|
/// return true if the output stream required adjustment or if
|
|
|
|
/// the requested location is on the first line.
|
2010-04-13 08:01:41 +08:00
|
|
|
bool MoveToLine(SourceLocation Loc) {
|
2010-11-12 15:15:47 +08:00
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
|
|
|
|
if (PLoc.isInvalid())
|
|
|
|
return false;
|
2013-01-28 12:37:37 +08:00
|
|
|
return MoveToLine(PLoc.getLine()) || (PLoc.getLine() == 1);
|
2010-04-13 08:01:41 +08:00
|
|
|
}
|
|
|
|
bool MoveToLine(unsigned LineNo);
|
|
|
|
|
2010-04-14 11:57:19 +08:00
|
|
|
bool AvoidConcat(const Token &PrevPrevTok, const Token &PrevTok,
|
|
|
|
const Token &Tok) {
|
|
|
|
return ConcatInfo.AvoidConcat(PrevPrevTok, PrevTok, Tok);
|
2009-02-13 08:46:04 +08:00
|
|
|
}
|
2014-05-22 12:46:25 +08:00
|
|
|
void WriteLineInfo(unsigned LineNo, const char *Extra=nullptr,
|
|
|
|
unsigned ExtraLen=0);
|
2010-09-10 06:45:38 +08:00
|
|
|
bool LineMarkersAreDisabled() const { return DisableLineMarkers; }
|
2009-06-15 09:25:23 +08:00
|
|
|
void HandleNewlinesInToken(const char *TokStr, unsigned Len);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 09:56:53 +08:00
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
2014-03-13 14:07:04 +08:00
|
|
|
void MacroDefined(const Token &MacroNameTok,
|
|
|
|
const MacroDirective *MD) override;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-08 06:27:00 +08:00
|
|
|
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
|
2014-03-13 14:07:04 +08:00
|
|
|
void MacroUndefined(const Token &MacroNameTok,
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroDefinition &MD) override;
|
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) {
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2009-12-07 09:42:56 +08:00
|
|
|
// Emit #line directives or GNU line markers depending on what mode we're in.
|
2015-02-26 08:17:25 +08:00
|
|
|
if (UseLineDirectives) {
|
2009-12-07 09:42:56 +08:00
|
|
|
OS << "#line" << ' ' << LineNo << ' ' << '"';
|
2013-08-29 09:42:42 +08:00
|
|
|
OS.write_escaped(CurFilename);
|
2009-12-07 09:42:56 +08:00
|
|
|
OS << '"';
|
|
|
|
} else {
|
|
|
|
OS << '#' << ' ' << LineNo << ' ' << '"';
|
2013-08-29 09:42:42 +08:00
|
|
|
OS.write_escaped(CurFilename);
|
2009-12-07 09:42:56 +08:00
|
|
|
OS << '"';
|
2010-03-07 15:30:06 +08:00
|
|
|
|
2009-12-06 09:02:14 +08:00
|
|
|
if (ExtraLen)
|
|
|
|
OS.write(Extra, ExtraLen);
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2009-12-06 09:02:14 +08:00
|
|
|
if (FileType == SrcMgr::C_System)
|
|
|
|
OS.write(" 3", 2);
|
|
|
|
else if (FileType == SrcMgr::C_ExternCSystem)
|
|
|
|
OS.write(" 3 4", 4);
|
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
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.
|
2010-04-13 08:01:41 +08:00
|
|
|
bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) {
|
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)
|
2011-07-15 00:14:52 +08:00
|
|
|
return false; // Spelling line moved, but expansion 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
|
|
|
}
|
2010-06-13 00:20:56 +08:00
|
|
|
} else if (!DisableLineMarkers) {
|
|
|
|
// Emit a #line or line marker.
|
2014-05-22 12:46:25 +08:00
|
|
|
WriteLineInfo(LineNo, nullptr, 0);
|
2010-06-13 00:20:56 +08: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.
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
CurLine = LineNo;
|
2007-12-10 05:11:08 +08:00
|
|
|
return true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2012-06-16 07:33:51 +08:00
|
|
|
bool
|
|
|
|
PrintPPOutputPPCallbacks::startNewLineIfNeeded(bool ShouldUpdateCurrentLine) {
|
|
|
|
if (EmittedTokensOnThisLine || EmittedDirectiveOnThisLine) {
|
2010-09-10 06:45:38 +08:00
|
|
|
OS << '\n';
|
|
|
|
EmittedTokensOnThisLine = false;
|
2012-06-16 07:33:51 +08:00
|
|
|
EmittedDirectiveOnThisLine = false;
|
|
|
|
if (ShouldUpdateCurrentLine)
|
|
|
|
++CurLine;
|
2010-09-10 06:45:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
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,
|
2011-10-12 01:29:44 +08:00
|
|
|
SrcMgr::CharacteristicKind NewFileType,
|
|
|
|
FileID PrevFID) {
|
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.
|
2010-04-13 08:06:42 +08:00
|
|
|
SourceManager &SourceMgr = SM;
|
2010-04-13 08:01:41 +08:00
|
|
|
|
|
|
|
PresumedLoc UserLoc = SourceMgr.getPresumedLoc(Loc);
|
2010-11-12 15:15:47 +08:00
|
|
|
if (UserLoc.isInvalid())
|
|
|
|
return;
|
|
|
|
|
2010-04-14 12:28:50 +08:00
|
|
|
unsigned NewLine = UserLoc.getLine();
|
2010-08-25 06:44:13 +08:00
|
|
|
|
2006-11-21 12:09:30 +08:00
|
|
|
if (Reason == PPCallbacks::EnterFile) {
|
2010-11-12 15:15:47 +08:00
|
|
|
SourceLocation IncludeLoc = UserLoc.getIncludeLoc();
|
2009-01-31 02:44:17 +08:00
|
|
|
if (IncludeLoc.isValid())
|
|
|
|
MoveToLine(IncludeLoc);
|
2006-11-21 12:09:30 +08:00
|
|
|
} else if (Reason == PPCallbacks::SystemHeaderPragma) {
|
2013-04-18 03:09:18 +08:00
|
|
|
// GCC emits the # directive for this directive on the line AFTER the
|
|
|
|
// directive and emits a bunch of spaces that aren't needed. This is because
|
|
|
|
// otherwise we will emit a line marker for THIS line, which requires an
|
|
|
|
// extra blank line after the directive to avoid making all following lines
|
|
|
|
// off by one. We can do better by simply incrementing NewLine here.
|
|
|
|
NewLine += 1;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2010-04-13 08:01:41 +08:00
|
|
|
|
|
|
|
CurLine = NewLine;
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2007-07-24 14:57:14 +08:00
|
|
|
CurFilename.clear();
|
2010-04-13 08:01:41 +08:00
|
|
|
CurFilename += UserLoc.getFilename();
|
2008-09-05 11:22:57 +08:00
|
|
|
FileType = NewFileType;
|
|
|
|
|
2013-01-09 11:16:42 +08:00
|
|
|
if (DisableLineMarkers) {
|
|
|
|
startNewLineIfNeeded(/*ShouldUpdateCurrentLine=*/false);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-25 06:44:13 +08:00
|
|
|
|
2008-09-05 11:22:57 +08:00
|
|
|
if (!Initialized) {
|
|
|
|
WriteLineInfo(CurLine);
|
|
|
|
Initialized = true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2008-09-05 11:22:57 +08:00
|
|
|
|
2012-11-16 09:51:11 +08:00
|
|
|
// Do not emit an enter marker for the main file (which we expect is the first
|
|
|
|
// entered file). This matches gcc, and improves compatibility with some tools
|
|
|
|
// which track the # line markers as a way to determine when the preprocessed
|
|
|
|
// output is in the context of the main file.
|
|
|
|
if (Reason == PPCallbacks::EnterFile && !IsFirstFileEntered) {
|
|
|
|
IsFirstFileEntered = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
case PPCallbacks::SystemHeaderPragma:
|
|
|
|
case PPCallbacks::RenameFile:
|
2008-09-05 11:22:57 +08:00
|
|
|
WriteLineInfo(CurLine);
|
2006-07-05 05:19:39 +08:00
|
|
|
break;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 09:53:46 +08:00
|
|
|
void PrintPPOutputPPCallbacks::InclusionDirective(SourceLocation HashLoc,
|
|
|
|
const Token &IncludeTok,
|
|
|
|
StringRef FileName,
|
|
|
|
bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange,
|
|
|
|
const FileEntry *File,
|
|
|
|
StringRef SearchPath,
|
|
|
|
StringRef RelativePath,
|
|
|
|
const Module *Imported) {
|
|
|
|
// When preprocessing, turn implicit imports into @imports.
|
|
|
|
// FIXME: This is a stop-gap until a more comprehensive "preprocessing with
|
|
|
|
// modules" solution is introduced.
|
|
|
|
if (Imported) {
|
|
|
|
startNewLineIfNeeded();
|
|
|
|
MoveToLine(HashLoc);
|
|
|
|
OS << "@import " << Imported->getFullModuleName() << ";"
|
|
|
|
<< " /* clang -E: implicit import for \"" << File->getName() << "\" */";
|
2014-09-11 05:29:41 +08:00
|
|
|
// Since we want a newline after the @import, but not a #<line>, start a new
|
|
|
|
// line immediately.
|
2013-04-30 01:26:22 +08:00
|
|
|
EmittedTokensOnThisLine = true;
|
2014-09-11 05:29:41 +08:00
|
|
|
startNewLineIfNeeded();
|
2013-04-10 09:53:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-17 03:25:54 +08:00
|
|
|
/// Ident - Handle #ident directives when read by the preprocessor.
|
2006-07-05 05:28:37 +08:00
|
|
|
///
|
2015-06-02 04:00:16 +08:00
|
|
|
void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, StringRef S) {
|
2006-07-05 05:19:39 +08:00
|
|
|
MoveToLine(Loc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-17 11:12:02 +08:00
|
|
|
OS.write("#ident ", strlen("#ident "));
|
2015-06-02 04:00:16 +08:00
|
|
|
OS.write(S.begin(), S.size());
|
2006-11-21 13:02:33 +08:00
|
|
|
EmittedTokensOnThisLine = true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2009-04-12 09:56:53 +08:00
|
|
|
/// MacroDefined - This hook is called whenever a macro definition is seen.
|
2010-11-20 05:33:15 +08:00
|
|
|
void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
|
2013-02-24 08:05:14 +08:00
|
|
|
const MacroDirective *MD) {
|
2013-03-27 01:17:01 +08:00
|
|
|
const MacroInfo *MI = MD->getMacroInfo();
|
2009-04-12 09:56:53 +08:00
|
|
|
// Only print out macro definitions in -dD mode.
|
|
|
|
if (!DumpDefines ||
|
|
|
|
// Ignore __FILE__ etc.
|
|
|
|
MI->isBuiltinMacro()) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 09:56:53 +08:00
|
|
|
MoveToLine(MI->getDefinitionLoc());
|
2010-11-20 05:33:15 +08:00
|
|
|
PrintMacroDefinition(*MacroNameTok.getIdentifierInfo(), *MI, PP, OS);
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2009-04-12 09:56:53 +08:00
|
|
|
}
|
|
|
|
|
2010-11-20 05:33:15 +08:00
|
|
|
void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
|
2015-05-04 11:15:40 +08:00
|
|
|
const MacroDefinition &MD) {
|
2010-08-08 06:27:00 +08:00
|
|
|
// Only print out macro definitions in -dD mode.
|
|
|
|
if (!DumpDefines) return;
|
|
|
|
|
2010-11-20 05:33:15 +08:00
|
|
|
MoveToLine(MacroNameTok.getLocation());
|
|
|
|
OS << "#undef " << MacroNameTok.getIdentifierInfo()->getName();
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2010-08-08 06:27:00 +08:00
|
|
|
}
|
2009-04-12 09:56:53 +08:00
|
|
|
|
2013-06-04 10:07:14 +08:00
|
|
|
static void outputPrintable(llvm::raw_ostream& OS,
|
2009-01-17 03:25:54 +08:00
|
|
|
const std::string &Str) {
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i) {
|
|
|
|
unsigned char Char = Str[i];
|
2013-02-09 06:30:41 +08:00
|
|
|
if (isPrintable(Char) && Char != '\\' && Char != '"')
|
2009-01-17 03:25:54 +08: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));
|
|
|
|
}
|
2013-06-04 10:07:14 +08:00
|
|
|
}
|
|
|
|
|
2010-06-27 01:11:39 +08:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
|
2013-04-18 00:16:16 +08:00
|
|
|
StringRef Namespace,
|
|
|
|
PragmaMessageKind Kind,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Str) {
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded();
|
2010-06-27 01:11:39 +08:00
|
|
|
MoveToLine(Loc);
|
2013-04-18 00:16:16 +08:00
|
|
|
OS << "#pragma ";
|
|
|
|
if (!Namespace.empty())
|
|
|
|
OS << Namespace << ' ';
|
|
|
|
switch (Kind) {
|
|
|
|
case PMK_Message:
|
2013-04-20 01:13:17 +08:00
|
|
|
OS << "message(\"";
|
2013-04-18 00:16:16 +08:00
|
|
|
break;
|
|
|
|
case PMK_Warning:
|
2013-04-19 00:49:37 +08:00
|
|
|
OS << "warning \"";
|
2013-04-18 00:16:16 +08:00
|
|
|
break;
|
|
|
|
case PMK_Error:
|
2013-04-19 00:49:37 +08:00
|
|
|
OS << "error \"";
|
2013-04-18 00:16:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2010-06-27 01:11:39 +08:00
|
|
|
|
2013-06-04 10:07:14 +08:00
|
|
|
outputPrintable(OS, Str);
|
2010-06-27 01:11:39 +08:00
|
|
|
OS << '"';
|
2013-04-20 01:13:17 +08:00
|
|
|
if (Kind == PMK_Message)
|
|
|
|
OS << ')';
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2010-06-27 01:11:39 +08:00
|
|
|
}
|
|
|
|
|
2013-04-17 02:41:26 +08:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
|
|
|
|
StringRef DebugType) {
|
|
|
|
startNewLineIfNeeded();
|
|
|
|
MoveToLine(Loc);
|
|
|
|
|
|
|
|
OS << "#pragma clang __debug ";
|
|
|
|
OS << DebugType;
|
|
|
|
|
|
|
|
setEmittedDirectiveOnThisLine();
|
|
|
|
}
|
|
|
|
|
2011-06-23 03:41:48 +08:00
|
|
|
void PrintPPOutputPPCallbacks::
|
2011-07-23 18:55:15 +08:00
|
|
|
PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded();
|
2011-06-23 03:41:48 +08:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic push";
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2011-06-23 03:41:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPPOutputPPCallbacks::
|
2011-07-23 18:55:15 +08:00
|
|
|
PragmaDiagnosticPop(SourceLocation Loc, StringRef Namespace) {
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded();
|
2011-06-23 03:41:48 +08:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic pop";
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2011-06-23 03:41:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 17:31:37 +08:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaDiagnostic(SourceLocation Loc,
|
|
|
|
StringRef Namespace,
|
|
|
|
diag::Severity Map,
|
|
|
|
StringRef Str) {
|
2012-06-16 07:33:51 +08:00
|
|
|
startNewLineIfNeeded();
|
2011-06-23 03:41:48 +08:00
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma " << Namespace << " diagnostic ";
|
2012-02-08 06:29:24 +08:00
|
|
|
switch (Map) {
|
2014-06-12 18:15:20 +08:00
|
|
|
case diag::Severity::Remark:
|
2014-02-28 17:11:08 +08:00
|
|
|
OS << "remark";
|
|
|
|
break;
|
2014-06-12 18:15:20 +08:00
|
|
|
case diag::Severity::Warning:
|
2011-06-23 03:41:48 +08:00
|
|
|
OS << "warning";
|
|
|
|
break;
|
2014-06-12 18:15:20 +08:00
|
|
|
case diag::Severity::Error:
|
2011-06-23 03:41:48 +08:00
|
|
|
OS << "error";
|
|
|
|
break;
|
2014-06-12 18:15:20 +08:00
|
|
|
case diag::Severity::Ignored:
|
2011-06-23 03:41:48 +08:00
|
|
|
OS << "ignored";
|
|
|
|
break;
|
2014-06-12 18:15:20 +08:00
|
|
|
case diag::Severity::Fatal:
|
2011-06-23 03:41:48 +08:00
|
|
|
OS << "fatal";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
OS << " \"" << Str << '"';
|
2012-06-16 07:33:51 +08:00
|
|
|
setEmittedDirectiveOnThisLine();
|
2011-06-23 03:41:48 +08:00
|
|
|
}
|
2009-01-17 03:25:54 +08:00
|
|
|
|
2013-09-14 06:00:30 +08:00
|
|
|
void PrintPPOutputPPCallbacks::PragmaWarning(SourceLocation Loc,
|
|
|
|
StringRef WarningSpec,
|
|
|
|
ArrayRef<int> Ids) {
|
|
|
|
startNewLineIfNeeded();
|
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma warning(" << WarningSpec << ':';
|
|
|
|
for (ArrayRef<int>::iterator I = Ids.begin(), E = Ids.end(); I != E; ++I)
|
|
|
|
OS << ' ' << *I;
|
|
|
|
OS << ')';
|
|
|
|
setEmittedDirectiveOnThisLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPPOutputPPCallbacks::PragmaWarningPush(SourceLocation Loc,
|
|
|
|
int Level) {
|
|
|
|
startNewLineIfNeeded();
|
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma warning(push";
|
2013-10-02 23:19:23 +08:00
|
|
|
if (Level >= 0)
|
2013-09-14 06:00:30 +08:00
|
|
|
OS << ", " << Level;
|
|
|
|
OS << ')';
|
|
|
|
setEmittedDirectiveOnThisLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPPOutputPPCallbacks::PragmaWarningPop(SourceLocation Loc) {
|
|
|
|
startNewLineIfNeeded();
|
|
|
|
MoveToLine(Loc);
|
|
|
|
OS << "#pragma warning(pop)";
|
|
|
|
setEmittedDirectiveOnThisLine();
|
|
|
|
}
|
|
|
|
|
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"
|
2011-07-15 00:14:52 +08:00
|
|
|
/// marker is set for spelling lines, not expansion ones.
|
2007-12-10 05:11:08 +08:00
|
|
|
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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2011-07-26 04:57:57 +08:00
|
|
|
unsigned ColNo = SM.getExpansionColumnNumber(Tok.getLocation());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-02-25 04:50:36 +08:00
|
|
|
// The first token on a line can have a column number of 1, yet still expect
|
|
|
|
// leading white space, if a macro expansion in column 1 starts with an empty
|
|
|
|
// macro argument, or an empty nested macro expansion. In this case, move the
|
|
|
|
// token to column 2.
|
|
|
|
if (ColNo == 1 && Tok.hasLeadingSpace())
|
|
|
|
ColNo = 2;
|
|
|
|
|
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 << ' ';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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 << ' ';
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-10 05:11:08 +08:00
|
|
|
return true;
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
|
|
|
|
2009-06-15 09:25:23 +08: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 23:08:12 +08:00
|
|
|
|
2009-06-15 09:25:23 +08:00
|
|
|
++NumNewlines;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-15 09:25:23 +08: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 23:08:12 +08:00
|
|
|
|
2009-06-15 09:25:23 +08:00
|
|
|
if (NumNewlines == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-06-15 12:08:28 +08:00
|
|
|
CurLine += NumNewlines;
|
2009-06-15 09:25:23 +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;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-06-16 07:44:27 +08:00
|
|
|
// Set to true if tokens should be expanded
|
|
|
|
bool ShouldExpandTokens;
|
|
|
|
|
|
|
|
UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks,
|
|
|
|
bool RequireTokenExpansion)
|
|
|
|
: Prefix(prefix), Callbacks(callbacks),
|
|
|
|
ShouldExpandTokens(RequireTokenExpansion) {}
|
2014-03-13 14:07:04 +08:00
|
|
|
void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
|
|
|
|
Token &PragmaTok) override {
|
2006-07-05 03:04:05 +08:00
|
|
|
// Figure out what line we went to and insert the appropriate number of
|
|
|
|
// newline characters.
|
2012-06-16 07:33:51 +08:00
|
|
|
Callbacks->startNewLineIfNeeded();
|
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));
|
2015-06-16 07:44:27 +08:00
|
|
|
|
|
|
|
Token PrevToken;
|
|
|
|
Token PrevPrevToken;
|
|
|
|
PrevToken.startToken();
|
|
|
|
PrevPrevToken.startToken();
|
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
// Read and print all of the pragma tokens.
|
2011-02-28 10:37:51 +08:00
|
|
|
while (PragmaTok.isNot(tok::eod)) {
|
2015-06-16 07:44:27 +08:00
|
|
|
if (PragmaTok.hasLeadingSpace() ||
|
|
|
|
Callbacks->AvoidConcat(PrevPrevToken, PrevToken, PragmaTok))
|
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());
|
2014-02-21 06:59:51 +08:00
|
|
|
|
2015-06-16 07:44:27 +08:00
|
|
|
PrevPrevToken = PrevToken;
|
|
|
|
PrevToken = PragmaTok;
|
|
|
|
|
|
|
|
if (ShouldExpandTokens)
|
2014-02-21 06:59:51 +08:00
|
|
|
PP.Lex(PragmaTok);
|
|
|
|
else
|
|
|
|
PP.LexUnexpandedToken(PragmaTok);
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|
2012-06-16 07:33:51 +08:00
|
|
|
Callbacks->setEmittedDirectiveOnThisLine();
|
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
|
|
|
|
2009-02-06 13:56:11 +08:00
|
|
|
static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok,
|
|
|
|
PrintPPOutputPPCallbacks *Callbacks,
|
2011-07-23 18:55:15 +08:00
|
|
|
raw_ostream &OS) {
|
2013-03-06 07:54:55 +08:00
|
|
|
bool DropComments = PP.getLangOpts().TraditionalCPP &&
|
|
|
|
!PP.getCommentRetentionState();
|
|
|
|
|
2010-02-28 02:02:51 +08:00
|
|
|
char Buffer[256];
|
2010-06-16 05:06:38 +08:00
|
|
|
Token PrevPrevTok, PrevTok;
|
|
|
|
PrevPrevTok.startToken();
|
|
|
|
PrevTok.startToken();
|
2009-02-06 13:56:11 +08:00
|
|
|
while (1) {
|
2012-06-16 07:33:51 +08:00
|
|
|
if (Callbacks->hasEmittedDirectiveOnThisLine()) {
|
|
|
|
Callbacks->startNewLineIfNeeded();
|
|
|
|
Callbacks->MoveToLine(Tok.getLocation());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-06 13:56:11 +08: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 23:08:12 +08:00
|
|
|
} else if (Tok.hasLeadingSpace() ||
|
2009-02-06 13:56:11 +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.
|
|
|
|
(Callbacks->hasEmittedTokensOnThisLine() &&
|
|
|
|
// Don't print "-" next to "-", it would form "--".
|
2010-04-14 11:57:19 +08:00
|
|
|
Callbacks->AvoidConcat(PrevPrevTok, PrevTok, Tok))) {
|
2009-02-06 13:56:11 +08:00
|
|
|
OS << ' ';
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-03-06 07:54:55 +08:00
|
|
|
if (DropComments && Tok.is(tok::comment)) {
|
|
|
|
// Skip comments. Normally the preprocessor does not generate
|
|
|
|
// tok::comment nodes at all when not keeping comments, but under
|
|
|
|
// -traditional-cpp the lexer keeps /all/ whitespace, including comments.
|
|
|
|
SourceLocation StartLoc = Tok.getLocation();
|
|
|
|
Callbacks->MoveToLine(StartLoc.getLocWithOffset(Tok.getLength()));
|
2014-01-31 05:50:18 +08:00
|
|
|
} else if (Tok.is(tok::annot_module_include) ||
|
|
|
|
Tok.is(tok::annot_module_begin) ||
|
|
|
|
Tok.is(tok::annot_module_end)) {
|
|
|
|
// PrintPPOutputPPCallbacks::InclusionDirective handles producing
|
|
|
|
// appropriate output here. Ignore this token entirely.
|
2013-11-15 12:24:58 +08:00
|
|
|
PP.Lex(Tok);
|
|
|
|
continue;
|
2013-03-06 07:54:55 +08:00
|
|
|
} else if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
|
2010-02-28 02:02:51 +08:00
|
|
|
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.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
|
2010-02-28 02:02:51 +08:00
|
|
|
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.
|
2013-02-22 02:53:19 +08:00
|
|
|
if (Tok.getKind() == tok::comment || Tok.getKind() == tok::unknown)
|
2010-02-28 02:02:51 +08:00
|
|
|
Callbacks->HandleNewlinesInToken(&S[0], S.size());
|
|
|
|
}
|
2012-06-16 07:33:51 +08:00
|
|
|
Callbacks->setEmittedTokensOnThisLine();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-06 13:56:11 +08:00
|
|
|
if (Tok.is(tok::eof)) break;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-04-14 11:57:19 +08:00
|
|
|
PrevPrevTok = PrevTok;
|
2009-02-06 13:56:11 +08:00
|
|
|
PrevTok = Tok;
|
|
|
|
PP.Lex(Tok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 05:51:58 +08:00
|
|
|
typedef std::pair<const IdentifierInfo *, MacroInfo *> id_macro_pair;
|
|
|
|
static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) {
|
|
|
|
return LHS->first->getName().compare(RHS->first->getName());
|
|
|
|
}
|
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) {
|
2010-06-12 04:10:12 +08:00
|
|
|
// Ignore unknown pragmas.
|
2014-05-01 20:54:03 +08:00
|
|
|
PP.IgnorePragmas();
|
2010-06-12 04:10:12 +08:00
|
|
|
|
2009-05-19 09:32:34 +08:00
|
|
|
// -dM mode just scans and ignores all tokens in the files, then dumps out
|
|
|
|
// the macro table at the end.
|
2010-04-21 04:35:58 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2009-05-19 09:32:34 +08:00
|
|
|
|
|
|
|
Token Tok;
|
|
|
|
do PP.Lex(Tok);
|
|
|
|
while (Tok.isNot(tok::eof));
|
|
|
|
|
Keep history of macro definitions and #undefs
Summary:
Summary: Keep history of macro definitions and #undefs with corresponding source locations, so that we can later find out all macros active in a specified source location. We don't save the history in PCH (no need currently). Memory overhead is about sizeof(void*)*3*<number of macro definitions and #undefs>+<in-memory size of all #undef'd macros>
I've run a test on a file composed of 109 .h files from boost 1.49 on x86-64 linux.
Stats before this patch:
*** Preprocessor Stats:
73222 directives found:
19171 #define.
4345 #undef.
#include/#include_next/#import:
5233 source files entered.
27 max include stack depth
19210 #if/#ifndef/#ifdef.
2384 #else/#elif.
6891 #endif.
408 #pragma.
14466 #if/#ifndef#ifdef regions skipped
80023/451669/1270 obj/fn/builtin macros expanded, 85724 on the fast path.
127145 token paste (##) operations performed, 11008 on the fast path.
Preprocessor Memory: 5874615B total
BumpPtr: 4399104
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
Stats with this patch:
...
Preprocessor Memory: 7541687B total
BumpPtr: 6066176
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
In my test increase in memory usage is about 1.7Mb, which is ~28% of initial preprocessor's memory usage and about 0.8% of clang's total VMM allocation.
As for CPU overhead, it should only be noticeable when iterating over all macros, and should mostly consist of couple extra dereferences and one comparison per macro + skipping of #undef'd macros. It's less trivial to measure, though, as the preprocessor consumes a very small fraction of compilation time.
Reviewers: doug.gregor, klimek, rsmith, djasper
Reviewed By: doug.gregor
CC: cfe-commits, chandlerc
Differential Revision: http://llvm-reviews.chandlerc.com/D28
llvm-svn: 162810
2012-08-29 08:20:03 +08:00
|
|
|
SmallVector<id_macro_pair, 128> MacrosByID;
|
|
|
|
for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
|
|
|
|
I != E; ++I) {
|
2015-04-30 07:20:19 +08:00
|
|
|
auto *MD = I->second.getLatest();
|
|
|
|
if (MD && MD->isDefined())
|
|
|
|
MacrosByID.push_back(id_macro_pair(I->first, MD->getMacroInfo()));
|
Keep history of macro definitions and #undefs
Summary:
Summary: Keep history of macro definitions and #undefs with corresponding source locations, so that we can later find out all macros active in a specified source location. We don't save the history in PCH (no need currently). Memory overhead is about sizeof(void*)*3*<number of macro definitions and #undefs>+<in-memory size of all #undef'd macros>
I've run a test on a file composed of 109 .h files from boost 1.49 on x86-64 linux.
Stats before this patch:
*** Preprocessor Stats:
73222 directives found:
19171 #define.
4345 #undef.
#include/#include_next/#import:
5233 source files entered.
27 max include stack depth
19210 #if/#ifndef/#ifdef.
2384 #else/#elif.
6891 #endif.
408 #pragma.
14466 #if/#ifndef#ifdef regions skipped
80023/451669/1270 obj/fn/builtin macros expanded, 85724 on the fast path.
127145 token paste (##) operations performed, 11008 on the fast path.
Preprocessor Memory: 5874615B total
BumpPtr: 4399104
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
Stats with this patch:
...
Preprocessor Memory: 7541687B total
BumpPtr: 6066176
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
In my test increase in memory usage is about 1.7Mb, which is ~28% of initial preprocessor's memory usage and about 0.8% of clang's total VMM allocation.
As for CPU overhead, it should only be noticeable when iterating over all macros, and should mostly consist of couple extra dereferences and one comparison per macro + skipping of #undef'd macros. It's less trivial to measure, though, as the preprocessor consumes a very small fraction of compilation time.
Reviewers: doug.gregor, klimek, rsmith, djasper
Reviewed By: doug.gregor
CC: cfe-commits, chandlerc
Differential Revision: http://llvm-reviews.chandlerc.com/D28
llvm-svn: 162810
2012-08-29 08:20:03 +08:00
|
|
|
}
|
2014-03-08 05:51:58 +08:00
|
|
|
llvm::array_pod_sort(MacrosByID.begin(), MacrosByID.end(), MacroIDCompare);
|
2009-05-19 09:32:34 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = MacrosByID.size(); i != e; ++i) {
|
|
|
|
MacroInfo &MI = *MacrosByID[i].second;
|
2009-09-09 23:08:12 +08:00
|
|
|
// Ignore computed macros like __LINE__ and friends.
|
2009-05-19 09:32:34 +08:00
|
|
|
if (MI.isBuiltinMacro()) continue;
|
|
|
|
|
|
|
|
PrintMacroDefinition(*MacrosByID[i].first, MI, PP, *OS);
|
2010-01-20 01:42:20 +08:00
|
|
|
*OS << '\n';
|
2009-05-19 09:32:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
/// DoPrintPreprocessedInput - This implements -E mode.
|
2006-07-05 05:28:37 +08:00
|
|
|
///
|
2011-07-23 18:55:15 +08:00
|
|
|
void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS,
|
2009-11-11 18:07:44 +08:00
|
|
|
const PreprocessorOutputOptions &Opts) {
|
2009-11-11 18:07:22 +08:00
|
|
|
// Show macros with no output is handled specially.
|
|
|
|
if (!Opts.ShowCPP) {
|
|
|
|
assert(Opts.ShowMacros && "Not yet implemented!");
|
|
|
|
DoPrintMacros(PP, OS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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.
|
2009-11-11 18:07:22 +08:00
|
|
|
PP.SetCommentRetentionState(Opts.ShowComments, Opts.ShowMacroComments);
|
2009-05-19 09:02:07 +08:00
|
|
|
|
2015-02-26 08:17:25 +08:00
|
|
|
PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(
|
|
|
|
PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, Opts.UseLineDirectives);
|
2015-06-16 07:44:27 +08:00
|
|
|
|
|
|
|
// Expand macros in pragmas with -fms-extensions. The assumption is that
|
|
|
|
// the majority of pragmas in such a file will be Microsoft pragmas.
|
|
|
|
PP.AddPragmaHandler(new UnknownPragmaHandler(
|
|
|
|
"#pragma", Callbacks,
|
|
|
|
/*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
|
|
|
|
PP.AddPragmaHandler(
|
|
|
|
"GCC", new UnknownPragmaHandler(
|
|
|
|
"#pragma GCC", Callbacks,
|
|
|
|
/*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
|
|
|
|
PP.AddPragmaHandler(
|
|
|
|
"clang", new UnknownPragmaHandler(
|
|
|
|
"#pragma clang", Callbacks,
|
|
|
|
/*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt));
|
|
|
|
|
|
|
|
// The tokens after pragma omp need to be expanded.
|
|
|
|
//
|
|
|
|
// OpenMP [2.1, Directive format]
|
|
|
|
// Preprocessing tokens following the #pragma omp are subject to macro
|
|
|
|
// replacement.
|
|
|
|
PP.AddPragmaHandler("omp",
|
|
|
|
new UnknownPragmaHandler("#pragma omp", Callbacks,
|
|
|
|
/*RequireTokenExpansion=*/true));
|
2009-05-19 09:32:34 +08:00
|
|
|
|
2014-09-10 12:53:53 +08:00
|
|
|
PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
|
2009-05-19 09:02:07 +08:00
|
|
|
|
2009-05-19 09:32:34 +08:00
|
|
|
// After we have configured the preprocessor, enter the main file.
|
2010-04-21 04:35:58 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2009-05-19 09:32:34 +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();
|
|
|
|
Token Tok;
|
2010-11-12 15:15:47 +08: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 09:32:34 +08:00
|
|
|
|
|
|
|
// Read all the preprocessed tokens, printing them out to the stream.
|
|
|
|
PrintPreprocessedTokens(PP, Tok, Callbacks, *OS);
|
|
|
|
*OS << '\n';
|
2006-07-05 03:04:05 +08:00
|
|
|
}
|