blob: 468b6c89f2bc5f76475a36e979786d8d14a4da9e [file] [log] [blame]
Ted Kremenek14f14572008-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 Lattnerbe8e5a42008-11-19 06:51:40 +000015#include "llvm/ADT/SmallString.h"
Ted Kremenekaa3e5372008-03-27 06:16:40 +000016#include <sstream>
Ted Kremenek14f14572008-03-27 03:49:32 +000017using namespace clang;
Ted Kremenek951a4212009-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 Kremenek14f14572008-03-27 03:49:32 +000025
Ted Kremenek951a4212009-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 Kremenek6b104ed2009-03-02 19:39:50 +000038 Kind k, DisplayHint hint)
Ted Kremenek077cd642009-03-06 07:53:30 +000039 : Pos(pos), str(s, 0, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {
40 assert(Pos.isValid() &&
41 "PathDiagnosticPiece's must have a valid location.");
42}
Ted Kremenek951a4212009-02-26 21:30:32 +000043
44PathDiagnosticPiece::PathDiagnosticPiece(FullSourceLoc pos,
Ted Kremenek6b104ed2009-03-02 19:39:50 +000045 const char* s, Kind k,
Ted Kremenek951a4212009-02-26 21:30:32 +000046 DisplayHint hint)
Ted Kremenek077cd642009-03-06 07:53:30 +000047 : Pos(pos), str(s, GetNumCharsToLastNonPeriod(s)), kind(k), Hint(hint) {
48 assert(Pos.isValid() &&
49 "PathDiagnosticPiece's must have a valid location.");
50}
51
52PathDiagnostic::PathDiagnostic() : Size(0) {}
Ted Kremenek951a4212009-02-26 21:30:32 +000053
Ted Kremenek14f14572008-03-27 03:49:32 +000054PathDiagnostic::~PathDiagnostic() {
55 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I;
56}
57
Ted Kremenek28379f32009-03-06 07:08:50 +000058
59PathDiagnostic::PathDiagnostic(const char* bugtype, const char* desc,
60 const char* category)
Ted Kremenek077cd642009-03-06 07:53:30 +000061 : Size(0),
62 BugType(bugtype, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremenek28379f32009-03-06 07:08:50 +000063 Desc(desc, GetNumCharsToLastNonPeriod(desc)),
64 Category(category, GetNumCharsToLastNonPeriod(category)) {}
65
66PathDiagnostic::PathDiagnostic(const std::string& bugtype,
67 const std::string& desc,
68 const std::string& category)
Ted Kremenek077cd642009-03-06 07:53:30 +000069 : Size(0),
70 BugType(bugtype, 0, GetNumCharsToLastNonPeriod(bugtype)),
Ted Kremenek28379f32009-03-06 07:08:50 +000071 Desc(desc, 0, GetNumCharsToLastNonPeriod(desc)),
72 Category(category, 0, GetNumCharsToLastNonPeriod(category)) {}
73
Chris Lattner6948ae62008-11-18 07:04:44 +000074void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
75 const DiagnosticInfo &Info) {
Ted Kremenek14f14572008-03-27 03:49:32 +000076
77 // Create a PathDiagnostic with a single piece.
78
Ted Kremenekf309cf92008-04-22 16:15:03 +000079 PathDiagnostic* D = new PathDiagnostic();
Ted Kremenek14f14572008-03-27 03:49:32 +000080
Chris Lattner8c7f00f2008-11-18 04:44:58 +000081 const char *LevelStr;
Ted Kremenekaa3e5372008-03-27 06:16:40 +000082 switch (DiagLevel) {
Mike Stump4fb74f72009-02-07 03:46:08 +000083 default:
Chris Lattner95cb5502009-02-06 03:57:44 +000084 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Chris Lattner8c7f00f2008-11-18 04:44:58 +000085 case Diagnostic::Note: LevelStr = "note: "; break;
86 case Diagnostic::Warning: LevelStr = "warning: "; break;
87 case Diagnostic::Error: LevelStr = "error: "; break;
Chris Lattner95cb5502009-02-06 03:57:44 +000088 case Diagnostic::Fatal: LevelStr = "fatal error: "; break;
Ted Kremenek14f14572008-03-27 03:49:32 +000089 }
Ted Kremenekaa3e5372008-03-27 06:16:40 +000090
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000091 llvm::SmallString<100> StrC;
92 StrC += LevelStr;
93 Info.FormatDiagnostic(StrC);
Ted Kremenekaa3e5372008-03-27 06:16:40 +000094
Chris Lattner6948ae62008-11-18 07:04:44 +000095 PathDiagnosticPiece *P =
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000096 new PathDiagnosticPiece(Info.getLocation(),
97 std::string(StrC.begin(), StrC.end()));
Ted Kremenekaa3e5372008-03-27 06:16:40 +000098
Chris Lattner6948ae62008-11-18 07:04:44 +000099 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
100 P->addRange(Info.getRange(i));
Douglas Gregor3bb30002009-02-26 21:00:50 +0000101 for (unsigned i = 0, e = Info.getNumCodeModificationHints(); i != e; ++i)
102 P->addCodeModificationHint(Info.getCodeModificationHint(i));
Ted Kremenekf309cf92008-04-22 16:15:03 +0000103 D->push_front(P);
Ted Kremenek14f14572008-03-27 03:49:32 +0000104
Ted Kremenekaa3e5372008-03-27 06:16:40 +0000105 HandlePathDiagnostic(D);
Ted Kremenek14f14572008-03-27 03:49:32 +0000106}