Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CLog.h
Line
Count
Source (jump to first uncovered line)
1
//===- CLog.h - Logging Interface -------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
10
#define LLVM_CLANG_TOOLS_LIBCLANG_CLOG_H
11
12
#include "clang-c/Index.h"
13
#include "clang/Basic/LLVM.h"
14
#include "llvm/ADT/IntrusiveRefCntPtr.h"
15
#include "llvm/ADT/SmallString.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/Support/Compiler.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include <string>
20
21
namespace llvm {
22
class format_object_base;
23
}
24
25
namespace clang {
26
  class FileEntry;
27
28
namespace cxindex {
29
30
class Logger;
31
typedef IntrusiveRefCntPtr<Logger> LogRef;
32
33
/// Collects logging output and writes it to stderr when it's destructed.
34
/// Common use case:
35
/// \code
36
///   if (LogRef Log = Logger::make(__func__)) {
37
///     *Log << "stuff";
38
///   }
39
/// \endcode
40
class Logger : public RefCountedBase<Logger> {
41
  std::string Name;
42
  bool Trace;
43
  SmallString<64> Msg;
44
  llvm::raw_svector_ostream LogOS;
45
public:
46
43.6k
  static const char *getEnvVar() {
47
43.6k
    static const char *sCachedVar = ::getenv("LIBCLANG_LOGGING");
48
43.6k
    return sCachedVar;
49
43.6k
  }
50
21.8k
  static bool isLoggingEnabled() { return getEnvVar() != nullptr; }
51
21.8k
  static bool isStackTracingEnabled() {
52
21.8k
    if (const char *EnvOpt = Logger::getEnvVar())
53
0
      return llvm::StringRef(EnvOpt) == "2";
54
21.8k
    return false;
55
21.8k
  }
56
  static LogRef make(llvm::StringRef name,
57
21.8k
                     bool trace = isStackTracingEnabled()) {
58
21.8k
    if (isLoggingEnabled())
59
0
      return new Logger(name, trace);
60
21.8k
    return nullptr;
61
21.8k
  }
62
63
  explicit Logger(llvm::StringRef name, bool trace)
64
0
      : Name(std::string(name)), Trace(trace), LogOS(Msg) {}
65
  ~Logger();
66
67
  Logger &operator<<(CXTranslationUnit);
68
  Logger &operator<<(const FileEntry *FE);
69
  Logger &operator<<(CXCursor cursor);
70
  Logger &operator<<(CXSourceLocation);
71
  Logger &operator<<(CXSourceRange);
72
  Logger &operator<<(CXString);
73
0
  Logger &operator<<(llvm::StringRef Str) { LogOS << Str; return *this; }
74
0
  Logger &operator<<(const char *Str) {
75
0
    if (Str)
76
0
      LogOS << Str;
77
0
    return *this;
78
0
  }
79
0
  Logger &operator<<(unsigned long N) { LogOS << N; return *this; }
80
0
  Logger &operator<<(long N) { LogOS << N ; return *this; }
81
0
  Logger &operator<<(unsigned int N) { LogOS << N; return *this; }
82
0
  Logger &operator<<(int N) { LogOS << N; return *this; }
83
0
  Logger &operator<<(char C) { LogOS << C; return *this; }
84
0
  Logger &operator<<(unsigned char C) { LogOS << C; return *this; }
85
0
  Logger &operator<<(signed char C) { LogOS << C; return *this; }
86
  Logger &operator<<(const llvm::format_object_base &Fmt);
87
};
88
89
}
90
}
91
92
/// Macros to automate common uses of Logger. Like this:
93
/// \code
94
///   LOG_FUNC_SECTION {
95
///     *Log << "blah";
96
///   }
97
/// \endcode
98
#define LOG_SECTION(NAME) \
99
12.3k
    if (clang::cxindex::LogRef 
Log12.2k
= clang::cxindex::Logger::make(NAME))
100
12.2k
#define LOG_FUNC_SECTION LOG_SECTION(__func__)
101
102
#endif