blob: 0ccb9006451e60e9aab17fc94fbf0d9c29c9ee7b [file] [log] [blame]
Ted Kremenekd3abcdf2008-03-27 03:49:32 +00001//===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PathDiagnostic-related interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathDiagnostic.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000016#include "llvm/Support/Casting.h"
Ted Kremenek120187d2008-03-27 06:16:40 +000017#include <sstream>
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000018using namespace clang;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000019using llvm::dyn_cast;
20using llvm::isa;
21
22bool PathDiagnosticMacroPiece::containsEvent() const {
23 for (const_iterator I = begin(), E = end(); I!=E; ++I) {
24 if (isa<PathDiagnosticEventPiece>(*I))
25 return true;
26
27 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
28 if (MP->containsEvent())
29 return true;
30 }
31
32 return false;
33}
Ted Kremenek8af29752009-02-26 21:30:32 +000034
35static size_t GetNumCharsToLastNonPeriod(const char *s) {
36 const char *start = s;
37 const char *lastNonPeriod = 0;
38
39 for ( ; *s != '\0' ; ++s)
40 if (*s != '.') lastNonPeriod = s;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000041
Ted Kremenek8af29752009-02-26 21:30:32 +000042 if (!lastNonPeriod)
43 return 0;
44
45 return (lastNonPeriod - start) + 1;
46}
47
48static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) {
49 return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]);
50}
51
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000052PathDiagnosticPiece::PathDiagnosticPiece(const std::string& s,
Ted Kremeneke3ce2652009-03-02 19:39:50 +000053 Kind k, DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000054 : str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000055
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000056PathDiagnosticPiece::PathDiagnosticPiece(const char* s, Kind k,
Ted Kremenek8af29752009-02-26 21:30:32 +000057 DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000058 : str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek48504512009-03-06 07:53:30 +000059
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000060PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
61 : kind(k), Hint(hint) {}
Ted Kremenek082cb8d2009-03-12 18:41:53 +000062
Ted Kremenek4e063872009-03-06 22:10:49 +000063PathDiagnosticPiece::~PathDiagnosticPiece() {}
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +000064PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
65PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
Ted Kremenek4e063872009-03-06 22:10:49 +000066
67PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {
68 for (iterator I = begin(), E = end(); I != E; ++I) delete *I;
69}
70
Ted Kremenek48504512009-03-06 07:53:30 +000071PathDiagnostic::PathDiagnostic() : Size(0) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000072
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000073PathDiagnostic::~PathDiagnostic() {
74 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
75}
76
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000077void PathDiagnostic::resetPath(bool deletePieces) {
78 Size = 0;
79
80 if (deletePieces)
81 for (iterator I=begin(), E=end(); I!=E; ++I)
82 delete &*I;
83
84 path.clear();
85}
86
Ted Kremeneka127cca2009-03-06 07:08:50 +000087
88PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc,
89 const char* category)
Ted Kremenek48504512009-03-06 07:53:30 +000090 : Size(0),
91 BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +000092 Desc(desc, GetNumCharsToLastNonPeriod(desc)),
93 Category(category, GetNumCharsToLastNonPeriod(category)) {}
94
95PathDiagnostic::PathDiagnostic(const std::string& bugtype,
96 const std::string& desc,
97 const std::string& category)
Ted Kremenek48504512009-03-06 07:53:30 +000098 : Size(0),
99 BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +0000100 Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)),
101 Category(category, 0, GetNumCharsToLastNonPeriod(category)) {}
102
Chris Lattner0a14eee2008-11-18 07:04:44 +0000103void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
104 const DiagnosticInfo &Info) {
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000105
106 // Create a PathDiagnostic with a single piece.
107
Ted Kremenek55851142008-04-22 16:15:03 +0000108 PathDiagnostic* D = new PathDiagnostic();
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000109
Chris Lattnere837f932008-11-18 04:44:58 +0000110 const char *LevelStr;
Ted Kremenek120187d2008-03-27 06:16:40 +0000111 switch (DiagLevel) {
Mike Stumpe87f5c12009-02-07 03:46:08 +0000112 default:
Chris Lattner41327582009-02-06 03:57:44 +0000113 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattnere837f932008-11-18 04:44:58 +0000114 case Diagnostic::Note: LevelStr = "note: "; break;
115 case Diagnostic::Warning: LevelStr = "warning: "; break;
116 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +0000117 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000118 }
Ted Kremenek120187d2008-03-27 06:16:40 +0000119
Chris Lattnerf4c83962008-11-19 06:51:40 +0000120 llvm::SmallString<100> StrC;
121 StrC += LevelStr;
122 Info.FormatDiagnostic(StrC);
Ted Kremenek120187d2008-03-27 06:16:40 +0000123
Chris Lattner0a14eee2008-11-18 07:04:44 +0000124 PathDiagnosticPiece *P =
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000125 new PathDiagnosticEventPiece(Info.getLocation(),
Chris Lattnerf4c83962008-11-19 06:51:40 +0000126 std::string(StrC.begin(), StrC.end()));
Ted Kremenek120187d2008-03-27 06:16:40 +0000127
Chris Lattner0a14eee2008-11-18 07:04:44 +0000128 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
129 P->addRange(Info.getRange(i));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000130 for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i)
131 P->addCodeModificationHint(Info.getCodeModificationHint(i));
Ted Kremenek55851142008-04-22 16:15:03 +0000132 D->push_front(P);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000133
Ted Kremenek120187d2008-03-27 06:16:40 +0000134 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000135}