diff options
Diffstat (limited to 'lib/Frontend')
-rw-r--r-- | lib/Frontend/CMakeLists.txt | 10 | ||||
-rw-r--r-- | lib/Frontend/HTMLDiagnostics.cpp | 502 | ||||
-rw-r--r-- | lib/Frontend/InitHeaderSearch.cpp | 326 | ||||
-rw-r--r-- | lib/Frontend/Makefile | 22 | ||||
-rw-r--r-- | lib/Frontend/ManagerRegistry.cpp | 20 | ||||
-rw-r--r-- | lib/Frontend/PlistDiagnostics.cpp | 239 | ||||
-rw-r--r-- | lib/Frontend/TextDiagnosticBuffer.cpp | 39 | ||||
-rw-r--r-- | lib/Frontend/TextDiagnosticPrinter.cpp | 315 |
8 files changed, 1473 insertions, 0 deletions
diff --git a/lib/Frontend/CMakeLists.txt b/lib/Frontend/CMakeLists.txt new file mode 100644 index 0000000000..2ff67953f7 --- /dev/null +++ b/lib/Frontend/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_NO_RTTI 1) + +add_clang_library(clangDriver + HTMLDiagnostics.cpp + InitHeaderSearch.cpp + TextDiagnosticBuffer.cpp + TextDiagnosticPrinter.cpp + PlistDiagnostics.cpp + ManagerRegistry.cpp + ) diff --git a/lib/Frontend/HTMLDiagnostics.cpp b/lib/Frontend/HTMLDiagnostics.cpp new file mode 100644 index 0000000000..cb05e55a43 --- /dev/null +++ b/lib/Frontend/HTMLDiagnostics.cpp @@ -0,0 +1,502 @@ +//===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the HTMLDiagnostics object. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/PathDiagnosticClients.h" +#include "clang/Analysis/PathDiagnostic.h" +#include "clang/AST/ASTContext.h" +#include "clang/AST/Decl.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/FileManager.h" +#include "clang/Rewrite/Rewriter.h" +#include "clang/Rewrite/HTMLRewrite.h" +#include "clang/Lex/Lexer.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Streams.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/System/Path.h" +#include <fstream> +using namespace clang; + +//===----------------------------------------------------------------------===// +// Boilerplate. +//===----------------------------------------------------------------------===// + +namespace { + +class VISIBILITY_HIDDEN HTMLDiagnostics : public PathDiagnosticClient { + llvm::sys::Path Directory, FilePrefix; + bool createdDir, noDir; + Preprocessor* PP; + PreprocessorFactory* PPF; + std::vector<const PathDiagnostic*> BatchedDiags; +public: + HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, + PreprocessorFactory* ppf); + + virtual ~HTMLDiagnostics(); + + virtual void HandlePathDiagnostic(const PathDiagnostic* D); + + void HandlePiece(Rewriter& R, FileID BugFileID, + const PathDiagnosticPiece& P, unsigned num, unsigned max); + + void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, + const char *HighlightStart = "<span class=\"mrange\">", + const char *HighlightEnd = "</span>"); + + void ReportDiag(const PathDiagnostic& D); +}; + +} // end anonymous namespace + +HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, Preprocessor* pp, + PreprocessorFactory* ppf) + : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), + PP(pp), PPF(ppf) { + + // All html files begin with "report" + FilePrefix.appendComponent("report"); +} + +PathDiagnosticClient* +clang::CreateHTMLDiagnosticClient(const std::string& prefix, Preprocessor* PP, + PreprocessorFactory* PPF) { + + return new HTMLDiagnostics(prefix, PP, PPF); +} + +//===----------------------------------------------------------------------===// +// Report processing. +//===----------------------------------------------------------------------===// + +void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { + if (!D) + return; + + if (D->empty()) { + delete D; + return; + } + + BatchedDiags.push_back(D); +} + +HTMLDiagnostics::~HTMLDiagnostics() { + + while (!BatchedDiags.empty()) { + const PathDiagnostic* D = BatchedDiags.back(); + BatchedDiags.pop_back(); + ReportDiag(*D); + delete D; + } +} + +void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D) { + + // Create the HTML directory if it is missing. + + if (!createdDir) { + createdDir = true; + std::string ErrorMsg; + Directory.createDirectoryOnDisk(true, &ErrorMsg); + + if (!Directory.isDirectory()) { + llvm::cerr << "warning: could not create directory '" + << Directory.toString() << "'\n" + << "reason: " << ErrorMsg << '\n'; + + noDir = true; + + return; + } + } + + if (noDir) + return; + + SourceManager &SMgr = D.begin()->getLocation().getManager(); + FileID FID; + + // Verify that the entire path is from the same FileID. + for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) { + FullSourceLoc L = I->getLocation().getInstantiationLoc(); + + if (FID.isInvalid()) { + FID = SMgr.getFileID(L); + } else if (SMgr.getFileID(L) != FID) + return; // FIXME: Emit a warning? + + // Check the source ranges. + for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), + RE=I->ranges_end(); RI!=RE; ++RI) { + + SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin()); + + if (!L.isFileID() || SMgr.getFileID(L) != FID) + return; // FIXME: Emit a warning? + + L = SMgr.getInstantiationLoc(RI->getEnd()); + + if (!L.isFileID() || SMgr.getFileID(L) != FID) + return; // FIXME: Emit a warning? + } + } + + if (FID.isInvalid()) + return; // FIXME: Emit a warning? + + // Create a new rewriter to generate HTML. + Rewriter R(SMgr); + + // Process the path. + + unsigned n = D.size(); + unsigned max = n; + + for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); + I!=E; ++I, --n) { + + HandlePiece(R, FID, *I, n, max); + } + + // Add line numbers, header, footer, etc. + + // unsigned FID = R.getSourceMgr().getMainFileID(); + html::EscapeText(R, FID); + html::AddLineNumbers(R, FID); + + // If we have a preprocessor, relex the file and syntax highlight. + // We might not have a preprocessor if we come from a deserialized AST file, + // for example. + + if (PP) html::SyntaxHighlight(R, FID, *PP); + + // FIXME: We eventually want to use PPF to create a fresh Preprocessor, + // once we have worked out the bugs. + // + // if (PPF) html::HighlightMacros(R, FID, *PPF); + // + if (PP) html::HighlightMacros(R, FID, *PP); + + // Get the full directory name of the analyzed file. + + const FileEntry* Entry = SMgr.getFileEntryForID(FID); + + // This is a cludge; basically we want to append either the full + // working directory if we have no directory information. This is + // a work in progress. + + std::string DirName = ""; + + if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { + llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); + DirName = P.toString() + "/"; + } + + // Add the name of the file as an <h1> tag. + + { + std::string s; + llvm::raw_string_ostream os(s); + + os << "<!-- REPORTHEADER -->\n" + << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" + "<tr><td class=\"rowname\">File:</td><td>" + << html::EscapeText(DirName) + << html::EscapeText(Entry->getName()) + << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" + "<a href=\"#EndPath\">line " + << (*D.rbegin()).getLocation().getInstantiationLineNumber() + << ", column " + << (*D.rbegin()).getLocation().getInstantiationColumnNumber() + << "</a></td></tr>\n" + "<tr><td class=\"rowname\">Description:</td><td>" + << D.getDescription() << "</td></tr>\n"; + + // Output any other meta data. + + for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); + I!=E; ++I) { + os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; + } + + os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n" + "<h3>Annotated Source Code</h3>\n"; + + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + // Embed meta-data tags. + + const std::string& BugDesc = D.getDescription(); + + if (!BugDesc.empty()) { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + const std::string& BugType = D.getBugType(); + if (!BugType.empty()) { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGTYPE " << BugType << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + const std::string& BugCategory = D.getCategory(); + + if (!BugCategory.empty()) { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGLINE " + << D.back()->getLocation().getInstantiationLineNumber() << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + { + std::string s; + llvm::raw_string_ostream os(s); + os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; + R.InsertStrBefore(SMgr.getLocForStartOfFile(FID), os.str()); + } + + // Add CSS, header, and footer. + + html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); + + // Get the rewrite buffer. + const RewriteBuffer *Buf = R.getRewriteBufferFor(FID); + + if (!Buf) { + llvm::cerr << "warning: no diagnostics generated for main file.\n"; + return; + } + + // Create the stream to write out the HTML. + std::ofstream os; + + { + // Create a path for the target HTML file. + llvm::sys::Path F(FilePrefix); + F.makeUnique(false, NULL); + + // Rename the file with an HTML extension. + llvm::sys::Path H(F); + H.appendSuffix("html"); + F.renamePathOnDisk(H, NULL); + + os.open(H.toString().c_str()); + + if (!os) { + llvm::cerr << "warning: could not create file '" << F.toString() << "'\n"; + return; + } + } + + // Emit the HTML to disk. + + for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) + os << *I; +} + +void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID, + const PathDiagnosticPiece& P, + unsigned num, unsigned max) { + + // For now, just draw a box above the line in question, and emit the + // warning. + FullSourceLoc Pos = P.getLocation(); + + if (!Pos.isValid()) + return; + + SourceManager &SM = R.getSourceMgr(); + assert(&Pos.getManager() == &SM && "SourceManagers are different!"); + std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos); + + if (LPosInfo.first != BugFileID) + return; + + const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first); + const char* FileStart = Buf->getBufferStart(); + + // Compute the column number. Rewind from the current position to the start + // of the line. + unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); + const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData(); + const char *LineStart = TokInstantiationPtr-ColNo; + + // Compute LineEnd. + const char *LineEnd = TokInstantiationPtr; + const char* FileEnd = Buf->getBufferEnd(); + while (*LineEnd != '\n' && LineEnd != FileEnd) + ++LineEnd; + + // Compute the margin offset by counting tabs and non-tabs. + unsigned PosNo = 0; + for (const char* c = LineStart; c != TokInstantiationPtr; ++c) + PosNo += *c == '\t' ? 8 : 1; + + // Create the html for the message. + { + // Get the string and determining its maximum substring. + const std::string& Msg = P.getString(); + unsigned max_token = 0; + unsigned cnt = 0; + unsigned len = Msg.size(); + + for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I) + switch (*I) { + default: + ++cnt; + continue; + case ' ': + case '\t': + case '\n': + if (cnt > max_token) max_token = cnt; + cnt = 0; + } + + if (cnt > max_token) max_token = cnt; + + // Next, determine the approximate size of the message bubble in em. + unsigned em; + const unsigned max_line = 120; + + if (max_token >= max_line) + em = max_token / 2; + else { + unsigned characters = max_line; + unsigned lines = len / max_line; + + if (lines > 0) { + for (; characters > max_token; --characters) + if (len / characters > lines) { + ++characters; + break; + } + } + + em = characters / 2; + } + + // Now generate the message bubble. + std::string s; + llvm::raw_string_ostream os(s); + + os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; + + if (num == max) + os << "EndPath"; + else + os << "Path" << num; + + os << "\" class=\"msg\" style=\"margin-left:" << PosNo << "ex"; + if (em < max_line/2) os << "; max-width:" << em << "em"; + os << "\">"; + + if (max > 1) + os << "<span class=\"PathIndex\">[" << num << "]</span> "; + + os << html::EscapeText(Msg) << "</div></td></tr>"; + + // Insert the new html. + unsigned DisplayPos = LineEnd - FileStart; + SourceLocation Loc = + SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos); + + R.InsertStrBefore(Loc, os.str()); + } + + // Now highlight the ranges. + + for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); + I != E; ++I) + HighlightRange(R, LPosInfo.first, *I); + +#if 0 + // If there is a code insertion hint, insert that code. + // FIXME: This code is disabled because it seems to mangle the HTML + // output. I'm leaving it here because it's generally the right idea, + // but needs some help from someone more familiar with the rewriter. + for (const CodeModificationHint *Hint = P.code_modifications_begin(), + *HintEnd = P.code_modifications_end(); + Hint != HintEnd; ++Hint) { + if (Hint->RemoveRange.isValid()) { + HighlightRange(R, LPosInfo.first, Hint->RemoveRange, + "<span class=\"CodeRemovalHint\">", "</span>"); + } + if (Hint->InsertionLoc.isValid()) { + std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true); + EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode + + "</span>"; + R.InsertStrBefore(Hint->InsertionLoc, EscapedCode); + } + } +#endif +} + +void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, + SourceRange Range, + const char *HighlightStart, + const char *HighlightEnd) { + + SourceManager& SM = R.getSourceMgr(); + + SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin()); + unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart); + + SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd()); + unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd); + + if (EndLineNo < StartLineNo) + return; + + if (SM.getFileID(InstantiationStart) != BugFileID || + SM.getFileID(InstantiationEnd) != BugFileID) + return; + + // Compute the column number of the end. + unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd); + unsigned OldEndColNo = EndColNo; + + if (EndColNo) { + // Add in the length of the token, so that we cover multi-char tokens. + EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM) - 1; + } + + // Highlight the range. Make the span tag the outermost tag for the + // selected range. + + SourceLocation E = + InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo); + + html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); +} diff --git a/lib/Frontend/InitHeaderSearch.cpp b/lib/Frontend/InitHeaderSearch.cpp new file mode 100644 index 0000000000..e7dd207abe --- /dev/null +++ b/lib/Frontend/InitHeaderSearch.cpp @@ -0,0 +1,326 @@ +//===--- InitHeaderSearch.cpp - Initialize header search paths ----------*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the InitHeaderSearch class. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/InitHeaderSearch.h" +#include "clang/Lex/HeaderSearch.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/LangOptions.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/ADT/SmallPtrSet.h" +#include "llvm/System/Path.h" +#include "llvm/Config/config.h" +#include <vector> +using namespace clang; + +void InitHeaderSearch::AddPath(const std::string &Path, IncludeDirGroup Group, + bool isCXXAware, bool isUserSupplied, + bool isFramework, bool IgnoreSysRoot) { + assert(!Path.empty() && "can't handle empty path here"); + FileManager &FM = Headers.getFileMgr(); + + // Compute the actual path, taking into consideration -isysroot. + llvm::SmallString<256> MappedPath; + + // Handle isysroot. + if (Group == System && !IgnoreSysRoot) { + // FIXME: Portability. This should be a sys::Path interface, this doesn't + // handle things like C:\ right, nor win32 \\network\device\blah. + if (isysroot.size() != 1 || isysroot[0] != '/') // Add isysroot if present. + MappedPath.append(isysroot.begin(), isysroot.end()); + } + + MappedPath.append(Path.begin(), Path.end()); + + // Compute the DirectoryLookup type. + SrcMgr::CharacteristicKind Type; + if (Group == Quoted || Group == Angled) + Type = SrcMgr::C_User; + else if (isCXXAware) + Type = SrcMgr::C_System; + else + Type = SrcMgr::C_ExternCSystem; + + + // If the directory exists, add it. + if (const DirectoryEntry *DE = FM.getDirectory(&MappedPath[0], + &MappedPath[0]+ + MappedPath.size())) { + IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, + isFramework)); + return; + } + + // Check to see if this is an apple-style headermap (which are not allowed to + // be frameworks). + if (!isFramework) { + if (const FileEntry *FE = FM.getFile(&MappedPath[0], + &MappedPath[0]+MappedPath.size())) { + if (const HeaderMap *HM = Headers.CreateHeaderMap(FE)) { + // It is a headermap, add it to the search path. + IncludeGroup[Group].push_back(DirectoryLookup(HM, Type,isUserSupplied)); + return; + } + } + } + + if (Verbose) + fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", + MappedPath.c_str()); +} + + +void InitHeaderSearch::AddEnvVarPaths(const char *Name) { + const char* at = getenv(Name); + if (!at || *at == 0) // Empty string should not add '.' path. + return; + + const char* delim = strchr(at, llvm::sys::PathSeparator); + while (delim != 0) { + if (delim-at == 0) + AddPath(".", Angled, false, true, false); + else + AddPath(std::string(at, std::string::size_type(delim-at)), Angled, false, + true, false); + at = delim + 1; + delim = strchr(at, llvm::sys::PathSeparator); + } + if (*at == 0) + AddPath(".", Angled, false, true, false); + else + AddPath(at, Angled, false, true, false); +} + + +void InitHeaderSearch::AddDefaultSystemIncludePaths(const LangOptions &Lang) { + // FIXME: temporary hack: hard-coded paths. + // FIXME: get these from the target? + +#ifdef LLVM_ON_WIN32 + if (Lang.CPlusPlus) { + // Mingw32 GCC version 4 + AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++", + System, true, false, false); + AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/mingw32", + System, true, false, false); + AddPath("c:/mingw/lib/gcc/mingw32/4.3.0/include/c++/backward", + System, true, false, false); + } + + // Mingw32 GCC version 4 + AddPath("C:/mingw/include", System, false, false, false); +#else + + if (Lang.CPlusPlus) { + AddPath("/usr/include/c++/4.2.1", System, true, false, false); + AddPath("/usr/include/c++/4.2.1/i686-apple-darwin10", System, true, false, + false); + AddPath("/usr/include/c++/4.2.1/backward", System, true, false, false); + + AddPath("/usr/include/c++/4.0.0", System, true, false, false); + AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false, + false); + AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false); + + // Ubuntu 7.10 - Gutsy Gibbon + AddPath("/usr/include/c++/4.1.3", System, true, false, false); + AddPath("/usr/include/c++/4.1.3/i486-linux-gnu", System, true, false, + false); + AddPath("/usr/include/c++/4.1.3/backward", System, true, false, false); + + // Fedora 8 + AddPath("/usr/include/c++/4.1.2", System, true, false, false); + AddPath("/usr/include/c++/4.1.2/i386-redhat-linux", System, true, false, + false); + AddPath("/usr/include/c++/4.1.2/backward", System, true, false, false); + + // Fedora 9 + AddPath("/usr/include/c++/4.3.0", System, true, false, false); + AddPath("/usr/include/c++/4.3.0/i386-redhat-linux", System, true, false, + false); + AddPath("/usr/include/c++/4.3.0/backward", System, true, false, false); + + // Fedora 10 + AddPath("/usr/include/c++/4.3.2", System, true, false, false); + AddPath("/usr/include/c++/4.3.2/i386-redhat-linux", System, true, false, + false); + AddPath("/usr/include/c++/4.3.2/backward", System, true, false, false); + + // Arch Linux 2008-06-24 + AddPath("/usr/include/c++/4.3.1", System, true, false, false); + AddPath("/usr/include/c++/4.3.1/i686-pc-linux-gnu", System, true, false, + false); + AddPath("/usr/include/c++/4.3.1/backward", System, true, false, false); + AddPath("/usr/include/c++/4.3.1/x86_64-unknown-linux-gnu", System, true, + false, false); + + // Gentoo x86 stable + AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4", System, + true, false, false); + AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/" + "i686-pc-linux-gnu", System, true, false, false); + AddPath("/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4/backward", + System, true, false, false); + + // DragonFly + AddPath("/usr/include/c++/4.1", System, true, false, false); + + // FreeBSD + AddPath("/usr/include/c++/4.2", System, true, false, false); + } + + AddPath("/usr/local/include", System, false, false, false); + + AddPath("/usr/include", System, false, false, false); + AddPath("/System/Library/Frameworks", System, true, false, true); + AddPath("/Library/Frameworks", System, true, false, true); +#endif +} + +void InitHeaderSearch::AddDefaultEnvVarPaths(const LangOptions &Lang) { + AddEnvVarPaths("CPATH"); + if (Lang.CPlusPlus && Lang.ObjC1) + AddEnvVarPaths("OBJCPLUS_INCLUDE_PATH"); + else if (Lang.CPlusPlus) + AddEnvVarPaths("CPLUS_INCLUDE_PATH"); + else if (Lang.ObjC1) + AddEnvVarPaths("OBJC_INCLUDE_PATH"); + else + AddEnvVarPaths("C_INCLUDE_PATH"); +} + + +/// RemoveDuplicates - If there are duplicate directory entries in the specified +/// search list, remove the later (dead) ones. +static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList, + bool Verbose) { + llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenDirs; + llvm::SmallPtrSet<const DirectoryEntry *, 8> SeenFrameworkDirs; + llvm::SmallPtrSet<const HeaderMap *, 8> SeenHeaderMaps; + for (unsigned i = 0; i != SearchList.size(); ++i) { + unsigned DirToRemove = i; + + const DirectoryLookup &CurEntry = SearchList[i]; + + if (CurEntry.isNormalDir()) { + // If this isn't the first time we've seen this dir, remove it. + if (SeenDirs.insert(CurEntry.getDir())) + continue; + } else if (CurEntry.isFramework()) { + // If this isn't the first time we've seen this framework dir, remove it. + if (SeenFrameworkDirs.insert(CurEntry.getFrameworkDir())) + continue; + } else { + assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); + // If this isn't the first time we've seen this headermap, remove it. + if (SeenHeaderMaps.insert(CurEntry.getHeaderMap())) + continue; + } + + // If we have a normal #include dir/framework/headermap that is shadowed + // later in the chain by a system include location, we actually want to + // ignore the user's request and drop the user dir... keeping the system + // dir. This is weird, but required to emulate GCC's search path correctly. + // + // Since dupes of system dirs are rare, just rescan to find the original + // that we're nuking instead of using a DenseMap. + if (CurEntry.getDirCharacteristic() != SrcMgr::C_User) { + // Find the dir that this is the same of. + unsigned FirstDir; + for (FirstDir = 0; ; ++FirstDir) { + assert(FirstDir != i && "Didn't find dupe?"); + + const DirectoryLookup &SearchEntry = SearchList[FirstDir]; + + // If these are different lookup types, then they can't be the dupe. + if (SearchEntry.getLookupType() != CurEntry.getLookupType()) + continue; + + bool isSame; + if (CurEntry.isNormalDir()) + isSame = SearchEntry.getDir() == CurEntry.getDir(); + else if (CurEntry.isFramework()) + isSame = SearchEntry.getFrameworkDir() == CurEntry.getFrameworkDir(); + else { + assert(CurEntry.isHeaderMap() && "Not a headermap or normal dir?"); + isSame = SearchEntry.getHeaderMap() == CurEntry.getHeaderMap(); + } + + if (isSame) + break; + } + + // If the first dir in the search path is a non-system dir, zap it + // instead of the system one. + if (SearchList[FirstDir].getDirCharacteristic() == SrcMgr::C_User) + DirToRemove = FirstDir; + } + + if (Verbose) { + fprintf(stderr, "ignoring duplicate directory \"%s\"\n", + CurEntry.getName()); + if (DirToRemove != i) + fprintf(stderr, " as it is a non-system directory that duplicates" + " a system directory\n"); + } + + // This is reached if the current entry is a duplicate. Remove the + // DirToRemove (usually the current dir). + SearchList.erase(SearchList.begin()+DirToRemove); + --i; + } +} + + +void InitHeaderSearch::Realize() { + // 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, Verbose); + RemoveDuplicates(IncludeGroup[Quoted], Verbose); + + // 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"); + const char *Name = SearchList[i].getName(); + const char *Suffix; + if (SearchList[i].isNormalDir()) + Suffix = ""; + else if (SearchList[i].isFramework()) + Suffix = " (framework directory)"; + else { + assert(SearchList[i].isHeaderMap() && "Unknown DirectoryLookup"); + Suffix = " (headermap)"; + } + fprintf(stderr, " %s%s\n", Name, Suffix); + } + fprintf(stderr, "End of search list.\n"); + } +} + diff --git a/lib/Frontend/Makefile b/lib/Frontend/Makefile new file mode 100644 index 0000000000..d009cffea0 --- /dev/null +++ b/lib/Frontend/Makefile @@ -0,0 +1,22 @@ +##===- clang/lib/Analysis/Makefile -------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## +# +# This implements analyses built on top of source-level CFGs. +# +##===----------------------------------------------------------------------===## + +LEVEL = ../../../.. +LIBRARYNAME := clangFrontend +BUILD_ARCHIVE = 1 +CXXFLAGS = -fno-rtti + +CPPFLAGS += -I$(PROJ_SRC_DIR)/../../include + +include $(LEVEL)/Makefile.common + diff --git a/lib/Frontend/ManagerRegistry.cpp b/lib/Frontend/ManagerRegistry.cpp new file mode 100644 index 0000000000..79f1e8178e --- /dev/null +++ b/lib/Frontend/ManagerRegistry.cpp @@ -0,0 +1,20 @@ +//===- ManagerRegistry.cpp - Pluggble Analyzer module creators --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the pluggable analyzer module creators. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/ManagerRegistry.h" + +using namespace clang; + +StoreManagerCreator ManagerRegistry::StoreMgrCreator = 0; + +ConstraintManagerCreator ManagerRegistry::ConstraintMgrCreator = 0; diff --git a/lib/Frontend/PlistDiagnostics.cpp b/lib/Frontend/PlistDiagnostics.cpp new file mode 100644 index 0000000000..4a87d5d7e5 --- /dev/null +++ b/lib/Frontend/PlistDiagnostics.cpp @@ -0,0 +1,239 @@ +//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the PlistDiagnostics object. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/PathDiagnosticClients.h" +#include "clang/Analysis/PathDiagnostic.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Basic/FileManager.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/System/Path.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/SmallVector.h" +using namespace clang; + +typedef llvm::DenseMap<FileID, unsigned> FIDMap; + +namespace clang { + class Preprocessor; + class PreprocessorFactory; +} + +namespace { + class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { + std::vector<const PathDiagnostic*> BatchedDiags; + const std::string OutputFile; + public: + PlistDiagnostics(const std::string& prefix); + ~PlistDiagnostics(); + void HandlePathDiagnostic(const PathDiagnostic* D); + }; +} // end anonymous namespace + +PlistDiagnostics::PlistDiagnostics(const std::string& output) + : OutputFile(output) {} + +PathDiagnosticClient* +clang::CreatePlistDiagnosticClient(const std::string& s, + Preprocessor*, PreprocessorFactory*) { + return new PlistDiagnostics(s); +} + +static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V, + SourceManager* SM, SourceLocation L) { + + FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); + FIDMap::iterator I = FIDs.find(FID); + if (I != FIDs.end()) return; + FIDs[FID] = V.size(); + V.push_back(FID); +} + +static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){ |