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:
|
2006-11-21 13:52:55 +08:00
|
|
|
// clang --help - Output help info.
|
|
|
|
// clang [options] - Read from stdin.
|
|
|
|
// clang [options] file - Read from "file".
|
|
|
|
// clang [options] file1 file2 - Read these files.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// TODO: Options to support:
|
|
|
|
//
|
|
|
|
// -ffatal-errors
|
|
|
|
// -ftabstop=width
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-07-05 03:04:05 +08:00
|
|
|
#include "clang.h"
|
2007-10-07 14:04:32 +08:00
|
|
|
#include "ASTConsumers.h"
|
2007-06-28 12:54:17 +08:00
|
|
|
#include "TextDiagnosticBuffer.h"
|
2007-06-07 17:34:54 +08:00
|
|
|
#include "TextDiagnosticPrinter.h"
|
2007-09-16 06:56:56 +08:00
|
|
|
#include "clang/Sema/ASTStreamer.h"
|
|
|
|
#include "clang/AST/ASTConsumer.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/FileManager.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"
|
2007-04-29 15:12:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "llvm/System/Signals.h"
|
2007-12-04 06:06:55 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2007-06-28 12:54:17 +08:00
|
|
|
#include <memory>
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Global options.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
Verbose("v", llvm::cl::desc("Enable verbose output"));
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
Stats("stats", llvm::cl::desc("Print performance metrics and statistics"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
enum ProgActions {
|
2007-10-11 08:43:27 +08:00
|
|
|
RewriteTest, // Rewriter testing stuff.
|
2007-05-24 14:29:05 +08:00
|
|
|
EmitLLVM, // Emit a .ll file.
|
2007-10-11 08:18:28 +08:00
|
|
|
ASTPrint, // Parse ASTs and print them.
|
|
|
|
ASTDump, // Parse ASTs and dump them.
|
|
|
|
ASTView, // Parse ASTs and view them in Graphviz.
|
2007-08-22 05:42:03 +08:00
|
|
|
ParseCFGDump, // Parse ASTS. Build CFGs. Print CFGs.
|
2007-09-07 07:00:42 +08:00
|
|
|
ParseCFGView, // Parse ASTS. Build CFGs. View CFGs.
|
2007-09-06 08:17:54 +08:00
|
|
|
AnalysisLiveVariables, // Print results of live-variable analysis.
|
2007-09-07 07:00:42 +08:00
|
|
|
WarnDeadStores, // Run DeadStores checker on parsed ASTs.
|
2007-09-26 02:37:20 +08:00
|
|
|
WarnDeadStoresCheck, // Check diagnostics for "DeadStores".
|
2007-09-18 04:49:30 +08:00
|
|
|
WarnUninitVals, // Run UnitializedVariables checker.
|
2007-10-17 07:37:27 +08:00
|
|
|
TestSerialization, // Run experimental serialization code.
|
2006-07-31 09:59:18 +08:00
|
|
|
ParsePrintCallbacks, // Parse and print each callback.
|
2006-11-06 02:00:10 +08:00
|
|
|
ParseSyntaxOnly, // Parse and perform semantic analysis.
|
2006-07-31 09:59:18 +08:00
|
|
|
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.
|
|
|
|
};
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<ProgActions>
|
|
|
|
ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
|
|
|
|
llvm::cl::init(ParseSyntaxOnly),
|
|
|
|
llvm::cl::values(
|
2006-06-18 13:43:12 +08:00
|
|
|
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(ParseSyntaxOnly, "fsyntax-only",
|
|
|
|
"Run parser and perform semantic analysis"),
|
2006-11-06 02:00:10 +08:00
|
|
|
clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
|
|
|
|
"Run parser and print each callback invoked"),
|
2007-10-11 08:18:28 +08:00
|
|
|
clEnumValN(ASTPrint, "ast-print",
|
|
|
|
"Build ASTs and then pretty-print them"),
|
|
|
|
clEnumValN(ASTDump, "ast-dump",
|
|
|
|
"Build ASTs and then debug dump them"),
|
2007-10-11 08:37:43 +08:00
|
|
|
clEnumValN(ASTView, "ast-view",
|
2007-10-11 08:18:28 +08:00
|
|
|
"Build ASTs and view them with GraphViz."),
|
2007-08-22 05:42:03 +08:00
|
|
|
clEnumValN(ParseCFGDump, "dump-cfg",
|
2007-08-30 05:56:09 +08:00
|
|
|
"Run parser, then build and print CFGs."),
|
|
|
|
clEnumValN(ParseCFGView, "view-cfg",
|
2007-09-06 08:17:54 +08:00
|
|
|
"Run parser, then build and view CFGs with Graphviz."),
|
|
|
|
clEnumValN(AnalysisLiveVariables, "dump-live-variables",
|
2007-09-07 05:26:58 +08:00
|
|
|
"Print results of live variable analysis."),
|
2007-09-26 02:05:45 +08:00
|
|
|
clEnumValN(WarnDeadStores, "warn-dead-stores",
|
2007-09-07 07:00:42 +08:00
|
|
|
"Flag warnings of stores to dead variables."),
|
2007-09-26 02:05:45 +08:00
|
|
|
clEnumValN(WarnUninitVals, "warn-uninit-values",
|
2007-09-18 04:49:30 +08:00
|
|
|
"Flag warnings of uses of unitialized variables."),
|
2007-10-17 07:37:27 +08:00
|
|
|
clEnumValN(TestSerialization, "test-pickling",
|
|
|
|
"Run prototype serializtion code."),
|
2007-05-24 14:29:05 +08:00
|
|
|
clEnumValN(EmitLLVM, "emit-llvm",
|
2007-09-07 05:26:58 +08:00
|
|
|
"Build ASTs then convert to LLVM, emit .ll file"),
|
2007-10-11 08:43:27 +08:00
|
|
|
clEnumValN(RewriteTest, "rewrite-test",
|
|
|
|
"Playground for the code rewriter"),
|
2006-06-18 13:43:12 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2007-09-27 03:42:19 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
VerifyDiagnostics("verify",
|
|
|
|
llvm::cl::desc("Verify emitted diagnostics and warnings."));
|
|
|
|
|
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
|
|
|
|
*/
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<LangKind>
|
|
|
|
BaseLang("x", llvm::cl::desc("Base language to compile"),
|
|
|
|
llvm::cl::init(langkind_unspecified),
|
|
|
|
llvm::cl::values(clEnumValN(langkind_c, "c", "C"),
|
2006-10-17 13:16:26 +08:00
|
|
|
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));
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"),
|
|
|
|
llvm::cl::Hidden);
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"),
|
|
|
|
llvm::cl::Hidden);
|
2006-10-17 13:16:26 +08:00
|
|
|
|
|
|
|
/// 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,
|
2007-07-16 12:18:29 +08:00
|
|
|
lang_cxx98, lang_gnucxx98,
|
|
|
|
lang_cxx0x, lang_gnucxx0x
|
2006-10-17 13:16:26 +08:00
|
|
|
};
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<LangStds>
|
|
|
|
LangStd("std", llvm::cl::desc("Language standard to compile for"),
|
|
|
|
llvm::cl::init(lang_unspecified),
|
|
|
|
llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"),
|
2006-11-21 09:21:07 +08:00
|
|
|
clEnumValN(lang_c89, "c90", "ISO C 1990"),
|
2006-10-17 13:16:26 +08:00
|
|
|
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++)"),
|
2007-07-16 12:18:29 +08:00
|
|
|
clEnumValN(lang_cxx0x, "c++0x",
|
|
|
|
"Upcoming ISO C++ 200x with amendments"),
|
|
|
|
clEnumValN(lang_gnucxx0x, "gnu++0x",
|
|
|
|
"Upcoming ISO C++ 200x with amendments and GNU "
|
|
|
|
"extensions (default for C++)"),
|
2006-10-17 13:16:26 +08:00
|
|
|
clEnumValEnd));
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
2006-12-04 15:48:37 +08:00
|
|
|
NoOperatorNames("fno-operator-names",
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::cl::desc("Do not treat C++ operator name keywords as "
|
|
|
|
"synonyms for operators"));
|
2006-12-04 15:48:37 +08:00
|
|
|
|
2007-10-15 10:50:23 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
PascalStrings("fpascal-strings",
|
|
|
|
llvm::cl::desc("Recognize and construct Pascal-style "
|
|
|
|
"string literals"));
|
2007-11-28 13:34:05 +08:00
|
|
|
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
WritableStrings("fwritable-strings",
|
|
|
|
llvm::cl::desc("Store string literals as writable data."));
|
2007-11-30 12:21:22 +08:00
|
|
|
|
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
LaxVectorConversions("flax-vector-conversions",
|
|
|
|
llvm::cl::desc("Allow implicit conversions between vectors"
|
|
|
|
" with a different number of elements or "
|
|
|
|
"different element types."));
|
2006-10-17 13:16:26 +08:00
|
|
|
// FIXME: add:
|
|
|
|
// -ansi
|
|
|
|
// -trigraphs
|
|
|
|
// -fdollars-in-identifiers
|
2007-10-15 10:50:23 +08:00
|
|
|
// -fpascal-strings
|
2006-10-17 13:16:26 +08:00
|
|
|
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:
|
2007-05-04 05:03:48 +08:00
|
|
|
LangStd = lang_gnu99;
|
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.
|
2007-07-16 12:18:29 +08:00
|
|
|
case lang_gnucxx0x:
|
|
|
|
case lang_cxx0x:
|
|
|
|
Options.CPlusPlus0x = 1;
|
|
|
|
// FALL THROUGH
|
2006-10-17 13:16:26 +08:00
|
|
|
case lang_gnucxx98:
|
|
|
|
case lang_cxx98:
|
|
|
|
Options.CPlusPlus = 1;
|
2006-12-04 15:48:37 +08:00
|
|
|
Options.CXXOperatorNames = !NoOperatorNames;
|
2007-11-15 15:30:50 +08:00
|
|
|
Options.Boolean = 1;
|
2006-10-17 13:16:26 +08:00
|
|
|
// 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.
|
2007-10-15 10:50:23 +08:00
|
|
|
Options.PascalStrings = PascalStrings;
|
2007-11-28 13:34:05 +08:00
|
|
|
Options.WritableStrings = WritableStrings;
|
2007-11-30 12:21:22 +08:00
|
|
|
Options.LaxVectorConversions = LaxVectorConversions;
|
2006-10-17 13:16:26 +08:00
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Our DiagnosticClient implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// FIXME: Werror should take a list of things, -Werror=foo,bar
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
2007-07-07 07:09:18 +08:00
|
|
|
WarnOnExtensions("pedantic", llvm::cl::init(false),
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::cl::desc("Issue a warning on uses of GCC extensions"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
2006-06-18 13:43:12 +08:00
|
|
|
ErrorOnExtensions("pedantic-errors",
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::cl::desc("Issue an error on uses of GCC extensions"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
2006-07-05 08:55:08 +08:00
|
|
|
WarnUnusedMacros("Wunused_macros",
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::cl::desc("Warn for unused macros in the main translation unit"));
|
2006-07-05 08:55:08 +08:00
|
|
|
|
2007-11-14 02:37:02 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
WarnFloatEqual("Wfloat-equal",
|
|
|
|
llvm::cl::desc("Warn about equality comparisons of floating point values."));
|
|
|
|
|
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);
|
2007-11-14 02:37:02 +08:00
|
|
|
|
|
|
|
// Silence "floating point comparison" warnings unless requested.
|
|
|
|
if (!WarnFloatEqual)
|
|
|
|
Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2007-12-04 06:06:55 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Target Triple Processing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static llvm::cl::opt<std::string>
|
|
|
|
TargetTriple("triple",
|
|
|
|
llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)."));
|
|
|
|
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
Archs("arch",
|
|
|
|
llvm::cl::desc("Specify target architecture (e.g. i686)."));
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class TripleProcessor {
|
|
|
|
llvm::StringMap<char> TriplesProcessed;
|
|
|
|
std::vector<std::string>& triples;
|
|
|
|
public:
|
|
|
|
TripleProcessor(std::vector<std::string>& t) : triples(t) {}
|
|
|
|
|
|
|
|
void addTriple(const std::string& t) {
|
|
|
|
if (TriplesProcessed.find(t.c_str(),t.c_str()+t.size()) ==
|
|
|
|
TriplesProcessed.end()) {
|
|
|
|
triples.push_back(t);
|
|
|
|
TriplesProcessed.GetOrCreateValue(t.c_str(),t.c_str()+t.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CreateTargetTriples(std::vector<std::string>& triples) {
|
|
|
|
std::string base_triple;
|
|
|
|
|
|
|
|
// Initialize base triple. If a -triple option has been specified, use
|
|
|
|
// that triple. Otherwise, default to the host triple.
|
2007-12-04 07:23:21 +08:00
|
|
|
if (TargetTriple.getValue().empty()) {
|
|
|
|
// HACK: For non-darwin systems, we don't have any real target support
|
|
|
|
// yet. For these systems, set the target to darwin.
|
2007-12-04 07:25:59 +08:00
|
|
|
if (!strstr(LLVM_HOSTTRIPLE,"darwin"))
|
2007-12-04 07:23:21 +08:00
|
|
|
base_triple = "i386-apple-darwin";
|
|
|
|
else
|
|
|
|
base_triple = LLVM_HOSTTRIPLE;
|
|
|
|
}
|
2007-12-04 06:06:55 +08:00
|
|
|
else
|
|
|
|
base_triple = TargetTriple.getValue();
|
|
|
|
|
|
|
|
// Decompose the base triple into "arch" and suffix.
|
|
|
|
std::string::size_type firstDash = base_triple.find("-");
|
|
|
|
|
2007-12-04 06:11:31 +08:00
|
|
|
if (firstDash == std::string::npos) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Malformed target triple: \"%s\" ('-' could not be found).\n",
|
|
|
|
base_triple.c_str());
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-12-04 06:06:55 +08:00
|
|
|
|
|
|
|
std::string suffix(base_triple,firstDash+1);
|
2007-12-04 06:11:31 +08:00
|
|
|
|
|
|
|
if (suffix.empty()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Malformed target triple: \"%s\" (no vendor or OS).\n",
|
|
|
|
base_triple.c_str());
|
|
|
|
exit (1);
|
|
|
|
}
|
2007-12-04 06:06:55 +08:00
|
|
|
|
|
|
|
// Create triple cacher.
|
|
|
|
TripleProcessor tp(triples);
|
|
|
|
|
|
|
|
// Add the primary triple to our set of triples if we are using the
|
|
|
|
// host-triple with no archs or using a specified target triple.
|
|
|
|
if (!TargetTriple.getValue().empty() || Archs.empty())
|
|
|
|
tp.addTriple(base_triple);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Archs.size(); i !=e; ++i)
|
|
|
|
tp.addTriple(Archs[i] + "-" + suffix);
|
|
|
|
}
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Initialization
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// FIXME: Preprocessor builtins to support.
|
|
|
|
// -A... - Play with #assertions
|
|
|
|
// -undef - Undefine all predefined macros
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Predefine the specified macro"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Undefine the specified macro"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
|
2007-10-10 06:10:18 +08:00
|
|
|
|
|
|
|
/// InitializePreprocessor - Initialize the preprocessor getting it and the
|
|
|
|
/// environment ready to process a single file. This returns the file ID for the
|
|
|
|
/// input file. If a failure happens, it returns 0.
|
|
|
|
///
|
|
|
|
static unsigned InitializePreprocessor(Preprocessor &PP,
|
|
|
|
const std::string &InFile,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
HeaderSearch &HeaderInfo,
|
|
|
|
const LangOptions &LangInfo,
|
|
|
|
std::vector<char> &PredefineBuffer) {
|
|
|
|
|
|
|
|
FileManager &FileMgr = HeaderInfo.getFileMgr();
|
|
|
|
|
|
|
|
// Figure out where to get and map in the main file.
|
|
|
|
unsigned MainFileID = 0;
|
|
|
|
if (InFile != "-") {
|
|
|
|
const FileEntry *File = FileMgr.getFile(InFile);
|
|
|
|
if (File) MainFileID = SourceMgr.createFileID(File, SourceLocation());
|
|
|
|
if (MainFileID == 0) {
|
|
|
|
fprintf(stderr, "Error reading '%s'!\n",InFile.c_str());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN();
|
|
|
|
if (SB) MainFileID = SourceMgr.createFileIDForMemBuffer(SB);
|
|
|
|
if (MainFileID == 0) {
|
|
|
|
fprintf(stderr, "Error reading standard input! Empty?\n");
|
|
|
|
return 0;
|
|
|
|
}
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2007-10-10 06:10:18 +08:00
|
|
|
DefineBuiltinMacro(PredefineBuffer, D_macros[i].c_str());
|
2006-06-18 13:43:12 +08:00
|
|
|
for (unsigned i = 0, e = U_macros.size(); i != e; ++i)
|
2007-10-10 06:10:18 +08:00
|
|
|
DefineBuiltinMacro(PredefineBuffer, U_macros[i].c_str(), "#undef ");
|
|
|
|
|
|
|
|
// FIXME: Read any files specified by -imacros or -include.
|
|
|
|
|
|
|
|
// Null terminate PredefinedBuffer and add it.
|
|
|
|
PredefineBuffer.push_back(0);
|
|
|
|
PP.setPredefines(&PredefineBuffer[0]);
|
|
|
|
|
|
|
|
// Once we've read this, we're done.
|
|
|
|
return MainFileID;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-10-10 06:10:18 +08:00
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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++
|
2007-08-27 01:47:35 +08:00
|
|
|
// FIXME: -imultilib
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// FIXME: -include,-imacros
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::opt<bool>
|
|
|
|
nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Various command line options. These four add directories to each chain.
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Add directory to framework include search path"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Add directory to include search path"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Add directory to AFTER include search path"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Add directory to QUOTE include search path"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Add directory to SYSTEM include search path"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Set directory to SYSTEM include search path with prefix"));
|
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"),
|
|
|
|
llvm::cl::Prefix,
|
|
|
|
llvm::cl::desc("Set directory to include search path with prefix"));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-08-27 01:47:35 +08:00
|
|
|
static llvm::cl::opt<std::string>
|
|
|
|
isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"),
|
|
|
|
llvm::cl::desc("Set the system root directory (usually /)"));
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// 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) {
|
2007-08-27 01:47:35 +08:00
|
|
|
const DirectoryEntry *DE;
|
|
|
|
if (Group == System)
|
|
|
|
DE = FM.getDirectory(isysroot + "/" + Path);
|
|
|
|
else
|
|
|
|
DE = FM.getDirectory(Path);
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (DE == 0) {
|
|
|
|
if (Verbose)
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "ignoring nonexistent directory \"%s\"\n",
|
|
|
|
Path.c_str());
|
2006-06-18 13:43:12 +08:00
|
|
|
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)
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
|
|
|
|
SearchList[i].getDir()->getName());
|
2006-06-18 13:43:12 +08:00
|
|
|
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,
|
2007-12-06 07:24:17 +08:00
|
|
|
const LangOptions &Lang) {
|
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.
|
2007-12-06 07:24:17 +08:00
|
|
|
for (unsigned i = 0, e = I_dirs.size(); i != e; ++i)
|
|
|
|
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-11-21 11:40:37 +08:00
|
|
|
if (Lang.CPlusPlus) {
|
|
|
|
AddPath("/usr/include/c++/4.0.0", System, true, false, false, FM);
|
|
|
|
AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false,
|
|
|
|
false, FM);
|
|
|
|
AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false,FM);
|
|
|
|
}
|
|
|
|
|
2006-10-17 14:20:32 +08:00
|
|
|
AddPath("/usr/local/include", System, false, false, false, FM);
|
2007-03-20 03:06:33 +08:00
|
|
|
// leopard
|
2007-03-15 05:52:03 +08:00
|
|
|
AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
|
2006-10-20 12:55:39 +08:00
|
|
|
false, false, false, FM);
|
2007-03-15 05:52:03 +08:00
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
|
2006-10-17 14:20:32 +08:00
|
|
|
System, false, false, false, FM);
|
2007-03-15 05:52:03 +08:00
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
|
2007-03-20 03:06:33 +08:00
|
|
|
"4.0.1/../../../../powerpc-apple-darwin0/include",
|
|
|
|
System, false, false, false, FM);
|
|
|
|
|
|
|
|
// tiger
|
|
|
|
AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System,
|
|
|
|
false, false, false, FM);
|
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include",
|
|
|
|
System, false, false, false, FM);
|
|
|
|
AddPath("/usr/lib/gcc/powerpc-apple-darwin8/"
|
2006-06-18 13:43:12 +08:00
|
|
|
"4.0.1/../../../../powerpc-apple-darwin8/include",
|
2006-10-17 14:20:32 +08:00
|
|
|
System, false, false, false, FM);
|
2007-03-20 03:06:33 +08:00
|
|
|
|
2006-10-17 14:20:32 +08:00
|
|
|
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) {
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "#include \"...\" search starts here:\n");
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned QuotedIdx = IncludeGroup[Quoted].size();
|
|
|
|
for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
|
|
|
|
if (i == QuotedIdx)
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "#include <...> search starts here:\n");
|
|
|
|
fprintf(stderr, " %s\n", SearchList[i].getDir()->getName());
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-11-06 02:39:59 +08:00
|
|
|
static void ParseFile(Preprocessor &PP, MinimalAction *PA, unsigned MainFileID){
|
2006-07-31 09:59:18 +08:00
|
|
|
Parser P(PP, *PA);
|
2007-10-10 06:10:18 +08:00
|
|
|
PP.EnterMainSourceFile(MainFileID);
|
2006-07-31 09:59:18 +08:00
|
|
|
|
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-06-18 13:43:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Main driver
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-06 02:27:04 +08:00
|
|
|
/// CreateASTConsumer - Create the ASTConsumer for the corresponding program
|
|
|
|
/// action. These consumers can operate on both ASTs that are freshly
|
|
|
|
/// parsed from source files as well as those deserialized from Bitcode.
|
|
|
|
static ASTConsumer* CreateASTConsumer(Diagnostic& Diag, FileManager& FileMgr,
|
|
|
|
const LangOptions& LangOpts) {
|
|
|
|
switch (ProgAction) {
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
case ASTPrint:
|
|
|
|
return CreateASTPrinter();
|
|
|
|
|
|
|
|
case ASTDump:
|
|
|
|
return CreateASTDumper();
|
|
|
|
|
|
|
|
case ASTView:
|
|
|
|
return CreateASTViewer();
|
|
|
|
|
|
|
|
case ParseCFGDump:
|
|
|
|
case ParseCFGView:
|
|
|
|
return CreateCFGDumper(ProgAction == ParseCFGView);
|
|
|
|
|
|
|
|
case AnalysisLiveVariables:
|
|
|
|
return CreateLiveVarAnalyzer();
|
|
|
|
|
|
|
|
case WarnDeadStores:
|
|
|
|
return CreateDeadStoreChecker(Diag);
|
|
|
|
|
|
|
|
case WarnUninitVals:
|
|
|
|
return CreateUnitValsChecker(Diag);
|
|
|
|
|
|
|
|
case TestSerialization:
|
2007-12-06 05:34:36 +08:00
|
|
|
return CreateSerializationTest(Diag, FileMgr, LangOpts);
|
2007-12-06 02:27:04 +08:00
|
|
|
|
|
|
|
case EmitLLVM:
|
|
|
|
return CreateLLVMEmitter(Diag, LangOpts);
|
|
|
|
|
|
|
|
case RewriteTest:
|
|
|
|
return CreateCodeRewriterTest(Diag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 05:20:00 +08:00
|
|
|
/// ProcessInputFile - Process a single input file with the specified state.
|
|
|
|
///
|
|
|
|
static void ProcessInputFile(Preprocessor &PP, unsigned MainFileID,
|
|
|
|
const std::string &InFile,
|
|
|
|
SourceManager &SourceMgr,
|
|
|
|
TextDiagnostics &OurDiagnosticClient,
|
|
|
|
HeaderSearch &HeaderInfo,
|
|
|
|
const LangOptions &LangInfo) {
|
2007-09-27 02:39:29 +08:00
|
|
|
|
|
|
|
ASTConsumer* Consumer = NULL;
|
2007-07-22 14:05:44 +08:00
|
|
|
bool ClearSourceMgr = false;
|
2007-09-27 02:39:29 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
switch (ProgAction) {
|
2007-06-27 11:19:45 +08:00
|
|
|
default:
|
2007-12-06 02:27:04 +08:00
|
|
|
Consumer = CreateASTConsumer(PP.getDiagnostics(), HeaderInfo.getFileMgr(),
|
|
|
|
PP.getLangOptions());
|
|
|
|
|
|
|
|
if (!Consumer) {
|
|
|
|
fprintf(stderr, "Unexpected program action!\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
case DumpTokens: { // Token dump mode.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2006-07-29 14:35:08 +08:00
|
|
|
// Start parsing the specified input file.
|
2007-10-10 06:10:18 +08:00
|
|
|
PP.EnterMainSourceFile(MainFileID);
|
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);
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "\n");
|
2007-10-10 02:03:42 +08:00
|
|
|
} while (Tok.isNot(tok::eof));
|
2007-07-22 14:05:44 +08:00
|
|
|
ClearSourceMgr = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
|
2007-07-21 00:59:19 +08:00
|
|
|
Token Tok;
|
2006-07-29 14:35:08 +08:00
|
|
|
// Start parsing the specified input file.
|
2007-10-10 06:10:18 +08:00
|
|
|
PP.EnterMainSourceFile(MainFileID);
|
2006-06-18 13:43:12 +08:00
|
|
|
do {
|
2006-06-18 14:48:37 +08:00
|
|
|
PP.Lex(Tok);
|
2007-10-10 02:03:42 +08:00
|
|
|
} while (Tok.isNot(tok::eof));
|
2007-07-22 14:05:44 +08:00
|
|
|
ClearSourceMgr = true;
|
2006-06-18 13:43:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2006-07-31 09:59:18 +08:00
|
|
|
|
|
|
|
case PrintPreprocessedInput: // -E mode.
|
2006-11-21 13:52:55 +08:00
|
|
|
DoPrintPreprocessedInput(MainFileID, PP, LangInfo);
|
2007-07-22 14:05:44 +08:00
|
|
|
ClearSourceMgr = true;
|
2006-07-31 09:59:18 +08:00
|
|
|
break;
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
case ParseNoop: // -parse-noop
|
2007-11-01 04:55:39 +08:00
|
|
|
ParseFile(PP, new MinimalAction(PP.getIdentifierTable()), MainFileID);
|
2007-07-22 14:05:44 +08:00
|
|
|
ClearSourceMgr = true;
|
2006-08-17 13:51:27 +08:00
|
|
|
break;
|
2006-11-06 02:00:10 +08:00
|
|
|
|
2006-07-31 09:59:18 +08:00
|
|
|
case ParsePrintCallbacks:
|
2007-11-01 04:55:39 +08:00
|
|
|
ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable()),
|
|
|
|
MainFileID);
|
2007-07-22 14:05:44 +08:00
|
|
|
ClearSourceMgr = true;
|
2006-08-17 13:51:27 +08:00
|
|
|
break;
|
2007-09-27 02:39:29 +08:00
|
|
|
|
2006-11-21 12:07:21 +08:00
|
|
|
case ParseSyntaxOnly: // -fsyntax-only
|
2007-09-27 02:39:29 +08:00
|
|
|
Consumer = new ASTConsumer();
|
2006-08-07 02:29:56 +08:00
|
|
|
break;
|
2007-09-17 03:46:59 +08:00
|
|
|
}
|
2007-09-27 02:39:29 +08:00
|
|
|
|
|
|
|
if (Consumer) {
|
2007-09-27 04:14:22 +08:00
|
|
|
if (VerifyDiagnostics)
|
2007-11-03 14:24:16 +08:00
|
|
|
exit(CheckASTConsumer(PP, MainFileID, Consumer));
|
|
|
|
|
|
|
|
// This deletes Consumer.
|
|
|
|
ParseAST(PP, MainFileID, Consumer, Stats);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2006-11-21 13:52:55 +08:00
|
|
|
if (Stats) {
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
|
2006-11-21 13:52:55 +08:00
|
|
|
PP.PrintStats();
|
2006-11-21 14:08:20 +08:00
|
|
|
PP.getIdentifierTable().PrintStats();
|
2006-11-21 14:34:57 +08:00
|
|
|
HeaderInfo.PrintStats();
|
2007-07-22 14:05:44 +08:00
|
|
|
if (ClearSourceMgr)
|
|
|
|
SourceMgr.PrintStats();
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "\n");
|
2006-11-21 13:52:55 +08:00
|
|
|
}
|
2007-07-22 14:05:44 +08:00
|
|
|
|
|
|
|
// For a multi-file compilation, some things are ok with nuking the source
|
|
|
|
// manager tables, other require stable fileid/macroid's across multiple
|
|
|
|
// files.
|
|
|
|
if (ClearSourceMgr) {
|
|
|
|
SourceMgr.clearIDTables();
|
|
|
|
}
|
2006-11-21 13:52:55 +08:00
|
|
|
}
|
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
static llvm::cl::list<std::string>
|
|
|
|
InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
|
|
|
|
llvm::sys::PrintStackTraceOnErrorSignal();
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
// If no input was specified, read from stdin.
|
|
|
|
if (InputFilenames.empty())
|
|
|
|
InputFilenames.push_back("-");
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
/// Create a SourceManager object. This tracks and owns all the file buffers
|
|
|
|
/// allocated to the program.
|
|
|
|
SourceManager SourceMgr;
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
// Create a file manager object to provide access to and cache the filesystem.
|
|
|
|
FileManager FileMgr;
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
// Initialize language options, inferring file types from input filenames.
|
|
|
|
// FIXME: This infers info from the first file, we should clump by language
|
|
|
|
// to handle 'x.c y.c a.cpp b.cpp'.
|
|
|
|
LangOptions LangInfo;
|
|
|
|
InitializeBaseLanguage(LangInfo, InputFilenames[0]);
|
|
|
|
InitializeLanguageStandard(LangInfo);
|
2007-06-16 05:20:00 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
std::auto_ptr<TextDiagnostics> DiagClient;
|
2007-09-27 04:14:22 +08:00
|
|
|
if (!VerifyDiagnostics) {
|
2007-06-28 12:54:17 +08:00
|
|
|
// Print diagnostics to stderr by default.
|
|
|
|
DiagClient.reset(new TextDiagnosticPrinter(SourceMgr));
|
|
|
|
} else {
|
|
|
|
// When checking diagnostics, just buffer them up.
|
|
|
|
DiagClient.reset(new TextDiagnosticBuffer(SourceMgr));
|
|
|
|
|
|
|
|
if (InputFilenames.size() != 1) {
|
|
|
|
fprintf(stderr,
|
2007-09-27 04:14:22 +08:00
|
|
|
"-verify only works on single input files for now.\n");
|
2007-06-28 12:54:17 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2007-06-16 05:20:00 +08:00
|
|
|
}
|
2006-11-21 13:52:55 +08:00
|
|
|
|
2007-06-27 11:19:45 +08:00
|
|
|
// Configure our handling of diagnostics.
|
2007-06-28 12:54:17 +08:00
|
|
|
Diagnostic Diags(*DiagClient);
|
2007-06-27 11:19:45 +08:00
|
|
|
InitializeDiagnostics(Diags);
|
|
|
|
|
|
|
|
// Get information about the targets being compiled for. Note that this
|
|
|
|
// pointer and the TargetInfoImpl objects are never deleted by this toy
|
|
|
|
// driver.
|
2007-12-04 06:06:55 +08:00
|
|
|
TargetInfo *Target;
|
|
|
|
|
|
|
|
{ // Create triples, and create the TargetInfo.
|
|
|
|
std::vector<std::string> triples;
|
|
|
|
CreateTargetTriples(triples);
|
2007-12-06 05:34:36 +08:00
|
|
|
Target = CreateTargetInfo(triples,&Diags);
|
2007-12-04 06:06:55 +08:00
|
|
|
|
2007-12-04 07:23:21 +08:00
|
|
|
if (Target == 0) {
|
|
|
|
fprintf(stderr, "Sorry, I don't know what target this is: %s\n",
|
|
|
|
triples[0].c_str());
|
|
|
|
fprintf(stderr, "Please use -triple or -arch.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-06-27 11:19:45 +08:00
|
|
|
}
|
|
|
|
|
2007-12-06 07:24:17 +08:00
|
|
|
// -I- is a deprecated GCC feature, scan for it and reject it.
|
|
|
|
for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
|
|
|
|
if (I_dirs[i] == "-") {
|
|
|
|
Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported);
|
|
|
|
I_dirs.erase(I_dirs.begin()+i);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 11:19:45 +08:00
|
|
|
// Process the -I options and set them in the HeaderInfo.
|
|
|
|
HeaderSearch HeaderInfo(FileMgr);
|
2007-06-28 12:54:17 +08:00
|
|
|
DiagClient->setHeaderSearch(HeaderInfo);
|
2007-12-06 07:24:17 +08:00
|
|
|
InitializeIncludePaths(HeaderInfo, FileMgr, LangInfo);
|
2007-06-27 11:19:45 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
|
|
|
|
// Set up the preprocessor with these options.
|
|
|
|
Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
|
|
|
|
const std::string &InFile = InputFilenames[i];
|
2007-10-10 06:10:18 +08:00
|
|
|
std::vector<char> PredefineBuffer;
|
2007-06-27 11:19:45 +08:00
|
|
|
unsigned MainFileID = InitializePreprocessor(PP, InFile, SourceMgr,
|
2007-06-28 12:10:04 +08:00
|
|
|
HeaderInfo, LangInfo,
|
2007-10-10 06:10:18 +08:00
|
|
|
PredefineBuffer);
|
2007-06-28 12:54:17 +08:00
|
|
|
|
2007-06-27 11:19:45 +08:00
|
|
|
if (!MainFileID) continue;
|
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
ProcessInputFile(PP, MainFileID, InFile, SourceMgr,
|
|
|
|
*DiagClient, HeaderInfo, LangInfo);
|
2007-06-27 11:19:45 +08:00
|
|
|
HeaderInfo.ClearFileInfo();
|
|
|
|
}
|
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
unsigned NumDiagnostics = Diags.getNumDiagnostics();
|
2007-06-27 11:19:45 +08:00
|
|
|
|
2007-06-28 12:54:17 +08:00
|
|
|
if (NumDiagnostics)
|
|
|
|
fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
|
|
|
|
(NumDiagnostics == 1 ? "" : "s"));
|
2006-08-14 06:25:42 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
if (Stats) {
|
2006-11-21 14:08:20 +08:00
|
|
|
// Printed from high-to-low level.
|
|
|
|
SourceMgr.PrintStats();
|
|
|
|
FileMgr.PrintStats();
|
2007-06-23 06:43:15 +08:00
|
|
|
fprintf(stderr, "\n");
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2006-10-25 11:15:08 +08:00
|
|
|
|
2007-07-21 13:40:53 +08:00
|
|
|
return Diags.getNumErrors() != 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|