clang API Documentation

TextPathDiagnostics.cpp
Go to the documentation of this file.
00001 //===--- TextPathDiagnostics.cpp - Text Diagnostics for Paths ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the TextPathDiagnostics object.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/StaticAnalyzer/Core/PathDiagnosticConsumers.h"
00015 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
00016 #include "clang/Lex/Preprocessor.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 using namespace clang;
00019 using namespace ento;
00020 using namespace llvm;
00021 
00022 namespace {
00023 
00024 /// \brief Simple path diagnostic client used for outputting as diagnostic notes
00025 /// the sequence of events.
00026 class TextPathDiagnostics : public PathDiagnosticConsumer {
00027   const std::string OutputFile;
00028   DiagnosticsEngine &Diag;
00029 
00030 public:
00031   TextPathDiagnostics(const std::string& output, DiagnosticsEngine &diag)
00032     : OutputFile(output), Diag(diag) {}
00033 
00034   void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags,
00035                             SmallVectorImpl<std::string> *FilesMade);
00036   
00037   virtual StringRef getName() const {
00038     return "TextPathDiagnostics";
00039   }
00040 
00041   PathGenerationScheme getGenerationScheme() const { return Minimal; }
00042   bool supportsLogicalOpControlFlow() const { return true; }
00043   bool supportsAllBlockEdges() const { return true; }
00044   virtual bool useVerboseDescription() const { return true; }
00045 };
00046 
00047 } // end anonymous namespace
00048 
00049 PathDiagnosticConsumer*
00050 ento::createTextPathDiagnosticConsumer(const std::string& out,
00051                                      const Preprocessor &PP) {
00052   return new TextPathDiagnostics(out, PP.getDiagnostics());
00053 }
00054 
00055 void TextPathDiagnostics::FlushDiagnosticsImpl(
00056                               std::vector<const PathDiagnostic *> &Diags,
00057                               SmallVectorImpl<std::string> *FilesMade) {
00058   for (std::vector<const PathDiagnostic *>::iterator it = Diags.begin(),
00059        et = Diags.end(); it != et; ++it) {
00060     const PathDiagnostic *D = *it;
00061     for (PathPieces::const_iterator I = D->path.begin(), E = D->path.end(); 
00062          I != E; ++I) {
00063       unsigned diagID =
00064         Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Note,
00065                                                  (*I)->getString());
00066       Diag.Report((*I)->getLocation().asLocation(), diagID);
00067     }
00068   }
00069 }