2006-06-18 13:43:12 +08:00
|
|
|
//===--- clang.cpp - C-Language Front-end ---------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This utility may be invoked in the following manner:
|
|
|
|
// clang --help - Output information about command line switches
|
|
|
|
// clang [options] - Read from stdin.
|
|
|
|
// clang [options] file - Read from "file".
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: Options to support:
|
|
|
|
//
|
|
|
|
// -ffatal-errors
|
|
|
|
// -ftabstop=width
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
#include "clang.h"
|
2006-08-17 13:51:27 +08:00
|
|
|
#include "clang/AST/AST.h"
|
2006-07-31 09:59:18 +08:00
|
|
|
#include "clang/Parse/Parser.h"
|
2006-10-22 15:28:56 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceBuffer.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2006-10-15 03:54:37 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/System/MappedFile.h"
|
|
|
|
#include "llvm/System/Signals.h"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
2006-08-14 06:25:42 +08:00
|
|
|
static unsigned NumDiagnostics = 0;
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Global options.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
Verbose("v", cl::desc("Enable verbose output"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
Stats("stats", cl::desc("Print performance metrics and statistics"));
|
|
|
|
|
|
|
|
enum ProgActions {
|
2006-08-17 13:51:27 +08:00
|
|
|
ParseSyntaxOnly, // Parse and perform semantic analysis.
|
|
|
|
ParsePrintASTs, // Parse and print raw ASTs.
|
2006-07-31 09:59:18 +08:00
|
|
|
ParsePrintCallbacks, // Parse and print each callback.
|
|
|
|
ParseNoop, // Parse with noop callbacks.
|
2006-06-18 13:43:12 +08:00
|
|
|
RunPreprocessorOnly, // Just lex, no output.
|
|
|
|
PrintPreprocessedInput, // -E mode.
|
|
|
|
DumpTokens // Token dump mode.
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<ProgActions>
|
2006-07-31 09:59:18 +08:00
|
|
|
ProgAction(cl::desc("Choose output type:"), cl::ZeroOrMore,
|
2006-10-06 13:56:09 +08:00
|
|
|
cl::init(ParseNoop),
|
2006-06-18 13:43:12 +08:00
|
|
|
cl::values(
|
|
|
|
clEnumValN(RunPreprocessorOnly, "Eonly",
|
|
|
|
"Just run preprocessor, no output (for timings)"),
|
|
|
|
clEnumValN(PrintPreprocessedInput, "E",
|
|
|
|
"Run preprocessor, emit preprocessed file"),
|
|
|
|
clEnumValN(DumpTokens, "dumptokens",
|
|
|
|
"Run preprocessor, dump internal rep of tokens"),
|
2006-07-31 09:59:18 +08:00
|
|
|
clEnumValN(ParseNoop, "parse-noop",
|
|
|
|
"Run parser with noop callbacks (for timings)"),
|
2006-08-17 13:51:27 +08:00
|
|
|
clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
|
|
|
|
"Run parser and print each callback invoked"),
|
|
|
|
clEnumValN(ParsePrintASTs, "parse-print-ast",
|
|
|
|
"Run parser and print raw ASTs"),
|
|
|
|
clEnumValN(ParseSyntaxOnly, "fsyntax-only",
|
|
|
|
"Run parser and perform semantic analysis"),
|
2006-06-18 13:43:12 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2006-10-17 13:16:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Language Options
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
enum LangKind {
|
|
|
|
langkind_unspecified,
|
|
|
|
langkind_c,
|
|
|
|
langkind_c_cpp,
|
|
|
|
langkind_cxx,
|
|
|
|
langkind_cxx_cpp,
|
|
|
|
langkind_objc,
|
|
|
|
langkind_objc_cpp,
|
|
|
|
langkind_objcxx,
|
|
|
|
langkind_objcxx_cpp
|
|
|
|
};
|
|
|
|
|
|
|
|
/* TODO: GCC also accepts:
|
|
|
|
c-header c++-header objective-c-header objective-c++-header
|
|
|
|
assembler assembler-with-cpp
|
|
|
|
ada, f77*, ratfor (!), f95, java, treelang
|
|
|
|
*/
|
|
|
|
static cl::opt<LangKind>
|
|
|
|
BaseLang("x", cl::desc("Base language to compile"),
|
|
|
|
cl::init(langkind_unspecified),
|
|
|
|
cl::values(clEnumValN(langkind_c, "c", "C"),
|
|
|
|
clEnumValN(langkind_cxx, "c++", "C++"),
|
|
|
|
clEnumValN(langkind_objc, "objective-c", "Objective C"),
|
|
|
|
clEnumValN(langkind_objcxx,"objective-c++","Objective C++"),
|
|
|
|
clEnumValN(langkind_c_cpp, "c-cpp-output",
|
|
|
|
"Preprocessed C"),
|
|
|
|
clEnumValN(langkind_cxx_cpp, "c++-cpp-output",
|
|
|
|
"Preprocessed C++"),
|
|
|
|
clEnumValN(langkind_objc_cpp, "objective-c-cpp-output",
|
|
|
|
"Preprocessed Objective C"),
|
|
|
|
clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output",
|
|
|
|
"Preprocessed Objective C++"),
|
|
|
|
clEnumValEnd));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
LangObjC("ObjC", cl::desc("Set base language to Objective-C"),
|
|
|
|
cl::Hidden);
|
|
|
|
static cl::opt<bool>
|
|
|
|
LangObjCXX("ObjC++", cl::desc("Set base language to Objective-C++"),
|
|
|
|
cl::Hidden);
|
|
|
|
|
|
|
|
/// InitializeBaseLanguage - Handle the -x foo options or infer a base language
|
|
|
|
/// from the input filename.
|
|
|
|
static void InitializeBaseLanguage(LangOptions &Options,
|
|
|
|
const std::string &Filename) {
|
|
|
|
if (BaseLang == langkind_unspecified) {
|
|
|
|
std::string::size_type DotPos = Filename.rfind('.');
|
|
|
|
if (LangObjC) {
|
|
|
|
BaseLang = langkind_objc;
|
|
|
|
} else if (LangObjCXX) {
|
|
|
|
BaseLang = langkind_objcxx;
|
|
|
|
} else if (DotPos == std::string::npos) {
|
|
|
|
BaseLang = langkind_c; // Default to C if no extension.
|
|
|
|
} else {
|
|
|
|
std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end());
|
|
|
|
// C header: .h
|
|
|
|
// C++ header: .hh or .H;
|
|
|
|
// assembler no preprocessing: .s
|
|
|
|
// assembler: .S
|
|
|
|
if (Ext == "c")
|
|
|
|
BaseLang = langkind_c;
|
|
|
|
else if (Ext == "i")
|
|
|
|
BaseLang = langkind_c_cpp;
|
|
|
|
else if (Ext == "ii")
|
|
|
|
BaseLang = langkind_cxx_cpp;
|
|
|
|
else if (Ext == "m")
|
|
|
|
BaseLang = langkind_objc;
|
|
|
|
else if (Ext == "mi")
|
|
|
|
BaseLang = langkind_objc_cpp;
|
|
|
|
else if (Ext == "mm" || Ext == "M")
|
|
|
|
BaseLang = langkind_objcxx;
|
|
|
|
else if (Ext == "mii")
|
|
|
|
BaseLang = langkind_objcxx_cpp;
|
|
|
|
else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" ||
|
|
|
|
Ext == "c++" || Ext == "cp" || Ext == "cxx")
|
|
|
|
BaseLang = langkind_cxx;
|
|
|
|
else
|
|
|
|
BaseLang = langkind_c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: implement -fpreprocessed mode.
|
|
|
|
bool NoPreprocess = false;
|
|
|
|
|
|
|
|
switch (BaseLang) {
|
|
|
|
default: assert(0 && "Unknown language kind!");
|
|
|
|
case langkind_c_cpp:
|
|
|
|
NoPreprocess = true;
|
|
|
|
// FALLTHROUGH
|
|
|
|
case langkind_c:
|
|
|
|
break;
|
|
|
|
case langkind_cxx_cpp:
|
|
|
|
NoPreprocess = true;
|
|
|
|
// FALLTHROUGH
|
|
|
|
case langkind_cxx:
|
|
|
|
Options.CPlusPlus = 1;
|
|
|
|
break;
|
|
|
|
case langkind_objc_cpp:
|
|
|
|
NoPreprocess = true;
|
|
|
|
// FALLTHROUGH
|
|
|
|
case langkind_objc:
|
|
|
|
Options.ObjC1 = Options.ObjC2 = 1;
|
|
|
|
break;
|
|
|
|
case langkind_objcxx_cpp:
|
|
|
|
NoPreprocess = true;
|
|
|
|
// FALLTHROUGH
|
|
|
|
case langkind_objcxx:
|
|
|
|
Options.ObjC1 = Options.ObjC2 = 1;
|
|
|
|
Options.CPlusPlus = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// LangStds - Language standards we support.
|
|
|
|
enum LangStds {
|
|
|
|
lang_unspecified,
|
|
|
|
lang_c89, lang_c94, lang_c99,
|
|
|
|
lang_gnu89, lang_gnu99,
|
|
|
|
lang_cxx98, lang_gnucxx98
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<LangStds>
|
|
|
|
LangStd("std", cl::desc("Language standard to compile for"),
|
|
|
|
cl::init(lang_unspecified),
|
|
|
|
cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
|
|
|
|
clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"),
|
|
|
|
clEnumValN(lang_c94, "iso9899:199409",
|
|
|
|
"ISO C 1990 with amendment 1"),
|
|
|
|
clEnumValN(lang_c99, "c99", "ISO C 1999"),
|
|
|
|
// clEnumValN(lang_c99, "c9x", "ISO C 1999"),
|
|
|
|
clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"),
|
|
|
|
// clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"),
|
|
|
|
clEnumValN(lang_gnu89, "gnu89",
|
|
|
|
"ISO C 1990 with GNU extensions (default for C)"),
|
|
|
|
clEnumValN(lang_gnu99, "gnu99",
|
|
|
|
"ISO C 1999 with GNU extensions"),
|
|
|
|
clEnumValN(lang_gnu99, "gnu9x",
|
|
|
|
"ISO C 1999 with GNU extensions"),
|
|
|
|
clEnumValN(lang_cxx98, "c++98",
|
|
|
|
"ISO C++ 1998 with amendments"),
|
|
|
|
clEnumValN(lang_gnucxx98, "gnu++98",
|
|
|
|
"ISO C++ 1998 with amendments and GNU "
|
|
|
|
"extensions (default for C++)"),
|
|
|
|
clEnumValEnd));
|
|
|
|
|
|
|
|
// FIXME: add:
|
|
|
|
// -ansi
|
|
|
|
// -trigraphs
|
|
|
|
// -fdollars-in-identifiers
|
|
|
|
static void InitializeLanguageStandard(LangOptions &Options) {
|
|
|
|
if (LangStd == lang_unspecified) {
|
|
|
|
// Based on the base language, pick one.
|
|
|
|
switch (BaseLang) {
|
|
|
|
default: assert(0 && "Unknown base language");
|
|
|
|
case langkind_c:
|
|
|
|
case langkind_c_cpp:
|
|
|
|
case langkind_objc:
|
|
|
|
case langkind_objc_cpp:
|
2006-10-20 14:13:18 +08:00
|
|
|
LangStd = lang_gnu89;
|
2006-10-17 13:16:26 +08:00
|
|
|
break;
|
|
|
|
case langkind_cxx:
|
|
|
|
case langkind_cxx_cpp:
|
|
|
|
case langkind_objcxx:
|
|
|
|
case langkind_objcxx_cpp:
|
|
|
|
LangStd = lang_gnucxx98;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (LangStd) {
|
|
|
|
default: assert(0 && "Unknown language standard!");
|
|
|
|
|
|
|
|
// Fall through from newer standards to older ones. This isn't really right.
|
|
|
|
// FIXME: Enable specifically the right features based on the language stds.
|
|
|
|
case lang_gnucxx98:
|
|
|
|
Options.CPPMinMax = 1;
|
|
|
|
// FALL THROUGH.
|
|
|
|
case lang_cxx98:
|
|
|
|
Options.CPlusPlus = 1;
|
|
|
|
// FALL THROUGH.
|
|
|
|
case lang_gnu99:
|
|
|
|
case lang_c99:
|
|
|
|
Options.Digraphs = 1;
|
|
|
|
Options.C99 = 1;
|
|
|
|
Options.HexFloats = 1;
|
|
|
|
// FALL THROUGH.
|
|
|
|
case lang_gnu89:
|
|
|
|
Options.BCPLComment = 1; // Only for C99/C++.
|
|
|
|
// FALL THROUGH.
|
|
|
|
case lang_c94:
|
|
|
|
case lang_c89:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Options.Trigraphs = 1; // -trigraphs or -ansi
|
|
|
|
Options.DollarIdents = 1; // FIXME: Really a target property.
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Our DiagnosticClient implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// FIXME: Werror should take a list of things, -Werror=foo,bar
|
|
|
|
static cl::opt<bool>
|
|
|
|
WarningsAsErrors("Werror", cl::desc("Treat all warnings as errors"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
WarnOnExtensions("pedantic",
|
|
|
|
cl::desc("Issue a warning on uses of GCC extensions"));
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
ErrorOnExtensions("pedantic-errors",
|
|
|
|
cl::desc("Issue an error on uses of GCC extensions"));
|
|
|
|
|
2006-07-05 08:55:08 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
WarnUnusedMacros("Wunused_macros",
|
|
|
|
cl::desc("Warn for unused macros in the main translation unit"));
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// InitializeDiagnostics - Initialize the diagnostic object, based on the
|
|
|
|
/// current command line option settings.
|
|
|
|
static void InitializeDiagnostics(Diagnostic &Diags) {
|
|
|
|
Diags.setWarningsAsErrors(WarningsAsErrors);
|
|
|
|
Diags.setWarnOnExtensions(WarnOnExtensions);
|
|
|
|
Diags.setErrorOnExtensions(ErrorOnExtensions);
|
2006-07-05 08:55:08 +08:00
|
|
|
|
|
|
|
// Silence the "macro is not used" warning unless requested.
|
|
|
|
if (!WarnUnusedMacros)
|
|
|
|
Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
NoShowColumn("fno-show-column",
|
|
|
|
cl::desc("Do not include column number on diagnostics"));
|
|
|
|
static cl::opt<bool>
|
|
|
|
NoCaretDiagnostics("fno-caret-diagnostics",
|
|
|
|
cl::desc("Do not include source line and caret with"
|
|
|
|
" diagnostics"));
|
|
|
|
|
|
|
|
/// DiagnosticPrinterSTDERR - This is a concrete diagnostic client, which prints
|
|
|
|
/// the diagnostics to standard error.
|
|
|
|
class DiagnosticPrinterSTDERR : public DiagnosticClient {
|
|
|
|
SourceManager &SourceMgr;
|
|
|
|
SourceLocation LastWarningLoc;
|
|
|
|
public:
|
|
|
|
DiagnosticPrinterSTDERR(SourceManager &sourceMgr)
|
|
|
|
: SourceMgr(sourceMgr) {}
|
|
|
|
|
|
|
|
void PrintIncludeStack(SourceLocation Pos);
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
|
2006-06-18 13:43:12 +08:00
|
|
|
SourceLocation Pos,
|
|
|
|
diag::kind ID, const std::string &Msg);
|
|
|
|
};
|
|
|
|
|
|
|
|
void DiagnosticPrinterSTDERR::
|
|
|
|
PrintIncludeStack(SourceLocation Pos) {
|
|
|
|
unsigned FileID = Pos.getFileID();
|
|
|
|
if (FileID == 0) return;
|
|
|
|
|
|
|
|
// Print out the other include frames first.
|
|
|
|
PrintIncludeStack(SourceMgr.getIncludeLoc(FileID));
|
|
|
|
|
|
|
|
unsigned LineNo = SourceMgr.getLineNumber(Pos);
|
|
|
|
|
|
|
|
const SourceBuffer *Buffer = SourceMgr.getBuffer(FileID);
|
|
|
|
std::cerr << "In file included from " << Buffer->getBufferIdentifier()
|
|
|
|
<< ":" << LineNo << ":\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 14:48:37 +08:00
|
|
|
void DiagnosticPrinterSTDERR::HandleDiagnostic(Diagnostic::Level Level,
|
2006-06-18 13:43:12 +08:00
|
|
|
SourceLocation Pos,
|
|
|
|
diag::kind ID,
|
|
|
|
const std::string &Extra) {
|
2006-08-14 06:25:42 +08:00
|
|
|
++NumDiagnostics;
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned LineNo = 0, FilePos = 0, FileID = 0, ColNo = 0;
|
|
|
|
unsigned LineStart = 0, LineEnd = 0;
|
|
|
|
const SourceBuffer *Buffer = 0;
|
|
|
|
|
|
|
|
if (Pos.isValid()) {
|
|
|
|
LineNo = SourceMgr.getLineNumber(Pos);
|
2006-07-03 14:28:56 +08:00
|
|
|
FileID = SourceMgr.getLogicalLoc(Pos).getFileID();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// First, if this diagnostic is not in the main file, print out the
|
|
|
|
// "included from" lines.
|
2006-07-03 14:28:56 +08:00
|
|
|
if (LastWarningLoc != SourceMgr.getIncludeLoc(FileID)) {
|
|
|
|
LastWarningLoc = SourceMgr.getIncludeLoc(FileID);
|
2006-06-18 13:43:12 +08:00
|
|
|
PrintIncludeStack(LastWarningLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the column number. Rewind from the current position to the start
|
|
|
|
// of the line.
|
|
|
|
ColNo = SourceMgr.getColumnNumber(Pos);
|
2006-06-26 09:48:23 +08:00
|
|
|
FilePos = SourceMgr.getSourceFilePos(Pos);
|
2006-06-25 05:29:50 +08:00
|
|
|
LineStart = FilePos-ColNo+1; // Column # is 1-based
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Compute the line end. Scan forward from the error position to the end of
|
|
|
|
// the line.
|
|
|
|
Buffer = SourceMgr.getBuffer(FileID);
|
|
|
|
const char *Buf = Buffer->getBufferStart();
|
|
|
|
const char *BufEnd = Buffer->getBufferEnd();
|
|
|
|
LineEnd = FilePos;
|
|
|
|
while (Buf+LineEnd != BufEnd &&
|
|
|
|
Buf[LineEnd] != '\n' && Buf[LineEnd] != '\r')
|
|
|
|
++LineEnd;
|
|
|
|
|
|
|
|
std::cerr << Buffer->getBufferIdentifier()
|
|
|
|
<< ":" << LineNo << ":";
|
|
|
|
if (ColNo && !NoShowColumn)
|
|
|
|
std::cerr << ColNo << ":";
|
|
|
|
std::cerr << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Level) {
|
|
|
|
default: assert(0 && "Unknown diagnostic type!");
|
|
|
|
case Diagnostic::Note: std::cerr << "note: "; break;
|
|
|
|
case Diagnostic::Warning: std::cerr << "warning: "; break;
|
|
|
|
case Diagnostic::Error: std::cerr << "error: "; break;
|
|
|
|
case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
|
|
|
|
case Diagnostic::Sorry: std::cerr << "sorry, unimplemented: "; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Msg = Diagnostic::getDescription(ID);
|
|
|
|
|
|
|
|
// Replace all instances of %s in Msg with 'Extra'.
|
|
|
|
if (Msg.size() > 1) {
|
|
|
|
for (unsigned i = 0; i < Msg.size()-1; ++i) {
|
|
|
|
if (Msg[i] == '%' && Msg[i+1] == 's') {
|
|
|
|
Msg = std::string(Msg.begin(), Msg.begin()+i) +
|
|
|
|
Extra +
|
|
|
|
std::string(Msg.begin()+i+2, Msg.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cerr << Msg << "\n";
|
|
|
|
|
|
|
|
if (!NoCaretDiagnostics && Pos.isValid()) {
|
|
|
|
// Print out a line of the source file.
|
|
|
|
const char *Buf = Buffer->getBufferStart();
|
|
|
|
std::cerr << std::string(Buf+LineStart, Buf+LineEnd) << "\n";
|
|
|
|
|
|
|
|
// If the source line contained any tab characters between the start of the
|
|
|
|
// line and the diagnostic, replace the space we inserted with a tab, so
|
|
|
|
// that the carat will be indented exactly like the source line.
|
|
|
|
std::string Indent(ColNo-1, ' ');
|
|
|
|
for (unsigned i = LineStart; i != FilePos; ++i)
|
|
|
|
if (Buf[i] == '\t')
|
|
|
|
Indent[i-LineStart] = '\t';
|
|
|
|
|
|
|
|
// Print out the caret itself.
|
|
|
|
std::cerr << Indent << "^\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Initialization
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// FIXME: Preprocessor builtins to support.
|
|
|
|
// -A... - Play with #assertions
|
|
|
|
// -undef - Undefine all predefined macros
|
|
|
|
|
|
|
|
static cl::list<std::string>
|
|
|
|
D_macros("D", cl::value_desc("macro"), cl::Prefix,
|
|
|
|
cl::desc("Predefine the specified macro"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
U_macros("U", cl::value_desc("macro"), cl::Prefix,
|
|
|
|
cl::desc("Undefine the specified macro"));
|
|
|
|
|
|
|
|
// Append a #define line to Buf for Macro. Macro should be of the form XXX,
|
|
|
|
// in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit
|
|
|
|
// "#define XXX Y z W". To get a #define with no value, use "XXX=".
|
|
|
|
static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro,
|
|
|
|
const char *Command = "#define ") {
|
|
|
|
Buf.insert(Buf.end(), Command, Command+strlen(Command));
|
|
|
|
if (const char *Equal = strchr(Macro, '=')) {
|
|
|
|
// Turn the = into ' '.
|
|
|
|
Buf.insert(Buf.end(), Macro, Equal);
|
|
|
|
Buf.push_back(' ');
|
|
|
|
Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal));
|
|
|
|
} else {
|
|
|
|
// Push "macroname 1".
|
|
|
|
Buf.insert(Buf.end(), Macro, Macro+strlen(Macro));
|
|
|
|
Buf.push_back(' ');
|
|
|
|
Buf.push_back('1');
|
|
|
|
}
|
|
|
|
Buf.push_back('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitializePredefinedMacros(Preprocessor &PP,
|
|
|
|
std::vector<char> &Buf) {
|
|
|
|
// FIXME: Implement magic like cpp_init_builtins for things like __STDC__
|
|
|
|
// and __DATE__ etc.
|
|
|
|
#if 0
|
|
|
|
/* __STDC__ has the value 1 under normal circumstances.
|
|
|
|
However, if (a) we are in a system header, (b) the option
|
|
|
|
stdc_0_in_system_headers is true (set by target config), and
|
|
|
|
(c) we are not in strictly conforming mode, then it has the
|
|
|
|
value 0. (b) and (c) are already checked in cpp_init_builtins. */
|
2006-07-04 12:50:29 +08:00
|
|
|
//case BT_STDC:
|
2006-06-18 13:43:12 +08:00
|
|
|
if (cpp_in_system_header (pfile))
|
|
|
|
number = 0;
|
|
|
|
else
|
|
|
|
number = 1;
|
|
|
|
break;
|
|
|
|
#endif
|
2006-10-15 09:05:46 +08:00
|
|
|
// These should all be defined in the preprocessor according to the
|
2006-07-04 12:50:29 +08:00
|
|
|
// current language configuration.
|
2006-06-18 13:43:12 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__STDC__=1");
|
2006-07-04 12:50:29 +08:00
|
|
|
//DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
|
2006-10-15 09:05:46 +08:00
|
|
|
if (PP.getLangOptions().C99)
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
|
|
|
|
else
|
|
|
|
DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-15 09:05:46 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
|
|
|
|
if (PP.getLangOptions().ObjC1)
|
|
|
|
DefineBuiltinMacro(Buf, "__OBJC__=1");
|
|
|
|
if (PP.getLangOptions().ObjC2)
|
|
|
|
DefineBuiltinMacro(Buf, "__OBJC2__=1");
|
|
|
|
|
2006-10-15 03:54:37 +08:00
|
|
|
// Get the target #defines.
|
|
|
|
PP.getTargetInfo().getTargetDefines(Buf);
|
|
|
|
|
2006-10-15 09:05:46 +08:00
|
|
|
// Compiler set macros.
|
2006-10-15 03:54:37 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
|
2006-06-18 13:43:12 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1030");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUC__=4");
|
|
|
|
DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002");
|
2006-10-15 09:05:46 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
|
|
|
|
"build 5250)\"");
|
|
|
|
|
|
|
|
// Build configuration options.
|
|
|
|
DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
|
2006-06-18 13:43:12 +08:00
|
|
|
DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__PIC__=1");
|
2006-10-15 09:05:46 +08:00
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (PP.getLangOptions().CPlusPlus) {
|
|
|
|
DefineBuiltinMacro(Buf, "__DEPRECATED=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__EXCEPTIONS=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__GNUG__=4");
|
|
|
|
DefineBuiltinMacro(Buf, "__GXX_WEAK__=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__cplusplus=1");
|
|
|
|
DefineBuiltinMacro(Buf, "__private_extern__=extern");
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Should emit a #line directive here.
|
|
|
|
|
|
|
|
// Add macros from the command line.
|
|
|
|
// FIXME: Should traverse the #define/#undef lists in parallel.
|
|
|
|
for (unsigned i = 0, e = D_macros.size(); i != e; ++i)
|
|
|
|
DefineBuiltinMacro(Buf, D_macros[i].c_str());
|
|
|
|
for (unsigned i = 0, e = U_macros.size(); i != e; ++i)
|
|
|
|
DefineBuiltinMacro(Buf, U_macros[i].c_str(), "#undef ");
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor include path information.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// This tool exports a large number of command line options to control how the
|
|
|
|
// preprocessor searches for header files. At root, however, the Preprocessor
|
|
|
|
// object takes a very simple interface: a list of directories to search for
|
|
|
|
//
|
|
|
|
// FIXME: -nostdinc,-nostdinc++
|
|
|
|
// FIXME: -isysroot,-imultilib
|
|
|
|
//
|
|
|
|
// FIXME: -include,-imacros
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
|
|
|
nostdinc("nostdinc", cl::desc("Disable standard #include directories"));
|
|
|
|
|
|
|
|
// Various command line options. These four add directories to each chain.
|
|
|
|
static cl::list<std::string>
|
2006-10-22 15:34:56 +08:00
|
|
|
F_dirs("F", cl::value_desc("directory"), cl::Prefix,
|
|
|
|
cl::desc("Add directory to framework include search path"));
|
|
|
|
static cl::list<std::string>
|
2006-06-18 13:43:12 +08:00
|
|
|
I_dirs("I", cl::value_desc("directory"), cl::Prefix,
|
|
|
|
cl::desc("Add directory to include search path"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
idirafter_dirs("idirafter", cl::value_desc("directory"), cl::Prefix,
|
|
|
|
cl::desc("Add directory to AFTER include search path"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
iquote_dirs("iquote", cl::value_desc("directory"), cl::Prefix,
|
|
|
|
cl::desc("Add directory to QUOTE include search path"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
isystem_dirs("isystem", cl::value_desc("directory"), cl::Prefix,
|
|
|
|
cl::desc("Add directory to SYSTEM include search path"));
|
|
|
|
|
|
|
|
// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
|
|
|
|
static cl::list<std::string>
|
|
|
|
iprefix_vals("iprefix", cl::value_desc("prefix"), cl::Prefix,
|
|
|
|
cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
iwithprefix_vals("iwithprefix", cl::value_desc("dir"), cl::Prefix,
|
|
|
|
cl::desc("Set directory to SYSTEM include search path with prefix"));
|
|
|
|
static cl::list<std::string>
|
|
|
|
iwithprefixbefore_vals("iwithprefixbefore", cl::value_desc("dir"), cl::Prefix,
|
|
|
|
cl::desc("Set directory to include search path with prefix"));
|
|
|
|
|
|
|
|
// Finally, implement the code that groks the options above.
|
|
|
|
enum IncludeDirGroup {
|
|
|
|
Quoted = 0,
|
|
|
|
Angled,
|
|
|
|
System,
|
|
|
|
After
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<DirectoryLookup> IncludeGroup[4];
|
|
|
|
|
|
|
|
/// AddPath - Add the specified path to the specified group list.
|
|
|
|
///
|
|
|
|
static void AddPath(const std::string &Path, IncludeDirGroup Group,
|
|
|
|
bool isCXXAware, bool isUserSupplied,
|
2006-10-17 14:20:32 +08:00
|
|
|
bool isFramework, FileManager &FM) {
|
2006-06-18 13:43:12 +08:00
|
|
|
const DirectoryEntry *DE = FM.getDirectory(Path);
|
|
|
|
if (DE == 0) {
|
|
|
|
if (Verbose)
|
|
|
|
std::cerr << "ignoring nonexistent directory \"" << Path << "\"\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DirectoryLookup::DirType Type;
|
|
|
|
if (Group == Quoted || Group == Angled)
|
|
|
|
Type = DirectoryLookup::NormalHeaderDir;
|
|
|
|
else if (isCXXAware)
|
|
|
|
Type = DirectoryLookup::SystemHeaderDir;
|
|
|
|
else
|
|
|
|
Type = DirectoryLookup::ExternCSystemHeaderDir;
|
|
|
|
|
2006-10-17 14:20:32 +08:00
|
|
|
IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
|
|
|
|
isFramework));
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// RemoveDuplicates - If there are duplicate directory entries in the specified
|
|
|
|
/// search list, remove the later (dead) ones.
|
|
|
|
static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList) {
|
|
|
|
std::set<const DirectoryEntry *> SeenDirs;
|
|
|
|
for (unsigned i = 0; i != SearchList.size(); ++i) {
|
|
|
|
// If this isn't the first time we've seen this dir, remove it.
|
|
|
|
if (!SeenDirs.insert(SearchList[i].getDir()).second) {
|
|
|
|
if (Verbose)
|
|
|
|
std::cerr << "ignoring duplicate directory \""
|
|
|
|
<< SearchList[i].getDir()->getName() << "\"\n";
|
|
|
|
SearchList.erase(SearchList.begin()+i);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
/// InitializeIncludePaths - Process the -I options and set them in the
|
|
|
|
/// HeaderSearch object.
|
|
|
|
static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM,
|
|
|
|
Diagnostic &Diags) {
|
2006-10-22 15:34:56 +08:00
|
|
|
// Handle -F... options.
|
|
|
|
for (unsigned i = 0, e = F_dirs.size(); i != e; ++i)
|
|
|
|
AddPath(F_dirs[i], Angled, false, true, true, FM);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Handle -I... options.
|
|
|
|
for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
|
|
|
|
if (I_dirs[i] == "-") {
|
|
|
|
// -I- is a deprecated GCC feature.
|
2006-10-18 13:34:33 +08:00
|
|
|
Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported);
|
2006-06-18 13:43:12 +08:00
|
|
|
} else {
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath(I_dirs[i], Angled, false, true, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle -idirafter... options.
|
|
|
|
for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath(idirafter_dirs[i], After, false, true, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Handle -iquote... options.
|
|
|
|
for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath(iquote_dirs[i], Quoted, false, true, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Handle -isystem... options.
|
|
|
|
for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath(isystem_dirs[i], System, false, true, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in
|
|
|
|
// parallel, processing the values in order of occurance to get the right
|
|
|
|
// prefixes.
|
|
|
|
{
|
|
|
|
std::string Prefix = ""; // FIXME: this isn't the correct default prefix.
|
|
|
|
unsigned iprefix_idx = 0;
|
|
|
|
unsigned iwithprefix_idx = 0;
|
|
|
|
unsigned iwithprefixbefore_idx = 0;
|
|
|
|
bool iprefix_done = iprefix_vals.empty();
|
|
|
|
bool iwithprefix_done = iwithprefix_vals.empty();
|
|
|
|
bool iwithprefixbefore_done = iwithprefixbefore_vals.empty();
|
|
|
|
while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) {
|
|
|
|
if (!iprefix_done &&
|
|
|
|
(iwithprefix_done ||
|
|
|
|
iprefix_vals.getPosition(iprefix_idx) <
|
|
|
|
iwithprefix_vals.getPosition(iwithprefix_idx)) &&
|
|
|
|
(iwithprefixbefore_done ||
|
|
|
|
iprefix_vals.getPosition(iprefix_idx) <
|
|
|
|
iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
|
|
|
|
Prefix = iprefix_vals[iprefix_idx];
|
|
|
|
++iprefix_idx;
|
|
|
|
iprefix_done = iprefix_idx == iprefix_vals.size();
|
|
|
|
} else if (!iwithprefix_done &&
|
|
|
|
(iwithprefixbefore_done ||
|
|
|
|
iwithprefix_vals.getPosition(iwithprefix_idx) <
|
|
|
|
iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) {
|
|
|
|
AddPath(Prefix+iwithprefix_vals[iwithprefix_idx],
|
2006-10-17 14:20:32 +08:00
|
|
|
System, false, false, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
++iwithprefix_idx;
|
|
|
|
iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
|
|
|
|
} else {
|
|
|
|
AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
|
2006-10-17 14:20:32 +08:00
|
|
|
Angled, false, false, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
++iwithprefixbefore_idx;
|
|
|
|
iwithprefixbefore_done =
|
|
|
|
iwithprefixbefore_idx == iwithprefixbefore_vals.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
|
|
|
|
// OBJC_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH environment variables.
|
|
|
|
|
|
|
|
// FIXME: temporary hack: hard-coded paths.
|
2006-10-17 14:20:32 +08:00
|
|
|
// FIXME: get these from the target?
|
2006-06-18 13:43:12 +08:00
|
|
|
if (!nostdinc) {
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath("/usr/local/include", System, false, false, false, FM);
|
2006-10-20 12:55:39 +08:00
|
|
|
AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
|
|
|
|
false, false, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
|
2006-10-17 14:20:32 +08:00
|
|
|
System, false, false, false, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
|
|
|
|
"4.0.1/../../../../powerpc-apple-darwin8/include",
|
2006-10-17 14:20:32 +08:00
|
|
|
System, false, false, false, FM);
|
|
|
|
AddPath("/usr/include", System, false, false, false, FM);
|
|
|
|
AddPath("/System/Library/Frameworks", System, true, false, true, FM);
|
|
|
|
AddPath("/Library/Frameworks", System, true, false, true, FM);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now that we have collected all of the include paths, merge them all
|
|
|
|
// together and tell the preprocessor about them.
|
|
|
|
|
|
|
|
// Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList.
|
|
|
|
std::vector<DirectoryLookup> SearchList;
|
|
|
|
SearchList = IncludeGroup[Angled];
|
|
|
|
SearchList.insert(SearchList.end(), IncludeGroup[System].begin(),
|
|
|
|
IncludeGroup[System].end());
|
|
|
|
SearchList.insert(SearchList.end(), IncludeGroup[After].begin(),
|
|
|
|
IncludeGroup[After].end());
|
|
|
|
RemoveDuplicates(SearchList);
|
|
|
|
RemoveDuplicates(IncludeGroup[Quoted]);
|
|
|
|
|
|
|
|
// Prepend QUOTED list on the search list.
|
|
|
|
SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(),
|
|
|
|
IncludeGroup[Quoted].end());
|
|
|
|
|
|
|
|
|
|
|
|
bool DontSearchCurDir = false; // TODO: set to true if -I- is set?
|
2006-10-18 13:34:33 +08:00
|
|
|
Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
|
|
|
|
DontSearchCurDir);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If verbose, print the list of directories that will be searched.
|
|
|
|
if (Verbose) {
|
|
|
|
std::cerr << "#include \"...\" search starts here:\n";
|
|
|
|
unsigned QuotedIdx = IncludeGroup[Quoted].size();
|
|
|
|
for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
|
|
|
|
if (i == QuotedIdx)
|
|
|
|
std::cerr << "#include <...> search starts here:\n";
|
|
|
|
std::cerr << " " << SearchList[i].getDir()->getName() << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Read any files specified by -imacros or -include.
|
|
|
|
static void ReadPrologFiles(Preprocessor &PP, std::vector<char> &Buf) {
|
|
|
|
// FIXME: IMPLEMENT
|
|
|
|
}
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2006-08-17 14:28:25 +08:00
|
|
|
// Basic Parser driver
|
2006-07-31 09:59:18 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-08-14 08:22:04 +08:00
|
|
|
static void ParseFile(Preprocessor &PP, Action *PA, unsigned MainFileID) {
|
2006-07-31 09:59:18 +08:00
|
|
|
Parser P(PP, *PA);
|
|
|
|
PP.EnterSourceFile(MainFileID, 0, true);
|
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
// Parsing the specified input file.
|
2006-07-31 09:59:18 +08:00
|
|
|
P.ParseTranslationUnit();
|
2006-08-17 14:28:25 +08:00
|
|
|
delete PA;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ASTStreamer drivers
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-07-31 09:59:18 +08:00
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
static void PrintASTs(Preprocessor &PP, unsigned MainFileID) {
|
2006-08-23 13:17:46 +08:00
|
|
|
ASTStreamerTy *Streamer = ASTStreamer_Init(PP, MainFileID, true);
|
2006-07-31 09:59:18 +08:00
|
|
|
|
2006-08-17 14:28:25 +08:00
|
|
|
while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
|
2006-10-16 08:33:54 +08:00
|
|
|
std::cerr << "Read top-level decl: ";
|
|
|
|
if (const IdentifierInfo *II = D->getIdentifier())
|
|
|
|
std::cerr << II->getName() << "\n";
|
|
|
|
else
|
|
|
|
std::cerr << "\n";
|
2006-08-17 14:28:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTStreamer_Terminate(Streamer);
|
2006-07-31 09:59:18 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main driver
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
|
|
|
|
sys::PrintStackTraceOnErrorSignal();
|
|
|
|
|
|
|
|
/// Create a SourceManager object. This tracks and owns all the file buffers
|
|
|
|
/// allocated to the program.
|
|
|
|
SourceManager SourceMgr;
|
|
|
|
|
|
|
|
// Print diagnostics to stderr.
|
|
|
|
DiagnosticPrinterSTDERR OurDiagnosticClient(SourceMgr);
|
|
|
|
|
|
|
|
// Configure our handling of diagnostics.
|
|
|
|
Diagnostic OurDiagnostics(OurDiagnosticClient);
|
|
|
|
InitializeDiagnostics(OurDiagnostics);
|
|
|
|
|
|
|
|
// Turn all options on.
|
|
|
|
LangOptions Options;
|
2006-10-17 13:16:26 +08:00
|
|
|
InitializeBaseLanguage(Options, InputFilename);
|
|
|
|
InitializeLanguageStandard(Options);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2006-10-14 15:39:34 +08:00
|
|
|
// Get information about the targets being compiled for. Note that this
|
|
|
|
// pointer and the TargetInfoImpl objects are never deleted by this toy
|
|
|
|
// driver.
|
|
|
|
TargetInfo *Target = CreateTargetInfo(OurDiagnostics);
|
|
|
|
if (Target == 0) {
|
|
|
|
std::cerr << "Sorry, don't know what target this is, please use -arch.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
|
|
|
FileManager FileMgr;
|
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Process the -I options and set them in the HeaderInfo.
|
|
|
|
HeaderSearch HeaderInfo(FileMgr);
|
|
|
|
InitializeIncludePaths(HeaderInfo, FileMgr, OurDiagnostics);
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Set up the preprocessor with these options.
|
2006-10-18 13:34:33 +08:00
|
|
|
Preprocessor PP(OurDiagnostics, Options, *Target, FileMgr, SourceMgr,
|
|
|
|
HeaderInfo);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Install things like __POWERPC__, __GNUC__, etc into the macro table.
|
|
|
|
std::vector<char> PrologMacros;
|
|
|
|
InitializePredefinedMacros(PP, PrologMacros);
|
|
|
|
|
|
|
|
// Read any files specified by -imacros or -include.
|
|
|
|
ReadPrologFiles(PP, PrologMacros);
|
|
|
|
|
2006-07-29 14:35:08 +08:00
|
|
|
// Figure out where to get and map in the main file.
|
|
|
|
unsigned MainFileID = 0;
|
|
|
|
if (InputFilename != "-") {
|
|
|
|
const FileEntry *File = FileMgr.getFile(InputFilename);
|
|
|
|
if (File) MainFileID = SourceMgr.createFileID(File, SourceLocation());
|
|
|
|
if (MainFileID == 0) {
|
|
|
|
std::cerr << "Error reading '" << InputFilename << "'!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SourceBuffer *SB = SourceBuffer::getSTDIN();
|
|
|
|
if (SB) MainFileID = SourceMgr.createFileIDForMemBuffer(SB);
|
|
|
|
if (MainFileID == 0) {
|
|
|
|
std::cerr << "Error reading standard input! Empty?\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Now that we have emitted the predefined macros, #includes, etc into
|
|
|
|
// PrologMacros, preprocess it to populate the initial preprocessor state.
|
|
|
|
{
|
|
|
|
// Memory buffer must end with a null byte!
|
|
|
|
PrologMacros.push_back(0);
|
|
|
|
|
|
|
|
SourceBuffer *SB = SourceBuffer::getMemBuffer(&PrologMacros.front(),
|
|
|
|
&PrologMacros.back(),
|
|
|
|
"<predefines>");
|
|
|
|
assert(SB && "Cannot fail to create predefined source buffer");
|
|
|
|
unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB);
|
|
|
|
assert(FileID && "Could not create FileID for predefines?");
|
|
|
|
|
|
|
|
// Start parsing the predefines.
|
|
|
|
PP.EnterSourceFile(FileID, 0);
|
|
|
|
|
|
|
|
// Lex the file, which will read all the macros.
|
|
|
|
LexerToken Tok;
|
2006-06-18 14:48:37 +08:00
|
|
|
PP.Lex(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(Tok.getKind() == tok::eof && "Didn't read entire file!");
|
|
|
|
|
|
|
|
// Once we've read this, we're done.
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ProgAction) {
|
2006-07-31 09:59:18 +08:00
|
|
|
case DumpTokens: { // Token dump mode.
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken Tok;
|
2006-07-29 14:35:08 +08:00
|
|
|
// Start parsing the specified input file.
|
|
|
|
PP.EnterSourceFile(MainFileID, 0, true);
|
2006-06-18 13:43:12 +08:00
|
|
|
do {
|
2006-06-18 14:48:37 +08:00
|
|
|
PP.Lex(Tok);
|
2006-07-31 09:59:18 +08:00
|
|
|
PP.DumpToken(Tok, true);
|
|
|
|
std::cerr << "\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
} while (Tok.getKind() != tok::eof);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
|
2006-06-18 13:43:12 +08:00
|
|
|
LexerToken Tok;
|
2006-07-29 14:35:08 +08:00
|
|
|
// Start parsing the specified input file.
|
|
|
|
PP.EnterSourceFile(MainFileID, 0, true);
|
2006-06-18 13:43:12 +08:00
|
|
|
do {
|
2006-06-18 14:48:37 +08:00
|
|
|
PP.Lex(Tok);
|
2006-06-18 13:43:12 +08:00
|
|
|
} while (Tok.getKind() != tok::eof);
|
|
|
|
break;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
|
|
|
|
case PrintPreprocessedInput: // -E mode.
|
|
|
|
DoPrintPreprocessedInput(MainFileID, PP, Options);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParseNoop: // -parse-noop
|
2006-08-17 13:51:27 +08:00
|
|
|
ParseFile(PP, new EmptyAction(), MainFileID);
|
|
|
|
break;
|
2006-07-31 09:59:18 +08:00
|
|
|
case ParsePrintCallbacks:
|
2006-08-17 13:51:27 +08:00
|
|
|
ParseFile(PP, CreatePrintParserActionsAction(), MainFileID);
|
|
|
|
break;
|
|
|
|
case ParsePrintASTs:
|
2006-08-17 14:28:25 +08:00
|
|
|
PrintASTs(PP, MainFileID);
|
2006-07-31 09:59:18 +08:00
|
|
|
break;
|
2006-08-07 02:29:56 +08:00
|
|
|
case ParseSyntaxOnly: // -fsyntax-only
|
2006-08-14 08:38:06 +08:00
|
|
|
ParseFile(PP, new EmptyAction(), MainFileID);
|
2006-08-07 02:29:56 +08:00
|
|
|
break;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-08-14 06:25:42 +08:00
|
|
|
if (NumDiagnostics)
|
|
|
|
std::cerr << NumDiagnostics << " diagnostics generated.\n";
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (Stats) {
|
|
|
|
// Printed from low-to-high level.
|
|
|
|
PP.getFileManager().PrintStats();
|
2006-10-18 13:34:33 +08:00
|
|
|
PP.getHeaderSearchInfo().PrintStats();
|
2006-06-18 13:43:12 +08:00
|
|
|
PP.getSourceManager().PrintStats();
|
|
|
|
PP.getIdentifierTable().PrintStats();
|
|
|
|
PP.PrintStats();
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
}
|