Stage two of getting CFE top correct.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39734 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Driver/ASTStreamers.cpp b/Driver/ASTStreamers.cpp
new file mode 100644
index 0000000..19e12bd
--- /dev/null
+++ b/Driver/ASTStreamers.cpp
@@ -0,0 +1,109 @@
+//===--- ASTStreamers.cpp - ASTStreamer Drivers ---------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// ASTStreamer drivers.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ASTStreamers.h"
+#include "clang/AST/AST.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Sema/ASTStreamer.h"
+
+void clang::BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
+ // collect global stats on Decls/Stmts (until we have a module streamer)
+ if (Stats) {
+ Decl::CollectingStats(true);
+ Stmt::CollectingStats(true);
+ }
+
+ ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
+ ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
+
+ while (ASTStreamer_ReadTopLevelDecl(Streamer))
+ /* keep reading */;
+
+ if (Stats) {
+ fprintf(stderr, "\nSTATISTICS:\n");
+ ASTStreamer_PrintStats(Streamer);
+ Context.PrintStats();
+ Decl::PrintStats();
+ Stmt::PrintStats();
+ }
+
+ ASTStreamer_Terminate(Streamer);
+}
+
+void clang::PrintFunctionDecl(FunctionDecl *FD) {
+ bool HasBody = FD->getBody();
+
+ std::string Proto = FD->getName();
+ FunctionType *AFT = cast<FunctionType>(FD->getType());
+
+ if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(AFT)) {
+ Proto += "(";
+ for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
+ if (i) Proto += ", ";
+ std::string ParamStr;
+ if (HasBody) ParamStr = FD->getParamDecl(i)->getName();
+
+ FT->getArgType(i).getAsStringInternal(ParamStr);
+ Proto += ParamStr;
+ }
+
+ if (FT->isVariadic()) {
+ if (FD->getNumParams()) Proto += ", ";
+ Proto += "...";
+ }
+ Proto += ")";
+ } else {
+ assert(isa<FunctionTypeNoProto>(AFT));
+ Proto += "()";
+ }
+
+ AFT->getResultType().getAsStringInternal(Proto);
+ fprintf(stderr, "\n%s", Proto.c_str());
+
+ if (FD->getBody()) {
+ fprintf(stderr, " ");
+ FD->getBody()->dump();
+ fprintf(stderr, "\n");
+ } else {
+ fprintf(stderr, ";\n");
+ }
+}
+
+void clang::PrintTypeDefDecl(TypedefDecl *TD) {
+ std::string S = TD->getName();
+ TD->getUnderlyingType().getAsStringInternal(S);
+ fprintf(stderr, "typedef %s;\n", S.c_str());
+}
+
+void clang::PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats) {
+ ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
+ ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
+
+ while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
+ if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ PrintFunctionDecl(FD);
+ } else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
+ PrintTypeDefDecl(TD);
+ } else {
+ fprintf(stderr, "Read top-level variable decl: '%s'\n", D->getName());
+ }
+ }
+
+ if (Stats) {
+ fprintf(stderr, "\nSTATISTICS:\n");
+ ASTStreamer_PrintStats(Streamer);
+ Context.PrintStats();
+ }
+
+ ASTStreamer_Terminate(Streamer);
+}
diff --git a/Driver/ASTStreamers.h b/Driver/ASTStreamers.h
new file mode 100644
index 0000000..2cce217
--- /dev/null
+++ b/Driver/ASTStreamers.h
@@ -0,0 +1,30 @@
+//===--- ASTStreamers.h - ASTStreamer Drivers -------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// AST Streamers.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef DRIVER_ASTSTREAMERS_H_
+#define DRIVER_ASTSTREAMERS_H_
+
+namespace clang {
+
+class Preprocessor;
+class FunctionDecl;
+class TypedefDecl;
+
+void BuildASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
+void PrintASTs(Preprocessor &PP, unsigned MainFileID, bool Stats);
+void PrintFunctionDecl(FunctionDecl *FD);
+void PrintTypeDefDecl(TypedefDecl *TD);
+
+} // end clang namespace
+
+#endif
diff --git a/Driver/DiagChecker.cpp b/Driver/DiagChecker.cpp
new file mode 100644
index 0000000..76b0526
--- /dev/null
+++ b/Driver/DiagChecker.cpp
@@ -0,0 +1,230 @@
+//===--- DiagChecker.cpp - Diagnostic Checking Functions ------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Process the input files and check that the diagnostic messages are expected.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "ASTStreamers.h"
+#include "TextDiagnosticBuffer.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/Preprocessor.h"
+using namespace clang;
+
+typedef TextDiagnosticBuffer::DiagList DiagList;
+typedef TextDiagnosticBuffer::const_iterator const_diag_iterator;
+
+// USING THE DIAGNOSTIC CHECKER:
+//
+// Indicating that a line expects an error or a warning is simple. Put a comment
+// on the line that has the diagnostic, use "expected-{error,warning}" to tag
+// if it's an expected error or warning, and place the expected text between {{
+// and }} markers. The full text doesn't have to be included, only enough to
+// ensure that the correct diagnostic was emitted.
+//
+// Here's an example:
+//
+// int A = B; // expected-error {{use of undeclared identifier 'B'}}
+//
+// You can place as many diagnostics on one line as you wish. To make the code
+// more readable, you can use slash-newline to separate out the diagnostics.
+
+static const char * const ExpectedErrStr = "expected-error";
+static const char * const ExpectedWarnStr = "expected-warning";
+
+/// FindDiagnostics - Go through the comment and see if it indicates expected
+/// diagnostics. If so, then put them in a diagnostic list.
+///
+static void FindDiagnostics(const std::string &Comment,
+ DiagList &ExpectedDiags,
+ SourceManager &SourceMgr,
+ SourceLocation Pos,
+ const char * const ExpectedStr) {
+ // Find all expected diagnostics
+ typedef std::string::size_type size_type;
+ size_type ColNo = std::string::npos;
+
+ for (;;) {
+ ColNo = Comment.find(ExpectedStr, ColNo);
+ if (ColNo == std::string::npos) break;
+
+ size_type OpenDiag = Comment.find_first_of("{{", ColNo);
+
+ if (OpenDiag == std::string::npos) {
+ fprintf(stderr,
+ "oops:%d: Cannot find beginning of expected error string\n",
+ SourceMgr.getLineNumber(Pos));
+ break;
+ }
+
+ OpenDiag += 2;
+ size_type CloseDiag = Comment.find_first_of("}}", OpenDiag);
+
+ if (CloseDiag == std::string::npos) {
+ fprintf(stderr,
+ "oops:%d: Cannot find end of expected error string\n",
+ SourceMgr.getLineNumber(Pos));
+ break;
+ }
+
+ std::string Msg(Comment.substr(OpenDiag, CloseDiag - OpenDiag));
+ ExpectedDiags.push_back(std::make_pair(Pos, Msg));
+ ColNo = CloseDiag + 2;
+ }
+}
+
+/// FindExpectedDiags - Lex the file to finds all of the expected errors and
+/// warnings.
+static void FindExpectedDiags(Preprocessor &PP, unsigned MainFileID,
+ DiagList &ExpectedErrors,
+ DiagList &ExpectedWarnings) {
+ // Return comments as tokens, this is how we find expected diagnostics.
+ PP.SetCommentRetentionState(true, true);
+
+ // Enter the cave.
+ PP.EnterSourceFile(MainFileID, 0, true);
+
+ LexerToken Tok;
+ do {
+ PP.Lex(Tok);
+
+ if (Tok.getKind() == tok::comment) {
+ std::string Comment = PP.getSpelling(Tok);
+
+ // Find all expected errors
+ FindDiagnostics(Comment, ExpectedErrors,PP.getSourceManager(),
+ Tok.getLocation(), ExpectedErrStr);
+
+ // Find all expected warnings
+ FindDiagnostics(Comment, ExpectedWarnings, PP.getSourceManager(),
+ Tok.getLocation(), ExpectedWarnStr);
+ }
+ } while (Tok.getKind() != tok::eof);
+
+ PP.SetCommentRetentionState(false, false);
+}
+
+/// PrintProblem - This takes a diagnostic map of the delta between expected and
+/// seen diagnostics. If there's anything in it, then something unexpected
+/// happened. Print the map out in a nice format and return "true". If the map
+/// is empty and we're not going to print things, then return "false".
+///
+static bool PrintProblem(SourceManager &SourceMgr,
+ const_diag_iterator diag_begin,
+ const_diag_iterator diag_end,
+ const char *Msg) {
+ if (diag_begin == diag_end) return false;
+
+ fprintf(stderr, "%s\n", Msg);
+
+ for (const_diag_iterator I = diag_begin, E = diag_end; I != E; ++I)
+ fprintf(stderr, " Line %d: %s\n",
+ SourceMgr.getLineNumber(I->first),
+ I->second.c_str());
+
+ return true;
+}
+
+/// CompareDiagLists - Compare two diangnostic lists and return the difference
+/// between them.
+///
+static bool CompareDiagLists(SourceManager &SourceMgr,
+ const_diag_iterator d1_begin,
+ const_diag_iterator d1_end,
+ const_diag_iterator d2_begin,
+ const_diag_iterator d2_end,
+ const char *Msg) {
+ DiagList DiffList;
+
+ for (const_diag_iterator I = d1_begin, E = d1_end; I != E; ++I) {
+ unsigned LineNo1 = SourceMgr.getLineNumber(I->first);
+ const std::string &Diag1 = I->second;
+ bool Found = false;
+
+ for (const_diag_iterator II = d2_begin, IE = d2_end; II != IE; ++II) {
+ unsigned LineNo2 = SourceMgr.getLineNumber(II->first);
+ if (LineNo1 != LineNo2) continue;
+
+ const std::string &Diag2 = II->second;
+ if (Diag2.find(Diag1) != std::string::npos ||
+ Diag1.find(Diag2) != std::string::npos) {
+ Found = true;
+ break;
+ }
+ }
+
+ if (!Found)
+ DiffList.push_back(std::make_pair(I->first, Diag1));
+ }
+
+ return PrintProblem(SourceMgr, DiffList.begin(), DiffList.end(), Msg);
+}
+
+/// CheckResults - This compares the expected results to those that
+/// were actually reported. It emits any discrepencies. Return "true" if there
+/// were problems. Return "false" otherwise.
+///
+static bool CheckResults(Preprocessor &PP,
+ const DiagList &ExpectedErrors,
+ const DiagList &ExpectedWarnings) {
+ const TextDiagnosticBuffer &Diags =
+ static_cast<const TextDiagnosticBuffer&>(PP.getDiagnostics().getClient());
+ SourceManager &SourceMgr = PP.getSourceManager();
+
+ // We want to capture the delta between what was expected and what was
+ // seen.
+ //
+ // Expected \ Seen - set expected but not seen
+ // Seen \ Expected - set seen but not expected
+ bool HadProblem = false;
+
+ // See if there were errors that were expected but not seen.
+ HadProblem |= CompareDiagLists(SourceMgr,
+ ExpectedErrors.begin(), ExpectedErrors.end(),
+ Diags.err_begin(), Diags.err_end(),
+ "Errors expected but not seen:");
+
+ // See if there were errors that were seen but not expected.
+ HadProblem |= CompareDiagLists(SourceMgr,
+ Diags.err_begin(), Diags.err_end(),
+ ExpectedErrors.begin(), ExpectedErrors.end(),
+ "Errors seen but not expected:");
+
+ // See if there were warnings that were expected but not seen.
+ HadProblem |= CompareDiagLists(SourceMgr,
+ ExpectedWarnings.begin(),
+ ExpectedWarnings.end(),
+ Diags.warn_begin(), Diags.warn_end(),
+ "Warnings expected but not seen:");
+
+ // See if there were warnings that were seen but not expected.
+ HadProblem |= CompareDiagLists(SourceMgr,
+ Diags.warn_begin(), Diags.warn_end(),
+ ExpectedWarnings.begin(),
+ ExpectedWarnings.end(),
+ "Warnings seen but not expected:");
+
+ return HadProblem;
+}
+
+/// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
+bool clang::CheckDiagnostics(Preprocessor &PP, unsigned MainFileID) {
+ // Gather the set of expected diagnostics.
+ DiagList ExpectedErrors, ExpectedWarnings;
+ FindExpectedDiags(PP, MainFileID, ExpectedErrors, ExpectedWarnings);
+
+ // Parse the specified input file.
+ BuildASTs(PP, MainFileID, false);
+
+ // Check that the expected diagnostics occurred.
+ return CheckResults(PP, ExpectedErrors, ExpectedWarnings);
+}
+
+
diff --git a/Driver/LLVMCodegen.cpp b/Driver/LLVMCodegen.cpp
new file mode 100644
index 0000000..e593b66
--- /dev/null
+++ b/Driver/LLVMCodegen.cpp
@@ -0,0 +1,68 @@
+//===--- LLVMCodegen.cpp - Emit LLVM Code from ASTs -----------------------===//
+//
+// 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 builds an AST and converts it to LLVM Code.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/CodeGen/ModuleBuilder.h"
+#include "clang/Sema/ASTStreamer.h"
+#include "clang/AST/AST.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Basic/Diagnostic.h"
+#include "llvm/Module.h"
+#include <iostream>
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// LLVM Emission
+//===----------------------------------------------------------------------===//
+
+void clang::EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
+ bool PrintStats) {
+ Diagnostic &Diags = PP.getDiagnostics();
+ // Create the streamer to read the file.
+ ASTContext Context(PP.getTargetInfo(), PP.getIdentifierTable());
+ ASTStreamerTy *Streamer = ASTStreamer_Init(PP, Context, MainFileID);
+
+ // Create the module to codegen into.
+ llvm::Module M("foo");
+
+ CodeGen::BuilderTy *Builder = CodeGen::Init(Context, M);
+
+ while (Decl *D = ASTStreamer_ReadTopLevelDecl(Streamer)) {
+ // If an error occurred, stop code generation, but continue parsing and
+ // semantic analysis (to ensure all warnings and errors are emitted).
+ if (Diags.hasErrorOccurred())
+ continue;
+
+ if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ CodeGen::CodeGenFunction(Builder, FD);
+ } else if (isa<TypedefDecl>(D)) {
+ std::cerr << "Read top-level typedef decl: '" << D->getName() << "'\n";
+ } else {
+ std::cerr << "Read top-level variable decl: '" << D->getName() << "'\n";
+ }
+ }
+
+ if (PrintStats) {
+ std::cerr << "\nSTATISTICS:\n";
+ CodeGen::PrintStats(Builder);
+ ASTStreamer_PrintStats(Streamer);
+ Context.PrintStats();
+ }
+
+ CodeGen::Terminate(Builder);
+ ASTStreamer_Terminate(Streamer);
+
+ // Print the generated code.
+ M.print(std::cout);
+}
+
diff --git a/Driver/Makefile b/Driver/Makefile
new file mode 100644
index 0000000..4c9db0d
--- /dev/null
+++ b/Driver/Makefile
@@ -0,0 +1,8 @@
+LEVEL = ../../..
+CPPFLAGS += -I$(PROJ_SRC_DIR)/../include
+CXXFLAGS = -fno-rtti
+
+TOOLNAME = clang
+USEDLIBS = clangCodeGen.a clangSEMA.a clangAST.a clangParse.a clangLex.a clangBasic.a LLVMCore.a LLVMSupport.a LLVMSystem.a
+
+include $(LEVEL)/Makefile.common
diff --git a/Driver/PPCBuiltins.def b/Driver/PPCBuiltins.def
new file mode 100644
index 0000000..6aed2ca
--- /dev/null
+++ b/Driver/PPCBuiltins.def
@@ -0,0 +1,24 @@
+//===--- PPCBuiltins.def - PowerPC Builtin function database ----*- C++ -*-===//
+//
+// 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 file defines the PowerPC-specific builtin function database. Users of
+// this file must define the BUILTIN macro to make use of this information.
+//
+//===----------------------------------------------------------------------===//
+
+// FIXME: this needs to be the full list supported by GCC. Right now, I'm just
+// adding stuff on demand.
+
+// The format of this database matches clang/AST/Builtins.def.
+
+// This is just a placeholder, the types and attributes are wrong.
+BUILTIN(__builtin_altivec_abs_v4sf , "ii" , "nc")
+// FIXME: Obviously incomplete.
+
+#undef BUILTIN
diff --git a/Driver/PrintParserCallbacks.cpp b/Driver/PrintParserCallbacks.cpp
new file mode 100644
index 0000000..3730d19
--- /dev/null
+++ b/Driver/PrintParserCallbacks.cpp
@@ -0,0 +1,55 @@
+//===--- PrintParserActions.cpp - Implement -parse-print-callbacks mode ---===//
+//
+// 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 code simply runs the preprocessor on the input file and prints out the
+// result. This is the traditional behavior of the -E option.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Lex/IdentifierTable.h"
+#include "clang/Parse/Action.h"
+#include "clang/Parse/DeclSpec.h"
+#include <iostream>
+using namespace clang;
+
+namespace {
+ class ParserPrintActions : public MinimalAction {
+
+ /// ParseDeclarator - This callback is invoked when a declarator is parsed
+ /// and 'Init' specifies the initializer if any. This is for things like:
+ /// "int X = 4" or "typedef int foo".
+ virtual DeclTy *ParseDeclarator(Scope *S, Declarator &D, ExprTy *Init,
+ DeclTy *LastInGroup) {
+ std::cout << "ParseDeclarator ";
+ if (IdentifierInfo *II = D.getIdentifier()) {
+ std::cout << "'" << II->getName() << "'";
+ } else {
+ std::cout << "<anon>";
+ }
+ std::cout << "\n";
+
+ // Pass up to EmptyActions so that the symbol table is maintained right.
+ return MinimalAction::ParseDeclarator(S, D, Init, LastInGroup);
+ }
+
+ /// PopScope - This callback is called immediately before the specified scope
+ /// is popped and deleted.
+ virtual void PopScope(SourceLocation Loc, Scope *S) {
+ std::cout << "PopScope\n";
+
+ // Pass up to EmptyActions so that the symbol table is maintained right.
+ MinimalAction::PopScope(Loc, S);
+ }
+ };
+}
+
+MinimalAction *clang::CreatePrintParserActionsAction() {
+ return new ParserPrintActions();
+}
diff --git a/Driver/PrintPreprocessedOutput.cpp b/Driver/PrintPreprocessedOutput.cpp
new file mode 100644
index 0000000..4a4f678
--- /dev/null
+++ b/Driver/PrintPreprocessedOutput.cpp
@@ -0,0 +1,436 @@
+//===--- PrintPreprocessedOutput.cpp - Implement the -E mode --------------===//
+//
+// 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 code simply runs the preprocessor on the input file and prints out the
+// result. This is the traditional behavior of the -E option.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/Lex/PPCallbacks.h"
+#include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/Pragma.h"
+#include "clang/Basic/SourceManager.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/Config/config.h"
+#include <cstdio>
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Simple buffered I/O
+//===----------------------------------------------------------------------===//
+//
+// Empirically, iostream is over 30% slower than stdio for this workload, and
+// stdio itself isn't very well suited. The problem with stdio is use of
+// putchar_unlocked. We have many newline characters that need to be emitted,
+// but stdio needs to do extra checks to handle line buffering mode. These
+// extra checks make putchar_unlocked fall off its inlined code path, hitting
+// slow system code. In practice, using 'write' directly makes 'clang -E -P'
+// about 10% faster than using the stdio path on darwin.
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#else
+#define USE_STDIO 1
+#endif
+
+static char *OutBufStart = 0, *OutBufEnd, *OutBufCur;
+
+/// InitOutputBuffer - Initialize our output buffer.
+///
+static void InitOutputBuffer() {
+#ifndef USE_STDIO
+ OutBufStart = new char[64*1024];
+ OutBufEnd = OutBufStart+64*1024;
+ OutBufCur = OutBufStart;
+#endif
+}
+
+/// FlushBuffer - Write the accumulated bytes to the output stream.
+///
+static void FlushBuffer() {
+#ifndef USE_STDIO
+ write(STDOUT_FILENO, OutBufStart, OutBufCur-OutBufStart);
+ OutBufCur = OutBufStart;
+#endif
+}
+
+/// CleanupOutputBuffer - Finish up output.
+///
+static void CleanupOutputBuffer() {
+#ifndef USE_STDIO
+ FlushBuffer();
+ delete [] OutBufStart;
+#endif
+}
+
+static void OutputChar(char c) {
+#ifdef USE_STDIO
+ putchar_unlocked(c);
+#else
+ if (OutBufCur >= OutBufEnd)
+ FlushBuffer();
+ *OutBufCur++ = c;
+#endif
+}
+
+static void OutputString(const char *Ptr, unsigned Size) {
+#ifdef USE_STDIO
+ fwrite(Ptr, Size, 1, stdout);
+#else
+ if (OutBufCur+Size >= OutBufEnd)
+ FlushBuffer();
+ memcpy(OutBufCur, Ptr, Size);
+ OutBufCur += Size;
+#endif
+}
+
+
+//===----------------------------------------------------------------------===//
+// Preprocessed token printer
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::opt<bool>
+DisableLineMarkers("P", llvm::cl::desc("Disable linemarker output in -E mode"));
+static llvm::cl::opt<bool>
+EnableCommentOutput("C", llvm::cl::desc("Enable comment output in -E mode"));
+static llvm::cl::opt<bool>
+EnableMacroCommentOutput("CC",
+ llvm::cl::desc("Enable comment output in -E mode, "
+ "even from macro expansions"));
+
+namespace {
+class PrintPPOutputPPCallbacks : public PPCallbacks {
+ Preprocessor &PP;
+ unsigned CurLine;
+ std::string CurFilename;
+ bool EmittedTokensOnThisLine;
+ DirectoryLookup::DirType FileType;
+public:
+ PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) {
+ CurLine = 0;
+ CurFilename = "\"<uninit>\"";
+ EmittedTokensOnThisLine = false;
+ FileType = DirectoryLookup::NormalHeaderDir;
+ }
+
+ void SetEmittedTokensOnThisLine() { EmittedTokensOnThisLine = true; }
+
+ virtual void FileChanged(SourceLocation Loc, FileChangeReason Reason,
+ DirectoryLookup::DirType FileType);
+ virtual void Ident(SourceLocation Loc, const std::string &str);
+
+
+ void HandleFirstTokOnLine(LexerToken &Tok);
+ void MoveToLine(SourceLocation Loc);
+ bool AvoidConcat(const LexerToken &PrevTok, const LexerToken &Tok);
+};
+}
+
+/// 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
+/// #line directive.
+void PrintPPOutputPPCallbacks::MoveToLine(SourceLocation Loc) {
+ if (DisableLineMarkers) {
+ if (EmittedTokensOnThisLine) {
+ OutputChar('\n');
+ EmittedTokensOnThisLine = false;
+ }
+ return;
+ }
+
+ unsigned LineNo = PP.getSourceManager().getLineNumber(Loc);
+
+ // If this line is "close enough" to the original line, just print newlines,
+ // otherwise print a #line directive.
+ if (LineNo-CurLine < 8) {
+ unsigned Line = CurLine;
+ for (; Line != LineNo; ++Line)
+ OutputChar('\n');
+ CurLine = Line;
+ } else {
+ if (EmittedTokensOnThisLine) {
+ OutputChar('\n');
+ EmittedTokensOnThisLine = false;
+ }
+
+ CurLine = LineNo;
+
+ OutputChar('#');
+ OutputChar(' ');
+ std::string Num = llvm::utostr_32(LineNo);
+ OutputString(&Num[0], Num.size());
+ OutputChar(' ');
+ OutputString(&CurFilename[0], CurFilename.size());
+
+ if (FileType == DirectoryLookup::SystemHeaderDir)
+ OutputString(" 3", 2);
+ else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
+ OutputString(" 3 4", 4);
+ OutputChar('\n');
+ }
+}
+
+
+/// 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,
+ DirectoryLookup::DirType FileType) {
+ if (DisableLineMarkers) return;
+
+ // Unless we are exiting a #include, make sure to skip ahead to the line the
+ // #include directive was at.
+ SourceManager &SourceMgr = PP.getSourceManager();
+ if (Reason == PPCallbacks::EnterFile) {
+ MoveToLine(SourceMgr.getIncludeLoc(Loc.getFileID()));
+ } else if (Reason == PPCallbacks::SystemHeaderPragma) {
+ MoveToLine(Loc);
+
+ // TODO GCC emits the # directive for this directive on the line AFTER the
+ // directive and emits a bunch of spaces that aren't needed. Emulate this
+ // strange behavior.
+ }
+
+ CurLine = SourceMgr.getLineNumber(Loc);
+ CurFilename = '"' + Lexer::Stringify(SourceMgr.getSourceName(Loc)) + '"';
+ FileType = FileType;
+
+ if (EmittedTokensOnThisLine) {
+ OutputChar('\n');
+ EmittedTokensOnThisLine = false;
+ }
+
+ if (DisableLineMarkers) return;
+
+ OutputChar('#');
+ OutputChar(' ');
+ std::string Num = llvm::utostr_32(CurLine);
+ OutputString(&Num[0], Num.size());
+ OutputChar(' ');
+ OutputString(&CurFilename[0], CurFilename.size());
+
+ switch (Reason) {
+ case PPCallbacks::EnterFile:
+ OutputString(" 1", 2);
+ break;
+ case PPCallbacks::ExitFile:
+ OutputString(" 2", 2);
+ break;
+ case PPCallbacks::SystemHeaderPragma: break;
+ case PPCallbacks::RenameFile: break;
+ }
+
+ if (FileType == DirectoryLookup::SystemHeaderDir)
+ OutputString(" 3", 2);
+ else if (FileType == DirectoryLookup::ExternCSystemHeaderDir)
+ OutputString(" 3 4", 4);
+
+ OutputChar('\n');
+}
+
+/// HandleIdent - Handle #ident directives when read by the preprocessor.
+///
+void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
+ MoveToLine(Loc);
+
+ OutputString("#ident ", strlen("#ident "));
+ OutputString(&S[0], S.size());
+ EmittedTokensOnThisLine = true;
+}
+
+/// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this
+/// is called for the first token on each new line.
+void PrintPPOutputPPCallbacks::HandleFirstTokOnLine(LexerToken &Tok) {
+ // Figure out what line we went to and insert the appropriate number of
+ // newline characters.
+ MoveToLine(Tok.getLocation());
+
+ // Print out space characters so that the first token on a line is
+ // indented for easy reading.
+ unsigned ColNo =
+ PP.getSourceManager().getColumnNumber(Tok.getLocation());
+
+ // 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.
+ if (ColNo <= 1 && Tok.getKind() == tok::hash)
+ OutputChar(' ');
+
+ // Otherwise, indent the appropriate number of spaces.
+ for (; ColNo > 1; --ColNo)
+ OutputChar(' ');
+}
+
+namespace {
+struct UnknownPragmaHandler : public PragmaHandler {
+ const char *Prefix;
+ PrintPPOutputPPCallbacks *Callbacks;
+
+ UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks)
+ : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {}
+ virtual void HandlePragma(Preprocessor &PP, LexerToken &PragmaTok) {
+ // Figure out what line we went to and insert the appropriate number of
+ // newline characters.
+ Callbacks->MoveToLine(PragmaTok.getLocation());
+ OutputString(Prefix, strlen(Prefix));
+
+ // Read and print all of the pragma tokens.
+ while (PragmaTok.getKind() != tok::eom) {
+ if (PragmaTok.hasLeadingSpace())
+ OutputChar(' ');
+ std::string TokSpell = PP.getSpelling(PragmaTok);
+ OutputString(&TokSpell[0], TokSpell.size());
+ PP.LexUnexpandedToken(PragmaTok);
+ }
+ OutputChar('\n');
+ }
+};
+} // end anonymous namespace
+
+/// AvoidConcat - If printing PrevTok immediately followed by Tok would cause
+/// the two individual tokens to be lexed as a single token, return true (which
+/// causes a space to be printed between them). This allows the output of -E
+/// mode to be lexed to the same token stream as lexing the input directly
+/// would.
+///
+/// This code must conservatively return true if it doesn't want to be 100%
+/// accurate. This will cause the output to include extra space characters, but
+/// the resulting output won't have incorrect concatenations going on. Examples
+/// include "..", which we print with a space between, because we don't want to
+/// track enough to tell "x.." from "...".
+bool PrintPPOutputPPCallbacks::AvoidConcat(const LexerToken &PrevTok,
+ const LexerToken &Tok) {
+ char Buffer[256];
+
+ // If we haven't emitted a token on this line yet, PrevTok isn't useful to
+ // look at and no concatenation could happen anyway.
+ if (!EmittedTokensOnThisLine)
+ return false;
+
+ // Basic algorithm: we look at the first character of the second token, and
+ // determine whether it, if appended to the first token, would form (or would
+ // contribute) to a larger token if concatenated.
+ char FirstChar;
+ if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
+ // Avoid spelling identifiers, the most common form of token.
+ FirstChar = II->getName()[0];
+ } else if (Tok.getLength() < 256) {
+ const char *TokPtr = Buffer;
+ PP.getSpelling(Tok, TokPtr);
+ FirstChar = TokPtr[0];
+ } else {
+ FirstChar = PP.getSpelling(Tok)[0];
+ }
+
+ tok::TokenKind PrevKind = PrevTok.getKind();
+ if (PrevTok.getIdentifierInfo()) // Language keyword or named operator.
+ PrevKind = tok::identifier;
+
+ switch (PrevKind) {
+ default: return false;
+ case tok::identifier: // id+id or id+number or id+L"foo".
+ return isalnum(FirstChar) || FirstChar == '_';
+ case tok::numeric_constant:
+ return isalnum(FirstChar) || Tok.getKind() == tok::numeric_constant ||
+ FirstChar == '+' || FirstChar == '-' || FirstChar == '.';
+ case tok::period: // ..., .*, .1234
+ return FirstChar == '.' || FirstChar == '*' || isdigit(FirstChar);
+ case tok::amp: // &&, &=
+ return FirstChar == '&' || FirstChar == '=';
+ case tok::plus: // ++, +=
+ return FirstChar == '+' || FirstChar == '=';
+ case tok::minus: // --, ->, -=, ->*
+ return FirstChar == '-' || FirstChar == '>' || FirstChar == '=';
+ case tok::slash: // /=, /*, //
+ return FirstChar == '=' || FirstChar == '*' || FirstChar == '/';
+ case tok::less: // <<, <<=, <=, <?=, <?, <:, <%
+ return FirstChar == '<' || FirstChar == '?' || FirstChar == '=' ||
+ FirstChar == ':' || FirstChar == '%';
+ case tok::greater: // >>, >=, >>=, >?=, >?, ->*
+ return FirstChar == '>' || FirstChar == '?' || FirstChar == '=' ||
+ FirstChar == '*';
+ case tok::pipe: // ||, |=
+ return FirstChar == '|' || FirstChar == '=';
+ case tok::percent: // %=, %>, %:
+ return FirstChar == '=' || FirstChar == '>' || FirstChar == ':';
+ case tok::colon: // ::, :>
+ return FirstChar == ':' || FirstChar == '>';
+ case tok::hash: // ##, #@, %:%:
+ return FirstChar == '#' || FirstChar == '@' || FirstChar == '%';
+ case tok::arrow: // ->*
+ return FirstChar == '*';
+
+ case tok::star: // *=
+ case tok::exclaim: // !=
+ case tok::lessless: // <<=
+ case tok::greaterequal: // >>=
+ case tok::caret: // ^=
+ case tok::equal: // ==
+ // Cases that concatenate only if the next char is =.
+ return FirstChar == '=';
+ }
+}
+
+/// DoPrintPreprocessedInput - This implements -E mode.
+///
+void clang::DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
+ const LangOptions &Options) {
+ // Inform the preprocessor whether we want it to retain comments or not, due
+ // to -C or -CC.
+ PP.SetCommentRetentionState(EnableCommentOutput, EnableMacroCommentOutput);
+
+ InitOutputBuffer();
+
+ LexerToken Tok, PrevTok;
+ char Buffer[256];
+ PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP);
+ PP.setPPCallbacks(Callbacks);
+
+ PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks));
+ PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks));
+
+ // After we have configured the preprocessor, enter the main file.
+
+ // Start parsing the specified input file.
+ PP.EnterSourceFile(MainFileID, 0, true);
+
+ do {
+ PrevTok = Tok;
+ PP.Lex(Tok);
+
+ // If this token is at the start of a line, emit newlines if needed.
+ if (Tok.isAtStartOfLine()) {
+ Callbacks->HandleFirstTokOnLine(Tok);
+ } else if (Tok.hasLeadingSpace() ||
+ // Don't print "-" next to "-", it would form "--".
+ Callbacks->AvoidConcat(PrevTok, Tok)) {
+ OutputChar(' ');
+ }
+
+ if (Tok.getLength() < 256) {
+ const char *TokPtr = Buffer;
+ unsigned Len = PP.getSpelling(Tok, TokPtr);
+ OutputString(TokPtr, Len);
+ } else {
+ std::string S = PP.getSpelling(Tok);
+ OutputString(&S[0], S.size());
+ }
+ Callbacks->SetEmittedTokensOnThisLine();
+ } while (Tok.getKind() != tok::eof);
+ OutputChar('\n');
+
+ CleanupOutputBuffer();
+}
+
diff --git a/Driver/Targets.cpp b/Driver/Targets.cpp
new file mode 100644
index 0000000..168084d
--- /dev/null
+++ b/Driver/Targets.cpp
@@ -0,0 +1,443 @@
+//===--- Targets.cpp - Implement -arch option and targets -----------------===//
+//
+// 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 file implements the -arch command line option and creates a TargetInfo
+// that represents them.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "clang/AST/Builtins.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/TargetInfo.h"
+#include "llvm/Support/CommandLine.h"
+using namespace clang;
+
+/// Note: a hard coded list of targets is clearly silly, these should be
+/// dynamicly registered and loadable with "-load".
+enum SupportedTargets {
+ target_ppc, target_ppc64,
+ target_i386, target_x86_64,
+ target_linux_i386
+};
+
+static llvm::cl::list<SupportedTargets>
+Archs("arch", llvm::cl::desc("Architectures to compile for"),
+llvm::cl::values(clEnumValN(target_ppc, "ppc", "32-bit Darwin PowerPC"),
+ clEnumValN(target_ppc64, "ppc64", "64-bit Darwin PowerPC"),
+ clEnumValN(target_i386, "i386", "32-bit Darwin X86"),
+ clEnumValN(target_x86_64, "x86_64","64-bit Darwin X86"),
+ clEnumValN(target_linux_i386,"linux", "Linux i386"),
+ clEnumValEnd));
+
+//===----------------------------------------------------------------------===//
+// Common code shared among targets.
+//===----------------------------------------------------------------------===//
+
+namespace {
+class DarwinTargetInfo : public TargetInfoImpl {
+public:
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ Defines.push_back("__APPLE__=1");
+ Defines.push_back("__MACH__=1");
+
+ if (1) {// -fobjc-gc controls this.
+ Defines.push_back("__weak=");
+ Defines.push_back("__strong=");
+ } else {
+ Defines.push_back("__weak=__attribute__((objc_gc(weak)))");
+ Defines.push_back("__strong=__attribute__((objc_gc(strong)))");
+ Defines.push_back("__OBJC_GC__");
+ }
+
+ // darwin_constant_cfstrings controls this.
+ Defines.push_back("__CONSTANT_CFSTRINGS__=1");
+
+ if (0) // darwin_pascal_strings
+ Defines.push_back("__PASCAL_STRINGS__");
+ }
+
+};
+} // end anonymous namespace.
+
+
+/// getPowerPCDefines - Return a set of the PowerPC-specific #defines that are
+/// not tied to a specific subtarget.
+static void getPowerPCDefines(std::vector<std::string> &Defines, bool is64Bit) {
+ // Target identification.
+ Defines.push_back("__ppc__");
+ Defines.push_back("_ARCH_PPC=1");
+ Defines.push_back("__POWERPC__=1");
+ if (is64Bit) {
+ Defines.push_back("_ARCH_PPC64");
+ Defines.push_back("_LP64");
+ Defines.push_back("__LP64__");
+ Defines.push_back("__ppc64__");
+ } else {
+ Defines.push_back("__ppc__=1");
+ }
+
+ // Target properties.
+ Defines.push_back("_BIG_ENDIAN=1");
+ Defines.push_back("__BIG_ENDIAN__=1");
+
+ if (is64Bit) {
+ Defines.push_back("__INTMAX_MAX__=9223372036854775807L");
+ Defines.push_back("__INTMAX_TYPE__=long int");
+ Defines.push_back("__LONG_MAX__=9223372036854775807L");
+ Defines.push_back("__PTRDIFF_TYPE__=long int");
+ Defines.push_back("__UINTMAX_TYPE__=long unsigned int");
+ } else {
+ Defines.push_back("__INTMAX_MAX__=9223372036854775807LL");
+ Defines.push_back("__INTMAX_TYPE__=long long int");
+ Defines.push_back("__LONG_MAX__=2147483647L");
+ Defines.push_back("__PTRDIFF_TYPE__=int");
+ Defines.push_back("__UINTMAX_TYPE__=long long unsigned int");
+ }
+ Defines.push_back("__INT_MAX__=2147483647");
+ Defines.push_back("__LONG_LONG_MAX__=9223372036854775807LL");
+ Defines.push_back("__CHAR_BIT__=8");
+ Defines.push_back("__SCHAR_MAX__=127");
+ Defines.push_back("__SHRT_MAX__=32767");
+ Defines.push_back("__SIZE_TYPE__=long unsigned int");
+
+ // Subtarget options.
+ Defines.push_back("__USER_LABEL_PREFIX__=_");
+ Defines.push_back("__NATURAL_ALIGNMENT__=1");
+ Defines.push_back("__REGISTER_PREFIX__=");
+
+ Defines.push_back("__WCHAR_MAX__=2147483647");
+ Defines.push_back("__WCHAR_TYPE__=int");
+ Defines.push_back("__WINT_TYPE__=int");
+
+ // Float macros.
+ Defines.push_back("__FLT_DENORM_MIN__=1.40129846e-45F");
+ Defines.push_back("__FLT_DIG__=6");
+ Defines.push_back("__FLT_EPSILON__=1.19209290e-7F");
+ Defines.push_back("__FLT_EVAL_METHOD__=0");
+ Defines.push_back("__FLT_HAS_INFINITY__=1");
+ Defines.push_back("__FLT_HAS_QUIET_NAN__=1");
+ Defines.push_back("__FLT_MANT_DIG__=24");
+ Defines.push_back("__FLT_MAX_10_EXP__=38");
+ Defines.push_back("__FLT_MAX_EXP__=128");
+ Defines.push_back("__FLT_MAX__=3.40282347e+38F");
+ Defines.push_back("__FLT_MIN_10_EXP__=(-37)");
+ Defines.push_back("__FLT_MIN_EXP__=(-125)");
+ Defines.push_back("__FLT_MIN__=1.17549435e-38F");
+ Defines.push_back("__FLT_RADIX__=2");
+
+ // double macros.
+ Defines.push_back("__DBL_DENORM_MIN__=4.9406564584124654e-324");
+ Defines.push_back("__DBL_DIG__=15");
+ Defines.push_back("__DBL_EPSILON__=2.2204460492503131e-16");
+ Defines.push_back("__DBL_HAS_INFINITY__=1");
+ Defines.push_back("__DBL_HAS_QUIET_NAN__=1");
+ Defines.push_back("__DBL_MANT_DIG__=53");
+ Defines.push_back("__DBL_MAX_10_EXP__=308");
+ Defines.push_back("__DBL_MAX_EXP__=1024");
+ Defines.push_back("__DBL_MAX__=1.7976931348623157e+308");
+ Defines.push_back("__DBL_MIN_10_EXP__=(-307)");
+ Defines.push_back("__DBL_MIN_EXP__=(-1021)");
+ Defines.push_back("__DBL_MIN__=2.2250738585072014e-308");
+ Defines.push_back("__DECIMAL_DIG__=33");
+
+ // 128-bit long double macros.
+ Defines.push_back("__LDBL_DENORM_MIN__=4.940656458412465441765687"
+ "92868221e-324L");
+ Defines.push_back("__LDBL_DIG__=31");
+ Defines.push_back("__LDBL_EPSILON__=4.9406564584124654417656879286822"
+ "1e-324L");
+ Defines.push_back("__LDBL_HAS_INFINITY__=1");
+ Defines.push_back("__LDBL_HAS_QUIET_NAN__=1");
+ Defines.push_back("__LDBL_MANT_DIG__=106");
+ Defines.push_back("__LDBL_MAX_10_EXP__=308");
+ Defines.push_back("__LDBL_MAX_EXP__=1024");
+ Defines.push_back("__LDBL_MAX__=1.7976931348623158079372897140"
+ "5301e+308L");
+ Defines.push_back("__LDBL_MIN_10_EXP__=(-291)");
+ Defines.push_back("__LDBL_MIN_EXP__=(-968)");
+ Defines.push_back("__LDBL_MIN__=2.004168360008972777996108051350"
+ "16e-292L");
+ Defines.push_back("__LONG_DOUBLE_128__=1");
+
+}
+
+/// getX86Defines - Return a set of the X86-specific #defines that are
+/// not tied to a specific subtarget.
+static void getX86Defines(std::vector<std::string> &Defines, bool is64Bit) {
+ // Target identification.
+ if (is64Bit) {
+ Defines.push_back("_LP64");
+ Defines.push_back("__LP64__");
+ Defines.push_back("__amd64__");
+ Defines.push_back("__amd64");
+ Defines.push_back("__x86_64");
+ Defines.push_back("__x86_64__");
+ } else {
+ Defines.push_back("__i386__=1");
+ Defines.push_back("__i386=1");
+ Defines.push_back("i386=1");
+ }
+
+ // Target properties.
+ Defines.push_back("__LITTLE_ENDIAN__=1");
+
+ if (is64Bit) {
+ Defines.push_back("__INTMAX_MAX__=9223372036854775807L");
+ Defines.push_back("__INTMAX_TYPE__=long int");
+ Defines.push_back("__LONG_MAX__=9223372036854775807L");
+ Defines.push_back("__PTRDIFF_TYPE__=long int");
+ Defines.push_back("__UINTMAX_TYPE__=long unsigned int");
+ } else {
+ Defines.push_back("__INTMAX_MAX__=9223372036854775807LL");
+ Defines.push_back("__INTMAX_TYPE__=long long int");
+ Defines.push_back("__LONG_MAX__=2147483647L");
+ Defines.push_back("__PTRDIFF_TYPE__=int");
+ Defines.push_back("__UINTMAX_TYPE__=long long unsigned int");
+ }
+ Defines.push_back("__CHAR_BIT__=8");
+ Defines.push_back("__INT_MAX__=2147483647");
+ Defines.push_back("__LONG_LONG_MAX__=9223372036854775807LL");
+ Defines.push_back("__SCHAR_MAX__=127");
+ Defines.push_back("__SHRT_MAX__=32767");
+ Defines.push_back("__SIZE_TYPE__=long unsigned int");
+
+ // Subtarget options.
+ Defines.push_back("__nocona=1");
+ Defines.push_back("__nocona__=1");
+ Defines.push_back("__tune_nocona__=1");
+ Defines.push_back("__SSE2_MATH__=1");
+ Defines.push_back("__SSE2__=1");
+ Defines.push_back("__SSE_MATH__=1");
+ Defines.push_back("__SSE__=1");
+ Defines.push_back("__MMX__=1");
+ Defines.push_back("__REGISTER_PREFIX__=");
+
+ Defines.push_back("__WCHAR_MAX__=2147483647");
+ Defines.push_back("__WCHAR_TYPE__=int");
+ Defines.push_back("__WINT_TYPE__=int");
+
+ // Float macros.
+ Defines.push_back("__FLT_DENORM_MIN__=1.40129846e-45F");
+ Defines.push_back("__FLT_DIG__=6");
+ Defines.push_back("__FLT_EPSILON__=1.19209290e-7F");
+ Defines.push_back("__FLT_EVAL_METHOD__=0");
+ Defines.push_back("__FLT_HAS_INFINITY__=1");
+ Defines.push_back("__FLT_HAS_QUIET_NAN__=1");
+ Defines.push_back("__FLT_MANT_DIG__=24");
+ Defines.push_back("__FLT_MAX_10_EXP__=38");
+ Defines.push_back("__FLT_MAX_EXP__=128");
+ Defines.push_back("__FLT_MAX__=3.40282347e+38F");
+ Defines.push_back("__FLT_MIN_10_EXP__=(-37)");
+ Defines.push_back("__FLT_MIN_EXP__=(-125)");
+ Defines.push_back("__FLT_MIN__=1.17549435e-38F");
+ Defines.push_back("__FLT_RADIX__=2");
+
+ // Double macros.
+ Defines.push_back("__DBL_DENORM_MIN__=4.9406564584124654e-324");
+ Defines.push_back("__DBL_DIG__=15");
+ Defines.push_back("__DBL_EPSILON__=2.2204460492503131e-16");
+ Defines.push_back("__DBL_HAS_INFINITY__=1");
+ Defines.push_back("__DBL_HAS_QUIET_NAN__=1");
+ Defines.push_back("__DBL_MANT_DIG__=53");
+ Defines.push_back("__DBL_MAX_10_EXP__=308");
+ Defines.push_back("__DBL_MAX_EXP__=1024");
+ Defines.push_back("__DBL_MAX__=1.7976931348623157e+308");
+ Defines.push_back("__DBL_MIN_10_EXP__=(-307)");
+ Defines.push_back("__DBL_MIN_EXP__=(-1021)");
+ Defines.push_back("__DBL_MIN__=2.2250738585072014e-308");
+ Defines.push_back("__DECIMAL_DIG__=21");
+
+ // 80-bit Long double macros.
+ Defines.push_back("__LDBL_DENORM_MIN__=3.64519953188247460253e-4951L");
+ Defines.push_back("__LDBL_DIG__=18");
+ Defines.push_back("__LDBL_EPSILON__=1.08420217248550443401e-19L");
+ Defines.push_back("__LDBL_HAS_INFINITY__=1");
+ Defines.push_back("__LDBL_HAS_QUIET_NAN__=1");
+ Defines.push_back("__LDBL_MANT_DIG__=64");
+ Defines.push_back("__LDBL_MAX_10_EXP__=4932");
+ Defines.push_back("__LDBL_MAX_EXP__=16384");
+ Defines.push_back("__LDBL_MAX__=1.18973149535723176502e+4932L");
+ Defines.push_back("__LDBL_MIN_10_EXP__=(-4931)");
+ Defines.push_back("__LDBL_MIN_EXP__=(-16381)");
+ Defines.push_back("__LDBL_MIN__=3.36210314311209350626e-4932L");
+
+}
+
+/// PPC builtin info.
+namespace PPC {
+ enum {
+ LastTIBuiltin = Builtin::FirstTSBuiltin-1,
+#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
+#include "PPCBuiltins.def"
+ LastTSBuiltin
+ };
+
+ static const Builtin::Info BuiltinInfo[] = {
+#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS },
+#include "PPCBuiltins.def"
+ };
+
+ static void getBuiltins(const Builtin::Info *&Records, unsigned &NumRecords) {
+ Records = BuiltinInfo;
+ NumRecords = LastTSBuiltin-Builtin::FirstTSBuiltin;
+ }
+} // End namespace PPC
+
+
+/// X86 builtin info.
+namespace X86 {
+ enum {
+ LastTIBuiltin = Builtin::FirstTSBuiltin-1,
+#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
+#include "X86Builtins.def"
+ LastTSBuiltin
+ };
+
+ static const Builtin::Info BuiltinInfo[] = {
+#define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS },
+#include "X86Builtins.def"
+ };
+
+ static void getBuiltins(const Builtin::Info *&Records, unsigned &NumRecords) {
+ Records = BuiltinInfo;
+ NumRecords = LastTSBuiltin-Builtin::FirstTSBuiltin;
+ }
+} // End namespace X86
+
+//===----------------------------------------------------------------------===//
+// Specific target implementations.
+//===----------------------------------------------------------------------===//
+
+
+namespace {
+class DarwinPPCTargetInfo : public DarwinTargetInfo {
+public:
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ DarwinTargetInfo::getTargetDefines(Defines);
+ getPowerPCDefines(Defines, false);
+ }
+ virtual void getTargetBuiltins(const Builtin::Info *&Records,
+ unsigned &NumRecords) const {
+ PPC::getBuiltins(Records, NumRecords);
+ }
+};
+} // end anonymous namespace.
+
+namespace {
+class DarwinPPC64TargetInfo : public DarwinTargetInfo {
+public:
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ DarwinTargetInfo::getTargetDefines(Defines);
+ getPowerPCDefines(Defines, true);
+ }
+ virtual void getTargetBuiltins(const Builtin::Info *&Records,
+ unsigned &NumRecords) const {
+ PPC::getBuiltins(Records, NumRecords);
+ }
+};
+} // end anonymous namespace.
+
+namespace {
+class DarwinI386TargetInfo : public DarwinTargetInfo {
+public:
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ DarwinTargetInfo::getTargetDefines(Defines);
+ getX86Defines(Defines, false);
+ }
+ virtual void getTargetBuiltins(const Builtin::Info *&Records,
+ unsigned &NumRecords) const {
+ X86::getBuiltins(Records, NumRecords);
+ }
+};
+} // end anonymous namespace.
+
+namespace {
+class DarwinX86_64TargetInfo : public DarwinTargetInfo {
+public:
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ DarwinTargetInfo::getTargetDefines(Defines);
+ getX86Defines(Defines, true);
+ }
+ virtual void getTargetBuiltins(const Builtin::Info *&Records,
+ unsigned &NumRecords) const {
+ X86::getBuiltins(Records, NumRecords);
+ }
+};
+} // end anonymous namespace.
+
+namespace {
+class LinuxTargetInfo : public DarwinTargetInfo {
+public:
+ LinuxTargetInfo() {
+ // Note: I have no idea if this is right, just for testing.
+ WCharWidth = 16;
+ }
+
+ virtual void getTargetDefines(std::vector<std::string> &Defines) const {
+ // TODO: linux-specific stuff.
+ getX86Defines(Defines, false);
+ }
+ virtual void getTargetBuiltins(const Builtin::Info *&Records,
+ unsigned &NumRecords) const {
+ X86::getBuiltins(Records, NumRecords);
+ }
+};
+} // end anonymous namespace.
+
+
+//===----------------------------------------------------------------------===//
+// Driver code
+//===----------------------------------------------------------------------===//
+
+/// CreateTarget - Create the TargetInfoImpl object for the specified target
+/// enum value.
+static TargetInfoImpl *CreateTarget(SupportedTargets T) {
+ switch (T) {
+ default: assert(0 && "Unknown target!");
+ case target_ppc: return new DarwinPPCTargetInfo();
+ case target_ppc64: return new DarwinPPC64TargetInfo();
+ case target_i386: return new DarwinI386TargetInfo();
+ case target_x86_64: return new DarwinX86_64TargetInfo();
+ case target_linux_i386: return new LinuxTargetInfo();
+ }
+}
+
+/// CreateTargetInfo - Return the set of target info objects as specified by
+/// the -arch command line option.
+TargetInfo *clang::CreateTargetInfo(Diagnostic &Diags) {
+ // If the user didn't specify at least one architecture, auto-sense the
+ // current host. TODO: This is a hack. :)
+ if (Archs.empty()) {
+#ifndef __APPLE__
+ // Assume non-apple = linux.
+ Archs.push_back(target_linux_i386);
+#elif (defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)) && \
+ defined(__ppc64__)
+ Archs.push_back(target_ppc64);
+#elif defined(__POWERPC__) || defined (__ppc__) || defined(_POWER)
+ Archs.push_back(target_ppc);
+#elif defined(__x86_64__)
+ Archs.push_back(target_x86_64);
+#elif defined(__i386__) || defined(i386) || defined(_M_IX86)
+ Archs.push_back(target_i386);
+#else
+ // Don't know what this is!
+ return 0;
+#endif
+ }
+
+ // Create the primary target and target info.
+ TargetInfo *TI = new TargetInfo(CreateTarget(Archs[0]), &Diags);
+
+ // Add all secondary targets.
+ for (unsigned i = 1, e = Archs.size(); i != e; ++i)
+ TI->AddSecondaryTarget(CreateTarget(Archs[i]));
+ return TI;
+}
diff --git a/Driver/TextDiagnosticBuffer.cpp b/Driver/TextDiagnosticBuffer.cpp
new file mode 100644
index 0000000..1f6075e
--- /dev/null
+++ b/Driver/TextDiagnosticBuffer.cpp
@@ -0,0 +1,38 @@
+//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a concrete diagnostic client, which buffers the diagnostic messages.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TextDiagnosticBuffer.h"
+#include "clang/Basic/SourceManager.h"
+using namespace clang;
+
+/// HandleDiagnostic - Store the errors & warnings that are reported.
+///
+void TextDiagnosticBuffer::HandleDiagnostic(Diagnostic::Level Level,
+ SourceLocation Pos,
+ diag::kind ID,
+ const std::string *Strs,
+ unsigned NumStrs,
+ const SourceRange *,
+ unsigned) {
+ switch (Level) {
+ default: assert(0 && "Diagnostic not handled during diagnostic buffering!");
+ case Diagnostic::Warning:
+ Warnings.push_back(std::make_pair(Pos, FormatDiagnostic(Level, ID, Strs,
+ NumStrs)));
+ break;
+ case Diagnostic::Error:
+ Errors.push_back(std::make_pair(Pos, FormatDiagnostic(Level, ID, Strs,
+ NumStrs)));
+ break;
+ }
+}
diff --git a/Driver/TextDiagnosticBuffer.h b/Driver/TextDiagnosticBuffer.h
new file mode 100644
index 0000000..34fbc6e
--- /dev/null
+++ b/Driver/TextDiagnosticBuffer.h
@@ -0,0 +1,51 @@
+//===--- TextDiagnosticBuffer.h - Buffer Text Diagnostics -------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a concrete diagnostic client, which buffers the diagnostic messages.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef DRIVER_TEXT_DIAGNOSTIC_BUFFER_H_
+#define DRIVER_TEXT_DIAGNOSTIC_BUFFER_H_
+
+#include "TextDiagnostics.h"
+#include <vector>
+
+namespace clang {
+
+class Preprocessor;
+class SourceManager;
+
+class TextDiagnosticBuffer : public TextDiagnostics {
+public:
+ typedef std::vector<std::pair<SourceLocation, std::string> > DiagList;
+ typedef DiagList::iterator iterator;
+ typedef DiagList::const_iterator const_iterator;
+private:
+ DiagList Errors, Warnings;
+public:
+ TextDiagnosticBuffer(SourceManager &SM) : TextDiagnostics(SM) {}
+
+ const_iterator err_begin() const { return Errors.begin(); }
+ const_iterator err_end() const { return Errors.end(); }
+
+ const_iterator warn_begin() const { return Warnings.begin(); }
+ const_iterator warn_end() const { return Warnings.end(); }
+
+ virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+ SourceLocation Pos,
+ diag::kind ID, const std::string *Strs,
+ unsigned NumStrs,
+ const SourceRange *Ranges,
+ unsigned NumRanges);
+};
+
+} // end namspace clang
+
+#endif
diff --git a/Driver/TextDiagnosticPrinter.cpp b/Driver/TextDiagnosticPrinter.cpp
new file mode 100644
index 0000000..1acd210
--- /dev/null
+++ b/Driver/TextDiagnosticPrinter.cpp
@@ -0,0 +1,225 @@
+//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This diagnostic client prints out their diagnostic messages.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TextDiagnosticPrinter.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/HeaderSearch.h"
+#include "clang/Lex/Lexer.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include <iostream>
+#include <string>
+using namespace clang;
+
+static llvm::cl::opt<bool>
+NoShowColumn("fno-show-column",
+ llvm::cl::desc("Do not include column number on diagnostics"));
+static llvm::cl::opt<bool>
+NoCaretDiagnostics("fno-caret-diagnostics",
+ llvm::cl::desc("Do not include source line and caret with"
+ " diagnostics"));
+
+void TextDiagnosticPrinter::
+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 llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(FileID);
+ std::cerr << "In file included from " << Buffer->getBufferIdentifier()
+ << ":" << LineNo << ":\n";
+}
+
+/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
+/// any characters in LineNo that intersect the SourceRange.
+void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
+ unsigned LineNo,
+ std::string &CaratLine,
+ const std::string &SourceLine) {
+ assert(CaratLine.size() == SourceLine.size() &&
+ "Expect a correspondence between source and carat line!");
+ if (!R.isValid()) return;
+
+ unsigned StartLineNo = SourceMgr.getLineNumber(R.Begin());
+ if (StartLineNo > LineNo) return; // No intersection.
+
+ unsigned EndLineNo = SourceMgr.getLineNumber(R.End());
+ if (EndLineNo < LineNo) return; // No intersection.
+
+ // Compute the column number of the start.
+ unsigned StartColNo = 0;
+ if (StartLineNo == LineNo) {
+ StartColNo = SourceMgr.getColumnNumber(R.Begin());
+ if (StartColNo) --StartColNo; // Zero base the col #.
+ }
+
+ // Pick the first non-whitespace column.
+ while (StartColNo < SourceLine.size() &&
+ (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
+ ++StartColNo;
+
+ // Compute the column number of the end.
+ unsigned EndColNo = CaratLine.size();
+ if (EndLineNo == LineNo) {
+ EndColNo = SourceMgr.getColumnNumber(R.End());
+ if (EndColNo) {
+ --EndColNo; // Zero base the col #.
+
+ // Add in the length of the token, so that we cover multi-char tokens.
+ EndColNo += GetTokenLength(R.End());
+ } else {
+ EndColNo = CaratLine.size();
+ }
+ }
+
+ // Pick the last non-whitespace column.
+ while (EndColNo-1 &&
+ (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
+ --EndColNo;
+
+ // Fill the range with ~'s.
+ assert(StartColNo <= EndColNo && "Invalid range!");
+ for (unsigned i = StartColNo; i != EndColNo; ++i)
+ CaratLine[i] = '~';
+}
+
+/// GetTokenLength - Given the source location of a token, determine its length.
+/// This is a fully general function that uses a lexer to relex the token.
+unsigned TextDiagnosticPrinter::GetTokenLength(SourceLocation Loc) {
+ const char *StrData =
+ SourceMgr.getCharacterData(SourceMgr.getLogicalLoc(Loc));
+
+ // Note, this could be special cased for common tokens like identifiers, ')',
+ // etc to make this faster, if it mattered.
+
+ unsigned FileID = Loc.getFileID();
+
+ // Create a lexer starting at the beginning of this token.
+ Lexer TheLexer(SourceMgr.getBuffer(FileID), FileID,
+ *ThePreprocessor, StrData);
+
+ LexerToken TheTok;
+ TheLexer.LexRawToken(TheTok);
+
+ return TheTok.getLength();
+}
+
+void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
+ SourceLocation Pos,
+ diag::kind ID,
+ const std::string *Strs,
+ unsigned NumStrs,
+ const SourceRange *Ranges,
+ unsigned NumRanges) {
+ unsigned LineNo = 0, FilePos = 0, FileID = 0, ColNo = 0;
+ unsigned LineStart = 0, LineEnd = 0;
+ const llvm::MemoryBuffer *Buffer = 0;
+
+ if (Pos.isValid()) {
+ LineNo = SourceMgr.getLineNumber(Pos);
+ FileID = SourceMgr.getLogicalLoc(Pos).getFileID();
+
+ // First, if this diagnostic is not in the main file, print out the
+ // "included from" lines.
+ if (LastWarningLoc != SourceMgr.getIncludeLoc(FileID)) {
+ LastWarningLoc = SourceMgr.getIncludeLoc(FileID);
+ PrintIncludeStack(LastWarningLoc);
+ }
+
+ // Compute the column number. Rewind from the current position to the start
+ // of the line.
+ ColNo = SourceMgr.getColumnNumber(Pos);
+ FilePos = SourceMgr.getSourceFilePos(Pos);
+ LineStart = FilePos-ColNo+1; // Column # is 1-based
+
+ // 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::cerr << FormatDiagnostic(Level, ID, Strs, NumStrs) << "\n";
+
+ if (!NoCaretDiagnostics && Pos.isValid()) {
+ // Get the line of the source file.
+ const char *Buf = Buffer->getBufferStart();
+ std::string SourceLine(Buf+LineStart, Buf+LineEnd);
+
+ // Create a line for the carat that is filled with spaces that is the same
+ // length as the line of source code.
+ std::string CaratLine(LineEnd-LineStart, ' ');
+
+ // Highlight all of the characters covered by Ranges with ~ characters.
+ for (unsigned i = 0; i != NumRanges; ++i)
+ HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
+
+ // Next, insert the carat itself.
+ if (ColNo-1 < CaratLine.size())
+ CaratLine[ColNo-1] = '^';
+ else
+ CaratLine.push_back('^');
+
+ // Scan the source line, looking for tabs. If we find any, manually expand
+ // them to 8 characters and update the CaratLine to match.
+ for (unsigned i = 0; i != SourceLine.size(); ++i) {
+ if (SourceLine[i] != '\t') continue;
+
+ // Replace this tab with at least one space.
+ SourceLine[i] = ' ';
+
+ // Compute the number of spaces we need to insert.
+ unsigned NumSpaces = ((i+8)&~7) - (i+1);
+ assert(NumSpaces < 8 && "Invalid computation of space amt");
+
+ // Insert spaces into the SourceLine.
+ SourceLine.insert(i+1, NumSpaces, ' ');
+
+ // Insert spaces or ~'s into CaratLine.
+ CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
+ }
+
+ // Finally, remove any blank spaces from the end of CaratLine.
+ while (CaratLine[CaratLine.size()-1] == ' ')
+ CaratLine.erase(CaratLine.end()-1);
+
+ // Emit what we have computed.
+ std::cerr << SourceLine << "\n";
+ std::cerr << CaratLine << "\n";
+ }
+}
diff --git a/Driver/TextDiagnosticPrinter.h b/Driver/TextDiagnosticPrinter.h
new file mode 100644
index 0000000..71e584eb
--- /dev/null
+++ b/Driver/TextDiagnosticPrinter.h
@@ -0,0 +1,46 @@
+//===--- TextDiagnosticPrinter.h - Text Diagnostic Client -------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is a concrete diagnostic client, which prints the diagnostics to
+// standard error.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEXT_DIAGNOSTIC_PRINTER_H_
+#define TEXT_DIAGNOSTIC_PRINTER_H_
+
+#include "TextDiagnostics.h"
+#include "clang/Basic/SourceLocation.h"
+
+namespace clang {
+class SourceManager;
+
+class TextDiagnosticPrinter : public TextDiagnostics {
+ SourceLocation LastWarningLoc;
+public:
+ TextDiagnosticPrinter(SourceManager &sourceMgr)
+ : TextDiagnostics(sourceMgr) {}
+
+ void PrintIncludeStack(SourceLocation Pos);
+ void HighlightRange(const SourceRange &R, unsigned LineNo,
+ std::string &CaratLine,
+ const std::string &SourceLine);
+ unsigned GetTokenLength(SourceLocation Loc);
+
+ virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+ SourceLocation Pos,
+ diag::kind ID, const std::string *Strs,
+ unsigned NumStrs,
+ const SourceRange *Ranges,
+ unsigned NumRanges);
+};
+
+} // end namspace clang
+
+#endif
diff --git a/Driver/TextDiagnostics.cpp b/Driver/TextDiagnostics.cpp
new file mode 100644
index 0000000..4fc7e0c
--- /dev/null
+++ b/Driver/TextDiagnostics.cpp
@@ -0,0 +1,60 @@
+//===--- TextDiagnostics.cpp - Text Diagnostics Parent Class --------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is the parent class for all text diagnostics.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TextDiagnostics.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Lex/HeaderSearch.h"
+using namespace clang;
+
+TextDiagnostics:: ~TextDiagnostics() {}
+
+std::string TextDiagnostics::FormatDiagnostic(Diagnostic::Level Level,
+ diag::kind ID,
+ const std::string *Strs,
+ unsigned NumStrs) {
+ std::string Msg = Diagnostic::getDescription(ID);
+
+ // Replace all instances of %0 in Msg with 'Extra'.
+ for (unsigned i = 0; i < Msg.size() - 1; ++i) {
+ if (Msg[i] == '%' && isdigit(Msg[i + 1])) {
+ unsigned StrNo = Msg[i + 1] - '0';
+ Msg = std::string(Msg.begin(), Msg.begin() + i) +
+ (StrNo < NumStrs ? Strs[StrNo] : "<<<INTERNAL ERROR>>>") +
+ std::string(Msg.begin() + i + 2, Msg.end());
+ }
+ }
+
+ return Msg;
+}
+
+bool TextDiagnostics::IgnoreDiagnostic(Diagnostic::Level Level,
+ SourceLocation Pos) {
+ if (Pos.isValid()) {
+ // If this is a warning or note, and if it a system header, suppress the
+ // diagnostic.
+ if (Level == Diagnostic::Warning ||
+ Level == Diagnostic::Note) {
+ SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(Pos);
+ const FileEntry *F = SourceMgr.getFileEntryForFileID(PhysLoc.getFileID());
+ if (F) {
+ DirectoryLookup::DirType DirInfo = TheHeaderSearch->getFileDirFlavor(F);
+ if (DirInfo == DirectoryLookup::SystemHeaderDir ||
+ DirInfo == DirectoryLookup::ExternCSystemHeaderDir)
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
diff --git a/Driver/TextDiagnostics.h b/Driver/TextDiagnostics.h
new file mode 100644
index 0000000..faf1b41
--- /dev/null
+++ b/Driver/TextDiagnostics.h
@@ -0,0 +1,53 @@
+//===--- TextDiagnostics.h - Text Diagnostics Checkers ----------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Bill Wendling and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is the parent class for all text diagnostics.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEXT_DIAGNOSTICS_H_
+#define TEXT_DIAGNOSTICS_H_
+
+#include "clang/Basic/Diagnostic.h"
+
+namespace clang {
+class SourceManager;
+class HeaderSearch;
+class Preprocessor;
+
+class TextDiagnostics : public DiagnosticClient {
+ HeaderSearch *TheHeaderSearch;
+protected:
+ SourceManager &SourceMgr;
+ Preprocessor *ThePreprocessor;
+
+ std::string FormatDiagnostic(Diagnostic::Level Level,
+ diag::kind ID,
+ const std::string *Strs,
+ unsigned NumStrs);
+public:
+ TextDiagnostics(SourceManager &sourceMgr) : SourceMgr(sourceMgr) {}
+ virtual ~TextDiagnostics();
+
+ void setHeaderSearch(HeaderSearch &HS) { TheHeaderSearch = &HS; }
+ void setPreprocessor(Preprocessor &P) { ThePreprocessor = &P; }
+
+ virtual bool IgnoreDiagnostic(Diagnostic::Level Level,
+ SourceLocation Pos);
+ virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
+ SourceLocation Pos,
+ diag::kind ID, const std::string *Strs,
+ unsigned NumStrs,
+ const SourceRange *Ranges,
+ unsigned NumRanges) = 0;
+};
+
+} // end namspace clang
+
+#endif
diff --git a/Driver/X86Builtins.def b/Driver/X86Builtins.def
new file mode 100644
index 0000000..c4e3033
--- /dev/null
+++ b/Driver/X86Builtins.def
@@ -0,0 +1,420 @@
+//===--- X86Builtins.def - X86 Builtin function database --------*- C++ -*-===//
+//
+// 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 file defines the X86-specific builtin function database. Users of
+// this file must define the BUILTIN macro to make use of this information.
+//
+//===----------------------------------------------------------------------===//
+
+// FIXME: this needs to be the full list supported by GCC. Right now, I'm just
+// adding stuff on demand.
+
+// The format of this database matches clang/AST/Builtins.def.
+
+BUILTIN(__builtin_ia32_emms , "v", "")
+
+// FIXME: These types are incorrect.
+// SSE intrinsics.
+BUILTIN(__builtin_ia32_comieq, "v", "")
+BUILTIN(__builtin_ia32_comilt, "v", "")
+BUILTIN(__builtin_ia32_comile, "v", "")
+BUILTIN(__builtin_ia32_comigt, "v", "")
+BUILTIN(__builtin_ia32_comige, "v", "")
+BUILTIN(__builtin_ia32_comineq, "v", "")
+BUILTIN(__builtin_ia32_ucomieq, "v", "")
+BUILTIN(__builtin_ia32_ucomilt, "v", "")
+BUILTIN(__builtin_ia32_ucomile, "v", "")
+BUILTIN(__builtin_ia32_ucomigt, "v", "")
+BUILTIN(__builtin_ia32_ucomige, "v", "")
+BUILTIN(__builtin_ia32_ucomineq, "v", "")
+BUILTIN(__builtin_ia32_comisdeq, "v", "")
+BUILTIN(__builtin_ia32_comisdlt, "v", "")
+BUILTIN(__builtin_ia32_comisdle, "v", "")
+BUILTIN(__builtin_ia32_comisdgt, "v", "")
+BUILTIN(__builtin_ia32_comisdge, "v", "")
+BUILTIN(__builtin_ia32_comisdneq, "v", "")
+BUILTIN(__builtin_ia32_ucomisdeq, "v", "")
+BUILTIN(__builtin_ia32_ucomisdlt, "v", "")
+BUILTIN(__builtin_ia32_ucomisdle, "v", "")
+BUILTIN(__builtin_ia32_ucomisdgt, "v", "")
+BUILTIN(__builtin_ia32_ucomisdge, "v", "")
+BUILTIN(__builtin_ia32_ucomisdneq, "v", "")
+BUILTIN(__builtin_ia32_addps, "v", "")
+BUILTIN(__builtin_ia32_subps, "v", "")
+BUILTIN(__builtin_ia32_mulps, "v", "")
+BUILTIN(__builtin_ia32_divps, "v", "")
+BUILTIN(__builtin_ia32_addss, "v", "")
+BUILTIN(__builtin_ia32_subss, "v", "")
+BUILTIN(__builtin_ia32_mulss, "v", "")
+BUILTIN(__builtin_ia32_divss, "v", "")
+BUILTIN(__builtin_ia32_cmpeqps, "v", "")
+BUILTIN(__builtin_ia32_cmpltps, "v", "")
+BUILTIN(__builtin_ia32_cmpleps, "v", "")
+BUILTIN(__builtin_ia32_cmpgtps, "v", "")
+BUILTIN(__builtin_ia32_cmpgeps, "v", "")
+BUILTIN(__builtin_ia32_cmpunordps, "v", "")
+BUILTIN(__builtin_ia32_cmpneqps, "v", "")
+BUILTIN(__builtin_ia32_cmpnltps, "v", "")
+BUILTIN(__builtin_ia32_cmpnleps, "v", "")
+BUILTIN(__builtin_ia32_cmpngtps, "v", "")
+BUILTIN(__builtin_ia32_cmpngeps, "v", "")
+BUILTIN(__builtin_ia32_cmpordps, "v", "")
+BUILTIN(__builtin_ia32_cmpeqss, "v", "")
+BUILTIN(__builtin_ia32_cmpltss, "v", "")
+BUILTIN(__builtin_ia32_cmpless, "v", "")
+BUILTIN(__builtin_ia32_cmpunordss, "v", "")
+BUILTIN(__builtin_ia32_cmpneqss, "v", "")
+BUILTIN(__builtin_ia32_cmpnltss, "v", "")
+BUILTIN(__builtin_ia32_cmpnless, "v", "")
+BUILTIN(__builtin_ia32_cmpngtss, "v", "")
+BUILTIN(__builtin_ia32_cmpngess, "v", "")
+BUILTIN(__builtin_ia32_cmpordss, "v", "")
+BUILTIN(__builtin_ia32_minps, "v", "")
+BUILTIN(__builtin_ia32_maxps, "v", "")
+BUILTIN(__builtin_ia32_minss, "v", "")
+BUILTIN(__builtin_ia32_maxss, "v", "")
+BUILTIN(__builtin_ia32_andps, "v", "")
+BUILTIN(__builtin_ia32_andnps, "v", "")
+BUILTIN(__builtin_ia32_orps, "v", "")
+BUILTIN(__builtin_ia32_xorps, "v", "")
+BUILTIN(__builtin_ia32_movss, "v", "")
+BUILTIN(__builtin_ia32_movhlps, "v", "")
+BUILTIN(__builtin_ia32_movlhps, "v", "")
+BUILTIN(__builtin_ia32_unpckhps, "v", "")
+BUILTIN(__builtin_ia32_unpcklps, "v", "")
+BUILTIN(__builtin_ia32_paddb, "v", "")
+BUILTIN(__builtin_ia32_paddw, "v", "")
+BUILTIN(__builtin_ia32_paddd, "v", "")
+BUILTIN(__builtin_ia32_paddq, "v", "")
+BUILTIN(__builtin_ia32_psubb, "v", "")
+BUILTIN(__builtin_ia32_psubw, "v", "")
+BUILTIN(__builtin_ia32_psubd, "v", "")
+BUILTIN(__builtin_ia32_psubq, "v", "")
+BUILTIN(__builtin_ia32_paddsb, "v", "")
+BUILTIN(__builtin_ia32_paddsw, "v", "")
+BUILTIN(__builtin_ia32_psubsb, "v", "")
+BUILTIN(__builtin_ia32_psubsw, "v", "")
+BUILTIN(__builtin_ia32_paddusb, "v", "")
+BUILTIN(__builtin_ia32_paddusw, "v", "")
+BUILTIN(__builtin_ia32_psubusb, "v", "")
+BUILTIN(__builtin_ia32_psubusw, "v", "")
+BUILTIN(__builtin_ia32_pmullw, "v", "")
+BUILTIN(__builtin_ia32_pmulhw, "v", "")
+BUILTIN(__builtin_ia32_pmulhuw, "v", "")
+BUILTIN(__builtin_ia32_pand, "v", "")
+BUILTIN(__builtin_ia32_pandn, "v", "")
+BUILTIN(__builtin_ia32_por, "v", "")
+BUILTIN(__builtin_ia32_pxor, "v", "")
+BUILTIN(__builtin_ia32_pavgb, "v", "")
+BUILTIN(__builtin_ia32_pavgw, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqb, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqw, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqd, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtb, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtw, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtd, "v", "")
+BUILTIN(__builtin_ia32_pmaxub, "v", "")
+BUILTIN(__builtin_ia32_pmaxsw, "v", "")
+BUILTIN(__builtin_ia32_pminub, "v", "")
+BUILTIN(__builtin_ia32_pminsw, "v", "")
+BUILTIN(__builtin_ia32_punpckhbw, "v", "")
+BUILTIN(__builtin_ia32_punpckhwd, "v", "")
+BUILTIN(__builtin_ia32_punpckhdq, "v", "")
+BUILTIN(__builtin_ia32_punpcklbw, "v", "")
+BUILTIN(__builtin_ia32_punpcklwd, "v", "")
+BUILTIN(__builtin_ia32_punpckldq, "v", "")
+BUILTIN(__builtin_ia32_addpd, "v", "")
+BUILTIN(__builtin_ia32_subpd, "v", "")
+BUILTIN(__builtin_ia32_mulpd, "v", "")
+BUILTIN(__builtin_ia32_divpd, "v", "")
+BUILTIN(__builtin_ia32_addsd, "v", "")
+BUILTIN(__builtin_ia32_subsd, "v", "")
+BUILTIN(__builtin_ia32_mulsd, "v", "")
+BUILTIN(__builtin_ia32_divsd, "v", "")
+BUILTIN(__builtin_ia32_cmpeqpd, "v", "")
+BUILTIN(__builtin_ia32_cmpltpd, "v", "")
+BUILTIN(__builtin_ia32_cmplepd, "v", "")
+BUILTIN(__builtin_ia32_cmpgtpd, "v", "")
+BUILTIN(__builtin_ia32_cmpgepd, "v", "")
+BUILTIN(__builtin_ia32_cmpunordpd, "v", "")
+BUILTIN(__builtin_ia32_cmpneqpd, "v", "")
+BUILTIN(__builtin_ia32_cmpnltpd, "v", "")
+BUILTIN(__builtin_ia32_cmpnlepd, "v", "")
+BUILTIN(__builtin_ia32_cmpngtpd, "v", "")
+BUILTIN(__builtin_ia32_cmpngepd, "v", "")
+BUILTIN(__builtin_ia32_cmpordpd, "v", "")
+BUILTIN(__builtin_ia32_cmpeqsd, "v", "")
+BUILTIN(__builtin_ia32_cmpltsd, "v", "")
+BUILTIN(__builtin_ia32_cmplesd, "v", "")
+BUILTIN(__builtin_ia32_cmpunordsd, "v", "")
+BUILTIN(__builtin_ia32_cmpneqsd, "v", "")
+BUILTIN(__builtin_ia32_cmpnltsd, "v", "")
+BUILTIN(__builtin_ia32_cmpnlesd, "v", "")
+BUILTIN(__builtin_ia32_cmpordsd, "v", "")
+BUILTIN(__builtin_ia32_minpd, "v", "")
+BUILTIN(__builtin_ia32_maxpd, "v", "")
+BUILTIN(__builtin_ia32_minsd, "v", "")
+BUILTIN(__builtin_ia32_maxsd, "v", "")
+BUILTIN(__builtin_ia32_andpd, "v", "")
+BUILTIN(__builtin_ia32_andnpd, "v", "")
+BUILTIN(__builtin_ia32_orpd, "v", "")
+BUILTIN(__builtin_ia32_xorpd, "v", "")
+BUILTIN(__builtin_ia32_movsd, "v", "")
+BUILTIN(__builtin_ia32_unpckhpd, "v", "")
+BUILTIN(__builtin_ia32_unpcklpd, "v", "")
+BUILTIN(__builtin_ia32_paddb128, "v", "")
+BUILTIN(__builtin_ia32_paddw128, "v", "")
+BUILTIN(__builtin_ia32_paddd128, "v", "")
+BUILTIN(__builtin_ia32_paddq128, "v", "")
+BUILTIN(__builtin_ia32_psubb128, "v", "")
+BUILTIN(__builtin_ia32_psubw128, "v", "")
+BUILTIN(__builtin_ia32_psubd128, "v", "")
+BUILTIN(__builtin_ia32_psubq128, "v", "")
+BUILTIN(__builtin_ia32_paddsb128, "v", "")
+BUILTIN(__builtin_ia32_paddsw128, "v", "")
+BUILTIN(__builtin_ia32_psubsb128, "v", "")
+BUILTIN(__builtin_ia32_psubsw128, "v", "")
+BUILTIN(__builtin_ia32_paddusb128, "v", "")
+BUILTIN(__builtin_ia32_paddusw128, "v", "")
+BUILTIN(__builtin_ia32_psubusb128, "v", "")
+BUILTIN(__builtin_ia32_psubusw128, "v", "")
+BUILTIN(__builtin_ia32_pmullw128, "v", "")
+BUILTIN(__builtin_ia32_pmulhw128, "v", "")
+BUILTIN(__builtin_ia32_pand128, "v", "")
+BUILTIN(__builtin_ia32_pandn128, "v", "")
+BUILTIN(__builtin_ia32_por128, "v", "")
+BUILTIN(__builtin_ia32_pxor128, "v", "")
+BUILTIN(__builtin_ia32_pavgb128, "v", "")
+BUILTIN(__builtin_ia32_pavgw128, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqb128, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqw128, "v", "")
+BUILTIN(__builtin_ia32_pcmpeqd128, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtb128, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtw128, "v", "")
+BUILTIN(__builtin_ia32_pcmpgtd128, "v", "")
+BUILTIN(__builtin_ia32_pmaxub128, "v", "")
+BUILTIN(__builtin_ia32_pmaxsw128, "v", "")
+BUILTIN(__builtin_ia32_pminub128, "v", "")
+BUILTIN(__builtin_ia32_pminsw128, "v", "")
+BUILTIN(__builtin_ia32_punpckhbw128, "v", "")
+BUILTIN(__builtin_ia32_punpckhwd128, "v", "")
+BUILTIN(__builtin_ia32_punpckhdq128, "v", "")
+BUILTIN(__builtin_ia32_punpckhqdq128, "v", "")
+BUILTIN(__builtin_ia32_punpcklbw128, "v", "")
+BUILTIN(__builtin_ia32_punpcklwd128, "v", "")
+BUILTIN(__builtin_ia32_punpckldq128, "v", "")
+BUILTIN(__builtin_ia32_punpcklqdq128, "v", "")
+BUILTIN(__builtin_ia32_packsswb128, "v", "")
+BUILTIN(__builtin_ia32_packssdw128, "v", "")
+BUILTIN(__builtin_ia32_packuswb128, "v", "")
+BUILTIN(__builtin_ia32_pmulhuw128, "v", "")
+BUILTIN(__builtin_ia32_addsubps, "v", "")
+BUILTIN(__builtin_ia32_addsubpd, "v", "")
+BUILTIN(__builtin_ia32_haddps, "v", "")
+BUILTIN(__builtin_ia32_haddpd, "v", "")
+BUILTIN(__builtin_ia32_hsubps, "v", "")
+BUILTIN(__builtin_ia32_hsubpd, "v", "")
+BUILTIN(__builtin_ia32_phaddw128, "v", "")
+BUILTIN(__builtin_ia32_phaddw, "v", "")
+BUILTIN(__builtin_ia32_phaddd128, "v", "")
+BUILTIN(__builtin_ia32_phaddd, "v", "")
+BUILTIN(__builtin_ia32_phaddsw128, "v", "")
+BUILTIN(__builtin_ia32_phaddsw, "v", "")
+BUILTIN(__builtin_ia32_phsubw128, "v", "")
+BUILTIN(__builtin_ia32_phsubw, "v", "")
+BUILTIN(__builtin_ia32_phsubd128, "v", "")
+BUILTIN(__builtin_ia32_phsubd, "v", "")
+BUILTIN(__builtin_ia32_phsubsw128, "v", "")
+BUILTIN(__builtin_ia32_phsubsw, "v", "")
+BUILTIN(__builtin_ia32_pmaddubsw128, "v", "")
+BUILTIN(__builtin_ia32_pmaddubsw, "v", "")
+BUILTIN(__builtin_ia32_pmulhrsw128, "v", "")
+BUILTIN(__builtin_ia32_pmulhrsw, "v", "")
+BUILTIN(__builtin_ia32_pshufb128, "v", "")
+BUILTIN(__builtin_ia32_pshufb, "v", "")
+BUILTIN(__builtin_ia32_psignb128, "v", "")
+BUILTIN(__builtin_ia32_psignb, "v", "")
+BUILTIN(__builtin_ia32_psignw128, "v", "")
+BUILTIN(__builtin_ia32_psignw, "v", "")
+BUILTIN(__builtin_ia32_psignd128, "v", "")
+BUILTIN(__builtin_ia32_psignd, "v", "")
+BUILTIN(__builtin_ia32_pabsb128, "v", "")
+BUILTIN(__builtin_ia32_pabsb, "v", "")
+BUILTIN(__builtin_ia32_pabsw128, "v", "")
+BUILTIN(__builtin_ia32_pabsw, "v", "")
+BUILTIN(__builtin_ia32_pabsd128, "v", "")
+BUILTIN(__builtin_ia32_pabsd, "v", "")
+BUILTIN(__builtin_ia32_psllw, "v", "")
+BUILTIN(__builtin_ia32_pslld, "v", "")
+BUILTIN(__builtin_ia32_psllq, "v", "")
+BUILTIN(__builtin_ia32_psrlw, "v", "")
+BUILTIN(__builtin_ia32_psrld, "v", "")
+BUILTIN(__builtin_ia32_psrlq, "v", "")
+BUILTIN(__builtin_ia32_psraw, "v", "")
+BUILTIN(__builtin_ia32_psrad, "v", "")
+BUILTIN(__builtin_ia32_pshufw, "v", "")
+BUILTIN(__builtin_ia32_pmaddwd, "v", "")
+BUILTIN(__builtin_ia32_packsswb, "v", "")
+BUILTIN(__builtin_ia32_packssdw, "v", "")
+BUILTIN(__builtin_ia32_packuswb, "v", "")
+BUILTIN(__builtin_ia32_ldmxcsr, "v", "")
+BUILTIN(__builtin_ia32_stmxcsr, "v", "")
+BUILTIN(__builtin_ia32_cvtpi2ps, "v", "")
+BUILTIN(__builtin_ia32_cvtps2pi, "v", "")
+BUILTIN(__builtin_ia32_cvtsi2ss, "v", "")
+BUILTIN(__builtin_ia32_cvtsi642ss, "v", "")
+BUILTIN(__builtin_ia32_cvtss2si, "v", "")
+BUILTIN(__builtin_ia32_cvtss2si64, "v", "")
+BUILTIN(__builtin_ia32_cvttps2pi, "v", "")
+BUILTIN(__builtin_ia32_cvttss2si, "v", "")
+BUILTIN(__builtin_ia32_cvttss2si64, "v", "")
+BUILTIN(__builtin_ia32_maskmovq, "v", "")
+BUILTIN(__builtin_ia32_loadups, "v", "")
+BUILTIN(__builtin_ia32_storeups, "v", "")
+BUILTIN(__builtin_ia32_loadhps, "v", "")
+BUILTIN(__builtin_ia32_loadlps, "v", "")
+BUILTIN(__builtin_ia32_storehps, "v", "")
+BUILTIN(__builtin_ia32_storelps, "v", "")
+BUILTIN(__builtin_ia32_movmskps, "v", "")
+BUILTIN(__builtin_ia32_pmovmskb, "v", "")
+BUILTIN(__builtin_ia32_movntps, "v", "")
+BUILTIN(__builtin_ia32_movntq, "v", "")
+BUILTIN(__builtin_ia32_sfence, "v", "")
+BUILTIN(__builtin_ia32_psadbw, "v", "")
+BUILTIN(__builtin_ia32_rcpps, "v", "")
+BUILTIN(__builtin_ia32_rcpss, "v", "")
+BUILTIN(__builtin_ia32_rsqrtps, "v", "")
+BUILTIN(__builtin_ia32_rsqrtss, "v", "")
+BUILTIN(__builtin_ia32_sqrtps, "v", "")
+BUILTIN(__builtin_ia32_sqrtss, "v", "")
+BUILTIN(__builtin_ia32_shufps, "v", "")
+BUILTIN(__builtin_ia32_femms, "v", "")
+BUILTIN(__builtin_ia32_pavgusb, "v", "")
+BUILTIN(__builtin_ia32_pf2id, "v", "")
+BUILTIN(__builtin_ia32_pfacc, "v", "")
+BUILTIN(__builtin_ia32_pfadd, "v", "")
+BUILTIN(__builtin_ia32_pfcmpeq, "v", "")
+BUILTIN(__builtin_ia32_pfcmpge, "v", "")
+BUILTIN(__builtin_ia32_pfcmpgt, "v", "")
+BUILTIN(__builtin_ia32_pfmax, "v", "")
+BUILTIN(__builtin_ia32_pfmin, "v", "")
+BUILTIN(__builtin_ia32_pfmul, "v", "")
+BUILTIN(__builtin_ia32_pfrcp, "v", "")
+BUILTIN(__builtin_ia32_pfrcpit1, "v", "")
+BUILTIN(__builtin_ia32_pfrcpit2, "v", "")
+BUILTIN(__builtin_ia32_pfrsqrt, "v", "")
+BUILTIN(__builtin_ia32_pfrsqit1, "v", "")
+BUILTIN(__builtin_ia32_pfsub, "v", "")
+BUILTIN(__builtin_ia32_pfsubr, "v", "")
+BUILTIN(__builtin_ia32_pi2fd, "v", "")
+BUILTIN(__builtin_ia32_pmulhrw, "v", "")
+BUILTIN(__builtin_ia32_pf2iw, "v", "")
+BUILTIN(__builtin_ia32_pfnacc, "v", "")
+BUILTIN(__builtin_ia32_pfpnacc, "v", "")
+BUILTIN(__builtin_ia32_pi2fw, "v", "")
+BUILTIN(__builtin_ia32_pswapdsf, "v", "")
+BUILTIN(__builtin_ia32_pswapdsi, "v", "")
+BUILTIN(__builtin_ia32_maskmovdqu, "v", "")
+BUILTIN(__builtin_ia32_loadupd, "v", "")
+BUILTIN(__builtin_ia32_storeupd, "v", "")
+BUILTIN(__builtin_ia32_loadhpd, "v", "")
+BUILTIN(__builtin_ia32_loadlpd, "v", "")
+BUILTIN(__builtin_ia32_movmskpd, "v", "")
+BUILTIN(__builtin_ia32_pmovmskb128, "v", "")
+BUILTIN(__builtin_ia32_movnti, "v", "")
+BUILTIN(__builtin_ia32_movntpd, "v", "")
+BUILTIN(__builtin_ia32_movntdq, "v", "")
+BUILTIN(__builtin_ia32_pshufd, "v", "")
+BUILTIN(__builtin_ia32_pshuflw, "v", "")
+BUILTIN(__builtin_ia32_pshufhw, "v", "")
+BUILTIN(__builtin_ia32_psadbw128, "v", "")
+BUILTIN(__builtin_ia32_sqrtpd, "v", "")
+BUILTIN(__builtin_ia32_sqrtsd, "v", "")
+BUILTIN(__builtin_ia32_shufpd, "v", "")
+BUILTIN(__builtin_ia32_cvtdq2pd, "v", "")
+BUILTIN(__builtin_ia32_cvtdq2ps, "v", "")
+BUILTIN(__builtin_ia32_cvtpd2dq, "v", "")
+BUILTIN(__builtin_ia32_cvtpd2pi, "v", "")
+BUILTIN(__builtin_ia32_cvtpd2ps, "v", "")
+BUILTIN(__builtin_ia32_cvttpd2dq, "v", "")
+BUILTIN(__builtin_ia32_cvttpd2pi, "v", "")
+BUILTIN(__builtin_ia32_cvtpi2pd, "v", "")
+BUILTIN(__builtin_ia32_cvtsd2si, "v", "")
+BUILTIN(__builtin_ia32_cvttsd2si, "v", "")
+BUILTIN(__builtin_ia32_cvtsd2si64, "v", "")
+BUILTIN(__builtin_ia32_cvttsd2si64, "v", "")
+BUILTIN(__builtin_ia32_cvtps2dq, "v", "")
+BUILTIN(__builtin_ia32_cvtps2pd, "v", "")
+BUILTIN(__builtin_ia32_cvttps2dq, "v", "")
+BUILTIN(__builtin_ia32_cvtsi2sd, "v", "")
+BUILTIN(__builtin_ia32_cvtsi642sd, "v", "")
+BUILTIN(__builtin_ia32_cvtsd2ss, "v", "")
+BUILTIN(__builtin_ia32_cvtss2sd, "v", "")
+BUILTIN(__builtin_ia32_clflush, "v", "")
+BUILTIN(__builtin_ia32_lfence, "v", "")
+BUILTIN(__builtin_ia32_mfence, "v", "")
+BUILTIN(__builtin_ia32_loaddqu, "v", "")
+BUILTIN(__builtin_ia32_storedqu, "v", "")
+BUILTIN(__builtin_ia32_psllwi, "v", "")
+BUILTIN(__builtin_ia32_pslldi, "v", "")
+BUILTIN(__builtin_ia32_psllqi, "v", "")
+BUILTIN(__builtin_ia32_psrawi, "v", "")
+BUILTIN(__builtin_ia32_psradi, "v", "")
+BUILTIN(__builtin_ia32_psrlwi, "v", "")
+BUILTIN(__builtin_ia32_psrldi, "v", "")
+BUILTIN(__builtin_ia32_psrlqi, "v", "")
+BUILTIN(__builtin_ia32_pmuludq, "v", "")
+BUILTIN(__builtin_ia32_pmuludq128, "v", "")
+BUILTIN(__builtin_ia32_psllw128, "v", "")
+BUILTIN(__builtin_ia32_pslld128, "v", "")
+BUILTIN(__builtin_ia32_psllq128, "v", "")
+BUILTIN(__builtin_ia32_psrlw128, "v", "")
+BUILTIN(__builtin_ia32_psrld128, "v", "")
+BUILTIN(__builtin_ia32_psrlq128, "v", "")
+BUILTIN(__builtin_ia32_psraw128, "v", "")
+BUILTIN(__builtin_ia32_psrad128, "v", "")
+BUILTIN(__builtin_ia32_pslldqi128, "v", "")
+BUILTIN(__builtin_ia32_psllwi128, "v", "")
+BUILTIN(__builtin_ia32_pslldi128, "v", "")
+BUILTIN(__builtin_ia32_psllqi128, "v", "")
+BUILTIN(__builtin_ia32_psrldqi128, "v", "")
+BUILTIN(__builtin_ia32_psrlwi128, "v", "")
+BUILTIN(__builtin_ia32_psrldi128, "v", "")
+BUILTIN(__builtin_ia32_psrlqi128, "v", "")
+BUILTIN(__builtin_ia32_psrawi128, "v", "")
+BUILTIN(__builtin_ia32_psradi128, "v", "")
+BUILTIN(__builtin_ia32_pmaddwd128, "v", "")
+BUILTIN(__builtin_ia32_monitor, "v", "")
+BUILTIN(__builtin_ia32_mwait, "v", "")
+BUILTIN(__builtin_ia32_movshdup, "v", "")
+BUILTIN(__builtin_ia32_movsldup, "v", "")
+BUILTIN(__builtin_ia32_lddqu, "v", "")
+BUILTIN(__builtin_ia32_palignr128, "v", "")
+BUILTIN(__builtin_ia32_palignr, "v", "")
+BUILTIN(__builtin_ia32_vec_init_v2si, "v", "")
+BUILTIN(__builtin_ia32_vec_init_v4hi, "v", "")
+BUILTIN(__builtin_ia32_vec_init_v8qi, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v2df, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v2di, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v4sf, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v4si, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v8hi, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v4hi, "v", "")
+BUILTIN(__builtin_ia32_vec_ext_v2si, "v", "")
+BUILTIN(__builtin_ia32_vec_set_v8hi, "v", "")
+BUILTIN(__builtin_ia32_vec_set_v4hi, "v", "")
+
+// Apple local SSE builtins? These are probably not needed eventually, but are
+// in the apple-gcc xmmintrin.h file (rdar://4099020).
+BUILTIN(__builtin_ia32_movqv4si, "v", "")
+BUILTIN(__builtin_ia32_loadlv4si, "v", "")
+BUILTIN(__builtin_ia32_storelv4si, "v", "")
+
+
+#undef BUILTIN
diff --git a/Driver/clang.cpp b/Driver/clang.cpp
new file mode 100644
index 0000000..43eaee6
--- /dev/null
+++ b/Driver/clang.cpp
@@ -0,0 +1,914 @@
+//===--- 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 help info.
+// clang [options] - Read from stdin.
+// clang [options] file - Read from "file".
+// clang [options] file1 file2 - Read these files.
+//
+//===----------------------------------------------------------------------===//
+//
+// TODO: Options to support:
+//
+// -ffatal-errors
+// -ftabstop=width
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang.h"
+#include "ASTStreamers.h"
+#include "TextDiagnosticBuffer.h"
+#include "TextDiagnosticPrinter.h"
+#include "clang/Parse/Parser.h"
+#include "clang/Lex/HeaderSearch.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/TargetInfo.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/System/Signals.h"
+#include <memory>
+using namespace clang;
+
+//===----------------------------------------------------------------------===//
+// Global options.
+//===----------------------------------------------------------------------===//
+
+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"));
+
+enum ProgActions {
+ EmitLLVM, // Emit a .ll file.
+ ParseASTPrint, // Parse ASTs and print them.
+ ParseASTCheck, // Parse ASTs and check diagnostics.
+ ParseAST, // Parse ASTs.
+ ParsePrintCallbacks, // Parse and print each callback.
+ ParseSyntaxOnly, // Parse and perform semantic analysis.
+ ParseNoop, // Parse with noop callbacks.
+ RunPreprocessorOnly, // Just lex, no output.
+ PrintPreprocessedInput, // -E mode.
+ DumpTokens // Token dump mode.
+};
+
+static llvm::cl::opt<ProgActions>
+ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore,
+ llvm::cl::init(ParseSyntaxOnly),
+ llvm::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"),
+ clEnumValN(ParseNoop, "parse-noop",
+ "Run parser with noop callbacks (for timings)"),
+ clEnumValN(ParseSyntaxOnly, "fsyntax-only",
+ "Run parser and perform semantic analysis"),
+ clEnumValN(ParsePrintCallbacks, "parse-print-callbacks",
+ "Run parser and print each callback invoked"),
+ clEnumValN(ParseAST, "parse-ast",
+ "Run parser and build ASTs"),
+ clEnumValN(ParseASTPrint, "parse-ast-print",
+ "Run parser, build ASTs, then print ASTs"),
+ clEnumValN(ParseASTCheck, "parse-ast-check",
+ "Run parser, build ASTs, then check diagnostics"),
+ clEnumValN(EmitLLVM, "emit-llvm",
+ "Build ASTs then convert to LLVM, emit .ll file"),
+ clEnumValEnd));
+
+//===----------------------------------------------------------------------===//
+// 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 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"),
+ 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 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);
+
+/// 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 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"),
+ clEnumValN(lang_c89, "c90", "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));
+
+static llvm::cl::opt<bool>
+NoOperatorNames("fno-operator-names",
+ llvm::cl::desc("Do not treat C++ operator name keywords as "
+ "synonyms for operators"));
+
+// 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:
+ LangStd = lang_gnu99;
+ 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:
+ case lang_cxx98:
+ Options.CPlusPlus = 1;
+ Options.CXXOperatorNames = !NoOperatorNames;
+ // 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.
+}
+
+//===----------------------------------------------------------------------===//
+// Our DiagnosticClient implementation
+//===----------------------------------------------------------------------===//
+
+// FIXME: Werror should take a list of things, -Werror=foo,bar
+static llvm::cl::opt<bool>
+WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors"));
+
+static llvm::cl::opt<bool>
+WarnOnExtensions("pedantic", llvm::cl::init(false),
+ llvm::cl::desc("Issue a warning on uses of GCC extensions"));
+
+static llvm::cl::opt<bool>
+ErrorOnExtensions("pedantic-errors",
+ llvm::cl::desc("Issue an error on uses of GCC extensions"));
+
+static llvm::cl::opt<bool>
+WarnUnusedMacros("Wunused_macros",
+ llvm::cl::desc("Warn for unused macros in the main translation unit"));
+
+
+/// 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);
+
+ // Silence the "macro is not used" warning unless requested.
+ if (!WarnUnusedMacros)
+ Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
+}
+
+//===----------------------------------------------------------------------===//
+// Preprocessor Initialization
+//===----------------------------------------------------------------------===//
+
+// FIXME: Preprocessor builtins to support.
+// -A... - Play with #assertions
+// -undef - Undefine all predefined macros
+
+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"));
+
+// 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. */
+ //case BT_STDC:
+ if (cpp_in_system_header (pfile))
+ number = 0;
+ else
+ number = 1;
+ break;
+#endif
+ // These should all be defined in the preprocessor according to the
+ // current language configuration.
+ DefineBuiltinMacro(Buf, "__STDC__=1");
+ //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1");
+ if (PP.getLangOptions().C99)
+ DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L");
+ else
+ DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L");
+
+ DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1");
+ if (PP.getLangOptions().ObjC1)
+ DefineBuiltinMacro(Buf, "__OBJC__=1");
+ if (PP.getLangOptions().ObjC2)
+ DefineBuiltinMacro(Buf, "__OBJC2__=1");
+
+ // Get the target #defines.
+ PP.getTargetInfo().getTargetDefines(Buf);
+
+ // Compiler set macros.
+ DefineBuiltinMacro(Buf, "__APPLE_CC__=5250");
+ 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");
+ DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. "
+ "build 5250)\"");
+
+ // Build configuration options.
+ DefineBuiltinMacro(Buf, "__DYNAMIC__=1");
+ DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0");
+ DefineBuiltinMacro(Buf, "__NO_INLINE__=1");
+ DefineBuiltinMacro(Buf, "__PIC__=1");
+
+
+ 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 llvm::cl::opt<bool>
+nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories"));
+
+// Various command line options. These four add directories to each chain.
+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"));
+
+// These handle -iprefix/-iwithprefix/-iwithprefixbefore.
+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"));
+
+// 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,
+ bool isFramework, FileManager &FM) {
+ const DirectoryEntry *DE = FM.getDirectory(Path);
+ if (DE == 0) {
+ if (Verbose)
+ fprintf(stderr, "ignoring nonexistent directory \"%s\"\n",
+ Path.c_str());
+ return;
+ }
+
+ DirectoryLookup::DirType Type;
+ if (Group == Quoted || Group == Angled)
+ Type = DirectoryLookup::NormalHeaderDir;
+ else if (isCXXAware)
+ Type = DirectoryLookup::SystemHeaderDir;
+ else
+ Type = DirectoryLookup::ExternCSystemHeaderDir;
+
+ IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied,
+ isFramework));
+}
+
+/// 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)
+ fprintf(stderr, "ignoring duplicate directory \"%s\"\n",
+ SearchList[i].getDir()->getName());
+ SearchList.erase(SearchList.begin()+i);
+ --i;
+ }
+ }
+}
+
+/// InitializeIncludePaths - Process the -I options and set them in the
+/// HeaderSearch object.
+static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM,
+ Diagnostic &Diags, const LangOptions &Lang) {
+ // Handle -F... options.
+ for (unsigned i = 0, e = F_dirs.size(); i != e; ++i)
+ AddPath(F_dirs[i], Angled, false, true, true, FM);
+
+ // Handle -I... options.
+ for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) {
+ if (I_dirs[i] == "-") {
+ // -I- is a deprecated GCC feature.
+ Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported);
+ } else {
+ AddPath(I_dirs[i], Angled, false, true, false, FM);
+ }
+ }
+
+ // Handle -idirafter... options.
+ for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i)
+ AddPath(idirafter_dirs[i], After, false, true, false, FM);
+
+ // Handle -iquote... options.
+ for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i)
+ AddPath(iquote_dirs[i], Quoted, false, true, false, FM);
+
+ // Handle -isystem... options.
+ for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i)
+ AddPath(isystem_dirs[i], System, false, true, false, FM);
+
+ // 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],
+ System, false, false, false, FM);
+ ++iwithprefix_idx;
+ iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size();
+ } else {
+ AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx],
+ Angled, false, false, false, FM);
+ ++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.
+ // FIXME: get these from the target?
+ if (!nostdinc) {
+ 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);
+ }
+
+ AddPath("/usr/local/include", System, false, false, false, FM);
+ // leopard
+ AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System,
+ false, false, false, FM);
+ AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include",
+ System, false, false, false, FM);
+ AddPath("/usr/lib/gcc/powerpc-apple-darwin9/"
+ "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/"
+ "4.0.1/../../../../powerpc-apple-darwin8/include",
+ 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);
+ }
+
+ // 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?
+ Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(),
+ DontSearchCurDir);
+
+ // If verbose, print the list of directories that will be searched.
+ if (Verbose) {
+ fprintf(stderr, "#include \"...\" search starts here:\n");
+ unsigned QuotedIdx = IncludeGroup[Quoted].size();
+ for (unsigned i = 0, e = SearchList.size(); i != e; ++i) {
+ if (i == QuotedIdx)
+ fprintf(stderr, "#include <...> search starts here:\n");
+ fprintf(stderr, " %s\n", SearchList[i].getDir()->getName());
+ }
+ }
+}
+
+
+// Read any files specified by -imacros or -include.
+static void ReadPrologFiles(Preprocessor &PP, std::vector<char> &Buf) {
+ // FIXME: IMPLEMENT
+}
+
+//===----------------------------------------------------------------------===//
+// Basic Parser driver
+//===----------------------------------------------------------------------===//
+
+static void ParseFile(Preprocessor &PP, MinimalAction *PA, unsigned MainFileID){
+ Parser P(PP, *PA);
+ PP.EnterSourceFile(MainFileID, 0, true);
+
+ // Parsing the specified input file.
+ P.ParseTranslationUnit();
+ delete PA;
+}
+
+//===----------------------------------------------------------------------===//
+// Main driver
+//===----------------------------------------------------------------------===//
+
+/// 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> &PrologMacros) {
+ FileManager &FileMgr = HeaderInfo.getFileMgr();
+
+ // Install things like __POWERPC__, __GNUC__, etc into the macro table.
+ InitializePredefinedMacros(PP, PrologMacros);
+
+ // Read any files specified by -imacros or -include.
+ ReadPrologFiles(PP, PrologMacros);
+
+ // 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;
+ }
+ }
+
+ // 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);
+
+ llvm::MemoryBuffer *SB =
+ llvm::MemoryBuffer::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;
+ PP.Lex(Tok);
+ assert(Tok.getKind() == tok::eof && "Didn't read entire file!");
+
+ // Once we've read this, we're done.
+ return MainFileID;
+}
+
+/// 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) {
+ switch (ProgAction) {
+ default:
+ fprintf(stderr, "Unexpected program action!\n");
+ return;
+ case DumpTokens: { // Token dump mode.
+ LexerToken Tok;
+ // Start parsing the specified input file.
+ PP.EnterSourceFile(MainFileID, 0, true);
+ do {
+ PP.Lex(Tok);
+ PP.DumpToken(Tok, true);
+ fprintf(stderr, "\n");
+ } while (Tok.getKind() != tok::eof);
+ break;
+ }
+ case RunPreprocessorOnly: { // Just lex as fast as we can, no output.
+ LexerToken Tok;
+ // Start parsing the specified input file.
+ PP.EnterSourceFile(MainFileID, 0, true);
+ do {
+ PP.Lex(Tok);
+ } while (Tok.getKind() != tok::eof);
+ break;
+ }
+
+ case PrintPreprocessedInput: // -E mode.
+ DoPrintPreprocessedInput(MainFileID, PP, LangInfo);
+ break;
+
+ case ParseNoop: // -parse-noop
+ ParseFile(PP, new MinimalAction(), MainFileID);
+ break;
+
+ case ParsePrintCallbacks:
+ ParseFile(PP, CreatePrintParserActionsAction(), MainFileID);
+ break;
+ case ParseSyntaxOnly: // -fsyntax-only
+ case ParseAST:
+ BuildASTs(PP, MainFileID, Stats);
+ break;
+ case ParseASTPrint:
+ PrintASTs(PP, MainFileID, Stats);
+ break;
+ case EmitLLVM:
+ EmitLLVMFromASTs(PP, MainFileID, Stats);
+ break;
+ case ParseASTCheck:
+ exit(CheckDiagnostics(PP, MainFileID));
+ break;
+ }
+
+ if (Stats) {
+ fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str());
+ PP.PrintStats();
+ PP.getIdentifierTable().PrintStats();
+ HeaderInfo.PrintStats();
+ fprintf(stderr, "\n");
+ }
+}
+
+static llvm::cl::list<std::string>
+InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>"));
+
+
+int main(int argc, char **argv) {
+ llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n");
+ llvm::sys::PrintStackTraceOnErrorSignal();
+
+ // If no input was specified, read from stdin.
+ if (InputFilenames.empty())
+ InputFilenames.push_back("-");
+
+ /// Create a SourceManager object. This tracks and owns all the file buffers
+ /// allocated to the program.
+ SourceManager SourceMgr;
+
+ // Create a file manager object to provide access to and cache the filesystem.
+ FileManager FileMgr;
+
+ // 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);
+
+ std::auto_ptr<TextDiagnostics> DiagClient;
+ if (ProgAction != ParseASTCheck) {
+ // 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,
+ "parse-ast-check only works on single input files for now.\n");
+ return 1;
+ }
+ }
+
+ // Configure our handling of diagnostics.
+ Diagnostic Diags(*DiagClient);
+ 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.
+ TargetInfo *Target = CreateTargetInfo(Diags);
+ if (Target == 0) {
+ fprintf(stderr,
+ "Sorry, don't know what target this is, please use -arch.\n");
+ exit(1);
+ }
+
+ // Process the -I options and set them in the HeaderInfo.
+ HeaderSearch HeaderInfo(FileMgr);
+ DiagClient->setHeaderSearch(HeaderInfo);
+ InitializeIncludePaths(HeaderInfo, FileMgr, Diags, LangInfo);
+
+ for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) {
+ // Set up the preprocessor with these options.
+ Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo);
+ DiagClient->setPreprocessor(PP);
+ const std::string &InFile = InputFilenames[i];
+ std::vector<char> PrologMacros;
+ unsigned MainFileID = InitializePreprocessor(PP, InFile, SourceMgr,
+ HeaderInfo, LangInfo,
+ PrologMacros);
+
+ if (!MainFileID) continue;
+
+ ProcessInputFile(PP, MainFileID, InFile, SourceMgr,
+ *DiagClient, HeaderInfo, LangInfo);
+ HeaderInfo.ClearFileInfo();
+ }
+
+ unsigned NumDiagnostics = Diags.getNumDiagnostics();
+
+ if (NumDiagnostics)
+ fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics,
+ (NumDiagnostics == 1 ? "" : "s"));
+
+ if (Stats) {
+ // Printed from high-to-low level.
+ SourceMgr.PrintStats();
+ FileMgr.PrintStats();
+ fprintf(stderr, "\n");
+ }
+
+ return Diags.getNumErrors();
+}
diff --git a/Driver/clang.h b/Driver/clang.h
new file mode 100644
index 0000000..717d888
--- /dev/null
+++ b/Driver/clang.h
@@ -0,0 +1,45 @@
+//===--- clang.h - 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 is the header file that pulls together the top-level driver.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_CLANG_H
+#define LLVM_CLANG_CLANG_H
+
+namespace clang {
+class Preprocessor;
+class LangOptions;
+class MinimalAction;
+class TargetInfo;
+class Diagnostic;
+
+/// DoPrintPreprocessedInput - Implement -E mode.
+void DoPrintPreprocessedInput(unsigned MainFileID, Preprocessor &PP,
+ const LangOptions &Options);
+
+/// CreatePrintParserActionsAction - Return the actions implementation that
+/// implements the -parse-print-callbacks option.
+MinimalAction *CreatePrintParserActionsAction();
+
+/// CreateTargetInfo - Return the set of target info objects as specified by
+/// the -arch command line option.
+TargetInfo *CreateTargetInfo(Diagnostic &Diags);
+
+/// EmitLLVMFromASTs - Implement -emit-llvm, which generates llvm IR from C.
+void EmitLLVMFromASTs(Preprocessor &PP, unsigned MainFileID,
+ bool PrintStats);
+
+/// CheckDiagnostics - Implement the -parse-ast-check diagnostic verifier.
+bool CheckDiagnostics(Preprocessor &PP, unsigned MainFileID);
+
+} // end namespace clang
+
+#endif