blob: c078b6d0f2ebf7049614e87219a944c4e414deda [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 Kremenek120187d2008-03-27 06:16:40 +000016#include <sstream>
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000017using namespace clang;
Ted Kremenek8af29752009-02-26 21:30:32 +000018
19static size_t GetNumCharsToLastNonPeriod(const char *s) {
20 const char *start = s;
21 const char *lastNonPeriod = 0;
22
23 for ( ; *s != '\0' ; ++s)
24 if (*s != '.') lastNonPeriod = s;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000025
Ted Kremenek8af29752009-02-26 21:30:32 +000026 if (!lastNonPeriod)
27 return 0;
28
29 return (lastNonPeriod - start) + 1;
30}
31
32static inline size_t GetNumCharsToLastNonPeriod(const std::string &s) {
33 return s.empty () ? 0 : GetNumCharsToLastNonPeriod(&s[0]);
34}
35
36PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos,
37 const std::string& s,
Ted Kremeneke3ce2652009-03-02 19:39:50 +000038 Kind k, DisplayHint hint)
39 : Pos(pos), str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000040
41PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos,
Ted Kremeneke3ce2652009-03-02 19:39:50 +000042 const char* s, Kind k,
Ted Kremenek8af29752009-02-26 21:30:32 +000043 DisplayHint hint)
Ted Kremeneke3ce2652009-03-02 19:39:50 +000044 : Pos(pos), str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {}
Ted Kremenek8af29752009-02-26 21:30:32 +000045
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000046PathDiagnostic::~PathDiagnostic() {
47 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
48}
49
Ted Kremeneka127cca2009-03-06 07:08:50 +000050
51PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc,
52 const char* category)
53 : BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)),
54 Desc(desc, GetNumCharsToLastNonPeriod(desc)),
55 Category(category, GetNumCharsToLastNonPeriod(category)) {}
56
57PathDiagnostic::PathDiagnostic(const std::string& bugtype,
58 const std::string& desc,
59 const std::string& category)
60 : BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)),
61 Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)),
62 Category(category, 0, GetNumCharsToLastNonPeriod(category)) {}
63
Chris Lattner0a14eee2008-11-18 07:04:44 +000064void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
65 const DiagnosticInfo &Info) {
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000066
67 // Create a PathDiagnostic with a single piece.
68
Ted Kremenek55851142008-04-22 16:15:03 +000069 PathDiagnostic* D = new PathDiagnostic();
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000070
Chris Lattnere837f932008-11-18 04:44:58 +000071 const char *LevelStr;
Ted Kremenek120187d2008-03-27 06:16:40 +000072 switch (DiagLevel) {
Mike Stumpe87f5c12009-02-07 03:46:08 +000073 default:
Chris Lattner41327582009-02-06 03:57:44 +000074 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattnere837f932008-11-18 04:44:58 +000075 case Diagnostic::Note: LevelStr = "note: "; break;
76 case Diagnostic::Warning: LevelStr = "warning: "; break;
77 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +000078 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000079 }
Ted Kremenek120187d2008-03-27 06:16:40 +000080
Chris Lattnerf4c83962008-11-19 06:51:40 +000081 llvm::SmallString<100> StrC;
82 StrC += LevelStr;
83 Info.FormatDiagnostic(StrC);
Ted Kremenek120187d2008-03-27 06:16:40 +000084
Chris Lattner0a14eee2008-11-18 07:04:44 +000085 PathDiagnosticPiece *P =
Chris Lattnerf4c83962008-11-19 06:51:40 +000086 new PathDiagnosticPiece(Info.getLocation(),
87 std::string(StrC.begin(), StrC.end()));
Ted Kremenek120187d2008-03-27 06:16:40 +000088
Chris Lattner0a14eee2008-11-18 07:04:44 +000089 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
90 P->addRange(Info.getRange(i));
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000091 for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i)
92 P->addCodeModificationHint(Info.getCodeModificationHint(i));
Ted Kremenek55851142008-04-22 16:15:03 +000093 D->push_front(P);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000094
Ted Kremenek120187d2008-03-27 06:16:40 +000095 HandlePathDiagnostic(D);
Ted Kremenekd3abcdf2008-03-27 03:49:32 +000096}