1c383a500aa59423264811be3874461bf8adbfea0Zonr Chang/*
2c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Copyright 2010, The Android Open Source Project
3c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
4c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
5c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * you may not use this file except in compliance with the License.
6c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * You may obtain a copy of the License at
7c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
8c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
9c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
10c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Unless required by applicable law or agreed to in writing, software
11c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
12c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * See the License for the specific language governing permissions and
14c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * limitations under the License.
15c383a500aa59423264811be3874461bf8adbfea0Zonr Chang */
16c383a500aa59423264811be3874461bf8adbfea0Zonr Chang
17e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_DIAGNOSTIC_BUFFER_H_  // NOLINT
18e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#define _FRAMEWORKS_COMPILE_SLANG_SLANG_DIAGNOSTIC_BUFFER_H_
19462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
205309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet#include <set>
21e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <string>
22462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include "clang/Basic/Diagnostic.h"
24462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
25e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/raw_ostream.h"
26462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
27462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaonamespace llvm {
286315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  class raw_string_ostream;
299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}
30462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
31462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaonamespace slang {
32462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
339207a2e495c8363606861e4f034504ec5c153dabLogan Chien// The diagnostics consumer instance (for reading the processed diagnostics)
349207a2e495c8363606861e4f034504ec5c153dabLogan Chienclass DiagnosticBuffer : public clang::DiagnosticConsumer {
355309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouilletprivate:
365309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet  // We keed track of the messages that have been already added to this
375309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet  // diagnostic buffer, to avoid duplicates.  This can happen because for a
385309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet  // given script we'll usually compile for both 32 and 64 bit targets.
395309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet  std::set<std::string> mIncludedMessages;
409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::string mDiags;
412eb9a3f7c48fe54eb4e813d80e3363bc79553a1eStephen Hines  std::unique_ptr<llvm::raw_string_ostream> mSOS;
42462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
435309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouilletpublic:
449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  DiagnosticBuffer();
459207a2e495c8363606861e4f034504ec5c153dabLogan Chien  virtual ~DiagnosticBuffer();
469207a2e495c8363606861e4f034504ec5c153dabLogan Chien
479207a2e495c8363606861e4f034504ec5c153dabLogan Chien  virtual void HandleDiagnostic(clang::DiagnosticsEngine::Level DiagLevel,
485309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet                                const clang::Diagnostic &Info) override;
49462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
506315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  inline const std::string &str() const {
516315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr    mSOS->flush();
526315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr    return mDiags;
536315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  }
54462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
556315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  inline void reset() {
565309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet    mIncludedMessages.clear();
575309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet    mSOS.reset();
585309b0cbeda2c75eedf6627e06f4471c77f98f83Jean-Luc Brouillet    mDiags.clear();
596315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  }
609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao};
619207a2e495c8363606861e4f034504ec5c153dabLogan Chien
62e67239de8d94975e7e2216ee6860ae2e6cb8b15aStephen Hines}  // namespace slang
63462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
64e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_DIAGNOSTIC_BUFFER_H_  NOLINT
65