diff options
Diffstat (limited to 'clang/lib/Frontend')
-rw-r--r-- | clang/lib/Frontend/CMakeLists.txt | 10 | ||||
-rw-r--r-- | clang/lib/Frontend/HTMLDiagnostics.cpp | 502 | ||||
-rw-r--r-- | clang/lib/Frontend/InitHeaderSearch.cpp | 326 | ||||
-rw-r--r-- | clang/lib/Frontend/Makefile | 22 | ||||
-rw-r--r-- | clang/lib/Frontend/ManagerRegistry.cpp | 20 | ||||
-rw-r--r-- | clang/lib/Frontend/PlistDiagnostics.cpp | 239 | ||||
-rw-r--r-- | clang/lib/Frontend/TextDiagnosticBuffer.cpp | 39 | ||||
-rw-r--r-- | clang/lib/Frontend/TextDiagnosticPrinter.cpp | 315 |
8 files changed, 1473 insertions, 0 deletions
diff --git a/clang/lib/Frontend/CMakeLists.txt b/clang/lib/Frontend/CMakeLists.txt new file mode 100644 index 00000000000..2ff67953f7e --- /dev/null +++ b/clang/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/clang/lib/Frontend/HTMLDiagnostics.cpp b/clang/lib/Frontend/HTMLDiagnostics.cpp new file mode 100644 index 00000000000..cb05e55a43a --- /dev/null +++ b/clang/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/clang/lib/Frontend/InitHeaderSearch.cpp b/clang/lib/Frontend/InitHeaderSearch.cpp new file mode 100644 index 00000000000..e7dd207abed --- /dev/null +++ b/clang/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/clang/lib/Frontend/Makefile b/clang/lib/Frontend/Makefile new file mode 100644 index 00000000000..d009cffea08 --- /dev/null +++ b/clang/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/clang/lib/Frontend/ManagerRegistry.cpp b/clang/lib/Frontend/ManagerRegistry.cpp new file mode 100644 index 00000000000..79f1e8178e1 --- /dev/null +++ b/clang/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/clang/lib/Frontend/PlistDiagnostics.cpp b/clang/lib/Frontend/PlistDiagnostics.cpp new file mode 100644 index 00000000000..4a87d5d7e50 --- /dev/null +++ b/clang/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){ + FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); + FIDMap::const_iterator I = FIDs.find(FID); + assert(I != FIDs.end()); + return I->second; +} + +static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { + for (unsigned i = 0; i < indent; ++i) + o << ' '; + return o; +} + +static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM, + SourceLocation L, const FIDMap& FM, + const unsigned indent) { + + Indent(o, indent) << "<dict>\n"; + Indent(o, indent) << " <key>line</key><integer>" + << SM->getInstantiationLineNumber(L) << "</integer>\n"; + Indent(o, indent) << " <key>col</key><integer>" + << SM->getInstantiationColumnNumber(L) << "</integer>\n"; + Indent(o, indent) << " <key>file</key><integer>" + << GetFID(FM, SM, L) << "</integer>\n"; + Indent(o, indent) << "</dict>\n"; +} + +static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R, + const FIDMap& FM, const unsigned indent) { + + Indent(o, indent) << "<array>\n"; + EmitLocation(o, SM, R.getBegin(), FM, indent+1); + EmitLocation(o, SM, R.getEnd(), FM, indent+1); + Indent(o, indent) << "</array>\n"; +} + +static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, + const FIDMap& FM, SourceManager* SM) { + + unsigned indent = 4; + Indent(o, indent) << "<dict>\n"; + ++indent; + + // Output the location. + FullSourceLoc L = P.getLocation(); + + Indent(o, indent) << "<key>location</key>\n"; + EmitLocation(o, SM, L, FM, indent); + + // Output the ranges (if any). + PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), + RE = P.ranges_end(); + + if (RI != RE) { + Indent(o, indent) << "<key>ranges</key>\n"; + Indent(o, indent) << "<array>\n"; + ++indent; + for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); + --indent; + Indent(o, indent) << "</array>\n"; + } + + // Output the text. + Indent(o, indent) << "<key>message</key>\n"; + Indent(o, indent) << "<string>" << P.getString() << "</string>\n"; + + // Output the hint. + Indent(o, indent) << "<key>displayhint</key>\n"; + Indent(o, indent) << "<string>" + << (P.getDisplayHint() == PathDiagnosticPiece::Above + ? "above" : "below") + << "</string>\n"; + + + // Finish up. + --indent; + Indent(o, indent); o << "</dict>\n"; +} + +void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { + if (!D) + return; + + if (D->empty()) { + delete D; + return; + } + + BatchedDiags.push_back(D); +} + +PlistDiagnostics::~PlistDiagnostics() { + + // Build up a set of FIDs that we use by scanning the locations and + // ranges of the diagnostics. + FIDMap FM; + llvm::SmallVector<FileID, 10> Fids; + SourceManager* SM = 0; + + if (!BatchedDiags.empty()) + SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager(); + + for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(), + DE = BatchedDiags.end(); DI != DE; ++DI) { + + const PathDiagnostic *D = *DI; + + for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) { + AddFID(FM, Fids, SM, I->getLocation()); + + for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), + RE=I->ranges_end(); RI!=RE; ++RI) { + AddFID(FM, Fids, SM, RI->getBegin()); + AddFID(FM, Fids, SM, RI->getEnd()); + } + } + } + + // Open the file. + std::string ErrMsg; + llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg); + if (!ErrMsg.empty()) { + llvm::errs() << "warning: could not creat file: " << OutputFile << '\n'; + return; + } + + // Write the plist header. + o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " + "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n"; + + // Write the root object: a <dict> containing... + // - "files", an <array> mapping from FIDs to file names + // - "diagnostics", an <array> containing the path diagnostics + o << "<dict>\n" + " <key>files</key>\n" + " <array>\n"; + + for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); + I!=E; ++I) + o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n"; + + o << " </array>\n" + " <key>diagnostics</key>\n" + " <array>\n"; + + for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(), + DE = BatchedDiags.end(); DI!=DE; ++DI) { + + o << " <dict>\n" + " <key>path</key>\n"; + + const PathDiagnostic *D = *DI; + // Create an owning smart pointer for 'D' just so that we auto-free it + // when we exit this method. + llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D)); + + o << " <array>\n"; + + for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) + ReportDiag(o, *I, FM, SM); + + o << " </array>\n"; + + // Output the bug type and bug category. + o << " <key>description</key>\n <string>" << D->getDescription() + << "</string>\n" + << " <key>category</key>\n <string>" << D->getCategory() + << "</string>\n" + << " </dict>\n"; + } + + o << " </array>\n"; + + // Finish. + o << "</dict>\n</plist>"; +} diff --git a/clang/lib/Frontend/TextDiagnosticBuffer.cpp b/clang/lib/Frontend/TextDiagnosticBuffer.cpp new file mode 100644 index 00000000000..a4518ee7e68 --- /dev/null +++ b/clang/lib/Frontend/TextDiagnosticBuffer.cpp @@ -0,0 +1,39 @@ +//===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file 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 "clang/Frontend/TextDiagnosticBuffer.h" +#include "llvm/ADT/SmallString.h" +using namespace clang; + +/// HandleDiagnostic - Store the errors, warnings, and notes that are +/// reported. +/// +void TextDiagnosticBuffer::HandleDiagnostic(Diagnostic::Level Level, + const DiagnosticInfo &Info) { + llvm::SmallString<100> StrC; + Info.FormatDiagnostic(StrC); + std::string Str(StrC.begin(), StrC.end()); + switch (Level) { + default: assert(0 && "Diagnostic not handled during diagnostic buffering!"); + case Diagnostic::Note: + Notes.push_back(std::make_pair(Info.getLocation(), Str)); + break; + case Diagnostic::Warning: + Warnings.push_back(std::make_pair(Info.getLocation(), Str)); + break; + case Diagnostic::Error: + case Diagnostic::Fatal: + Errors.push_back(std::make_pair(Info.getLocation(), Str)); + break; + } +} diff --git a/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/clang/lib/Frontend/TextDiagnosticPrinter.cpp new file mode 100644 index 00000000000..939a65f406d --- /dev/null +++ b/clang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -0,0 +1,315 @@ +//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This diagnostic client prints out their diagnostic messages. +// +//===----------------------------------------------------------------------===// + +#include "clang/Frontend/TextDiagnosticPrinter.h" +#include "clang/Basic/SourceManager.h" +#include "clang/Lex/Lexer.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/ADT/SmallString.h" +#include <algorithm> +using namespace clang; + +void TextDiagnosticPrinter:: +PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { + if (Loc.isInvalid()) return; + + PresumedLoc PLoc = SM.getPresumedLoc(Loc); + + // Print out the other include frames first. + PrintIncludeStack(PLoc.getIncludeLoc(), SM); + + OS << "In file included from " << PLoc.getFilename() + << ':' << PLoc.getLine() << ":\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, + const SourceManager &SM, + unsigned LineNo, FileID FID, + std::string &CaretLine, + const std::string &SourceLine) { + assert(CaretLine.size() == SourceLine.size() && + "Expect a correspondence between source and caret line!"); + if (!R.isValid()) return; + + SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); + SourceLocation End = SM.getInstantiationLoc(R.getEnd()); + + // If the End location and the start location are the same and are a macro + // location, then the range was something that came from a macro expansion + // or _Pragma. If this is an object-like macro, the best we can do is to + // highlight the range. If this is a function-like macro, we'd also like to + // highlight the arguments. + if (Begin == End && R.getEnd().isMacroID()) + End = SM.getInstantiationRange(R.getEnd()).second; + + unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); + if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) + return; // No intersection. + + unsigned EndLineNo = SM.getInstantiationLineNumber(End); + if (EndLineNo < LineNo || SM.getFileID(End) != FID) + return; // No intersection. + + // Compute the column number of the start. + unsigned StartColNo = 0; + if (StartLineNo == LineNo) { + StartColNo = SM.getInstantiationColumnNumber(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 = CaretLine.size(); + if (EndLineNo == LineNo) { + EndColNo = SM.getInstantiationColumnNumber(End); + if (EndColNo) { + --EndColNo; // Zero base the col #. + + // Add in the length of the token, so that we cover multi-char tokens. + EndColNo += Lexer::MeasureTokenLength(End, SM); + } else { + EndColNo = CaretLine.size(); + } + } + + // Pick the last non-whitespace column. + if (EndColNo <= SourceLine.size()) + while (EndColNo-1 && + (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) + --EndColNo; + else + EndColNo = SourceLine.size(); + + // Fill the range with ~'s. + assert(StartColNo <= EndColNo && "Invalid range!"); + for (unsigned i = StartColNo; i < EndColNo; ++i) + CaretLine[i] = '~'; +} + +void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, + SourceRange *Ranges, + unsigned NumRanges, + SourceManager &SM, + const CodeModificationHint *Hints, + unsigned NumHints) { + assert(!Loc.isInvalid() && "must have a valid source location here"); + + // We always emit diagnostics about the instantiation points, not the spelling + // points. This more closely correlates to what the user writes. + if (!Loc.isFileID()) { + SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; + EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM); + + // Map the location through the macro. + Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc)); + + // Map the ranges. + for (unsigned i = 0; i != NumRanges; ++i) { + SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); + if (S.isMacroID()) + S = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(S)); + if (E.isMacroID()) + E = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(E)); + Ranges[i] = SourceRange(S, E); + } + + // Emit the file/line/column that this expansion came from. + OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc) + << ':'; + if (ShowColumn) + OS << SM.getInstantiationColumnNumber(Loc) << ':'; + OS << " note: instantiated from:\n"; + } + + // Decompose the location into a FID/Offset pair. + std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); + FileID FID = LocInfo.first; + unsigned FileOffset = LocInfo.second; + + // Get information about the buffer it points into. + std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); + const char *BufStart = BufferInfo.first; + const char *BufEnd = BufferInfo.second; + + unsigned ColNo = SM.getColumnNumber(FID, FileOffset); + + // Rewind from the current position to the start of the line. + const char *TokPtr = BufStart+FileOffset; + const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. + + + // Compute the line end. Scan forward from the error position to the end of + // the line. + const char *LineEnd = TokPtr; + while (LineEnd != BufEnd && + *LineEnd != '\n' && *LineEnd != '\r') + ++LineEnd; + + // Copy the line of code into an std::string for ease of manipulation. + std::string SourceLine(LineStart, LineEnd); + + // Create a line for the caret that is filled with spaces that is the same + // length as the line of source code. + std::string CaretLine(LineEnd-LineStart, ' '); + + // Highlight all of the characters covered by Ranges with ~ characters. + if (NumRanges) { + unsigned LineNo = SM.getLineNumber(FID, FileOffset); + + for (unsigned i = 0, e = NumRanges; i != e; ++i) + HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); + } + + // Next, insert the caret itself. + if (ColNo-1 < CaretLine.size()) + CaretLine[ColNo-1] = '^'; + else + CaretLine.push_back('^'); + + // Scan the source line, looking for tabs. If we find any, manually expand + // them to 8 characters and update the CaretLine 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 CaretLine. + CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); + } + + // Finally, remove any blank spaces from the end of CaretLine. + while (CaretLine[CaretLine.size()-1] == ' ') + CaretLine.erase(CaretLine.end()-1); + + // Emit what we have computed. + OS << SourceLine << '\n'; + OS << CaretLine << '\n'; + + if (NumHints) { + std::string InsertionLine; + for (const CodeModificationHint *Hint = Hints, + *LastHint = Hints + NumHints; + Hint != LastHint; ++Hint) { + if (Hint->InsertionLoc.isValid()) { + // We have an insertion hint. Determine whether the inserted + // code is on the same line as the caret. + std::pair<FileID, unsigned> HintLocInfo + = SM.getDecomposedLoc(Hint->InsertionLoc); + if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) == + SM.getLineNumber(FID, FileOffset)) { + // Insert the new code into the line just below the code + // that the user wrote. + unsigned HintColNo + = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second); + unsigned LastColumnModified + = HintColNo - 1 + Hint->CodeToInsert.size(); + if (LastColumnModified > InsertionLine.size()) + InsertionLine.resize(LastColumnModified, ' '); + std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), + InsertionLine.begin() + HintColNo - 1); + } + } + } + + if (!InsertionLine.empty()) + OS << InsertionLine << '\n'; + } +} + + +void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, + const DiagnosticInfo &Info) { + // If the location is specified, print out a file/line/col and include trace + // if enabled. + if (Info.getLocation().isValid()) { + const SourceManager &SM = Info.getLocation().getManager(); + PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); + unsigned LineNo = PLoc.getLine(); + + // First, if this diagnostic is not in the main file, print out the + // "included from" lines. + if (LastWarningLoc != PLoc.getIncludeLoc()) { + LastWarningLoc = PLoc.getIncludeLoc(); + PrintIncludeStack(LastWarningLoc, SM); + } + + // Compute the column number. + if (ShowLocation) { + OS << PLoc.getFilename() << ':' << LineNo << ':'; + if (ShowColumn) + if (unsigned ColNo = PLoc.getColumn()) + OS << ColNo << ':'; + OS << ' '; + } + } + + switch (Level) { + case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); + case Diagnostic::Note: OS << "note: "; break; + case Diagnostic::Warning: OS << "warning: "; break; + case Diagnostic::Error: OS << "error: "; break; + case Diagnostic::Fatal: OS << "fatal error: "; break; + } + + llvm::SmallString<100> OutStr; + Info.FormatDiagnostic(OutStr); + OS.write(OutStr.begin(), OutStr.size()); + OS << '\n'; + + // If caret diagnostics are enabled and we have location, we want to emit the + // caret. However, we only do this if the location moved from the last + // diagnostic, or if the diagnostic has ranges. We don't want to emit the + // same caret multiple times if one loc has multiple diagnostics. + if (CaretDiagnostics && Info.getLocation().isValid() && + ((LastLoc != Info.getLocation()) || Info.getNumRanges() || + Info.getNumCodeModificationHints())) { + // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. + LastLoc = Info.getLocation(); + + // Get the ranges into a local array we can hack on. + SourceRange Ranges[20]; + unsigned NumRanges = Info.getNumRanges(); + assert(NumRanges < 20 && "Out of space"); + for (unsigned i = 0; i != NumRanges; ++i) + Ranges[i] = Info.getRange(i); + + unsigned NumHints = Info.getNumCodeModificationHints(); + for (unsigned idx = 0; idx < NumHints; ++idx) { + const CodeModificationHint &Hint = Info.getCodeModificationHint(idx); + if (Hint.RemoveRange.isValid()) { + assert(NumRanges < 20 && "Out of space"); + Ranges[NumRanges++] = Hint.RemoveRange; + } + } + + EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), + Info.getCodeModificationHints(), + Info.getNumCodeModificationHints()); + } + + OS.flush(); +} |