blob: 02fe1658ad279a223856dfade474559da33a4e88 [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"
Ted Kremenek97b40032009-03-26 21:39:39 +000015#include "clang/AST/Expr.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000016#include "llvm/ADT/SmallString.h"
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000017#include "llvm/Support/Casting.h"
Ted Kremenek120187d2008-03-27 06:16:40 +000018#include <sstream>
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000019using namespace clang;
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000020using llvm::dyn_cast;
21using llvm::isa;
22
23bool PathDiagnosticMacroPiece::containsEvent() const {
24 for (const_iterator I = begin(), E = end(); I!=E; ++I) {
25 if (isa<PathDiagnosticEventPiece>(*I))
26 return true;
27
28 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I))
29 if (MP->containsEvent())
30 return true;
31 }
32
33 return false;
34}
Ted Kremenek8af29752009-02-26 21:30:32 +000035
36static size_t GetNumCharsToLastNonPeriod(const char *s) {
37 const char *start = s;
38 const char *lastNonPeriod = 0;
39
40 for ( ; *s != '\0' ; ++s)
41 if (*s != '.') lastNonPeriod = s;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000042
Ted Kremenek8af29752009-02-26 21:30:32 +000043 if (!lastNonPeriod)
44 return 0;
45
46 return (lastNonPeriod - start) + 1;
47}
48
49static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) {
50 return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]);
51}
52
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000053PathDiagnosticPiece::PathDiagnosticPiece(const std::string& s,
Ted Kremeneke3ce2652009-03-02 19:39:50 +000054 Kind k, DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000055 : str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000056
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000057PathDiagnosticPiece::PathDiagnosticPiece(const char* s, Kind k,
Ted Kremenek8af29752009-02-26 21:30:32 +000058 DisplayHint hint)
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000059 : str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek48504512009-03-06 07:53:30 +000060
Ted Kremenek1f9bd0f2009-03-26 21:21:35 +000061PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint)
62 : kind(k), Hint(hint) {}
Ted Kremenek082cb8d2009-03-12 18:41:53 +000063
Ted Kremenek4e063872009-03-06 22:10:49 +000064PathDiagnosticPiece::~PathDiagnosticPiece() {}
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +000065PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {}
66PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {}
Ted Kremenek4e063872009-03-06 22:10:49 +000067
68PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() {
69 for (iterator I = begin(), E = end(); I != E; ++I) delete *I;
70}
71
Ted Kremenek48504512009-03-06 07:53:30 +000072PathDiagnostic::PathDiagnostic() : Size(0) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000073
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000074PathDiagnostic::~PathDiagnostic() {
75 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
76}
77
Ted Kremenek0e5c8d42009-03-10 05:16:17 +000078void PathDiagnostic::resetPath(bool deletePieces) {
79 Size = 0;
80
81 if (deletePieces)
82 for (iterator I=begin(), E=end(); I!=E; ++I)
83 delete &*I;
84
85 path.clear();
86}
87
Ted Kremeneka127cca2009-03-06 07:08:50 +000088
89PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc,
90 const char* category)
Ted Kremenek48504512009-03-06 07:53:30 +000091 : Size(0),
92 BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +000093 Desc(desc, GetNumCharsToLastNonPeriod(desc)),
94 Category(category, GetNumCharsToLastNonPeriod(category)) {}
95
96PathDiagnostic::PathDiagnostic(const std::string& bugtype,
97 const std::string& desc,
98 const std::string& category)
Ted Kremenek48504512009-03-06 07:53:30 +000099 : Size(0),
100 BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremeneka127cca2009-03-06 07:08:50 +0000101 Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)),
102 Category(category, 0, GetNumCharsToLastNonPeriod(category)) {}
103
Chris Lattner0a14eee2008-11-18 07:04:44 +0000104void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
105 const DiagnosticInfo &Info) {
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000106
107 // Create a PathDiagnostic with a single piece.
108
Ted Kremenek55851142008-04-22 16:15:03 +0000109 PathDiagnostic* D = new PathDiagnostic();
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000110
Chris Lattnere837f932008-11-18 04:44:58 +0000111 const char *LevelStr;
Ted Kremenek120187d2008-03-27 06:16:40 +0000112 switch (DiagLevel) {
Mike Stumpe87f5c12009-02-07 03:46:08 +0000113 default:
Chris Lattner41327582009-02-06 03:57:44 +0000114 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattnere837f932008-11-18 04:44:58 +0000115 case Diagnostic::Note: LevelStr = "note: "; break;
116 case Diagnostic::Warning: LevelStr = "warning: "; break;
117 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +0000118 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000119 }
Ted Kremenek120187d2008-03-27 06:16:40 +0000120
Chris Lattnerf4c83962008-11-19 06:51:40 +0000121 llvm::SmallString<100> StrC;
122 StrC += LevelStr;
123 Info.FormatDiagnostic(StrC);
Ted Kremenek120187d2008-03-27 06:16:40 +0000124
Chris Lattner0a14eee2008-11-18 07:04:44 +0000125 PathDiagnosticPiece *P =
Ted Kremenek1fbfd5b2009-03-06 23:58:11 +0000126 new PathDiagnosticEventPiece(Info.getLocation(),
Chris Lattnerf4c83962008-11-19 06:51:40 +0000127 std::string(StrC.begin(), StrC.end()));
Ted Kremenek120187d2008-03-27 06:16:40 +0000128
Chris Lattner0a14eee2008-11-18 07:04:44 +0000129 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
130 P->addRange(Info.getRange(i));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000131 for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i)
132 P->addCodeModificationHint(Info.getCodeModificationHint(i));
Ted Kremenek55851142008-04-22 16:15:03 +0000133 D->push_front(P);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000134
Ted Kremenek120187d2008-03-27 06:16:40 +0000135 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +0000136}
Ted Kremenek97b40032009-03-26 21:39:39 +0000137
138//===----------------------------------------------------------------------===//
139// PathDiagnosticLocation methods.
140//===----------------------------------------------------------------------===//
141
142FullSourceLoc PathDiagnosticLocation::asLocation() const {
Ted Kremenekb7737022009-03-26 21:42:51 +0000143 // Note that we want a 'switch' here so that the compiler can warn us in
144 // case we add more cases.
Ted Kremenek97b40032009-03-26 21:39:39 +0000145 switch (K) {
146 case SingleLoc:
147 case Range:
Ted Kremeneka5465002009-03-26 21:42:00 +0000148 break;
Ted Kremenek97b40032009-03-26 21:39:39 +0000149 case Statement:
Ted Kremenek6f002042009-03-26 23:12:02 +0000150 return FullSourceLoc(S->getLocStart(), const_cast<SourceManager&>(*SM));
Ted Kremenek97b40032009-03-26 21:39:39 +0000151 }
Ted Kremeneka5465002009-03-26 21:42:00 +0000152
Ted Kremenek6f002042009-03-26 23:12:02 +0000153 return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM));
Ted Kremenek97b40032009-03-26 21:39:39 +0000154}
Ted Kremenekbecf8882009-03-26 21:48:17 +0000155
156SourceRange PathDiagnosticLocation::asRange() const {
157 // Note that we want a 'switch' here so that the compiler can warn us in
158 // case we add more cases.
159 switch (K) {
160 case SingleLoc:
161 case Range:
162 break;
163 case Statement:
164 return S->getSourceRange();
165 }
166
167 return R;
168}
169