GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/options/set_language.cpp Lines: 29 29 100.0 %
Date: 2021-05-22 Branches: 9 16 56.3 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Morgan Deters, Tim King, Kshitij Bansal
4
 *
5
 * This file is part of the cvc5 project.
6
 *
7
 * Copyright (c) 2009-2021 by the authors listed in the file AUTHORS
8
 * in the top-level source directory and their institutional affiliations.
9
 * All rights reserved.  See the file COPYING in the top-level source
10
 * directory for licensing information.
11
 * ****************************************************************************
12
 *
13
 * Definition of input and output languages.
14
 */
15
#include "options/set_language.h"
16
17
#include <iosfwd>
18
#include <iomanip>
19
20
#include "options/base_options.h"
21
#include "options/language.h"
22
#include "options/options.h"
23
24
namespace cvc5 {
25
namespace language {
26
27
9398
const int SetLanguage::s_iosIndex = std::ios_base::xalloc();
28
29
92
SetLanguage::Scope::Scope(std::ostream& out, OutputLanguage language)
30
  : d_out(out)
31
92
  , d_oldLanguage(SetLanguage::getLanguage(out))
32
{
33
92
  SetLanguage::setLanguage(out, language);
34
92
}
35
36
184
SetLanguage::Scope::~Scope(){
37
92
  SetLanguage::setLanguage(d_out, d_oldLanguage);
38
92
}
39
40
41
8771
SetLanguage::SetLanguage(OutputLanguage l)
42
8771
  : d_language(l)
43
8771
{}
44
45
8388
void SetLanguage::applyLanguage(std::ostream& out) {
46
  // (offset by one to detect whether default has been set yet)
47
8388
  out.iword(s_iosIndex) = int(d_language) + 1;
48
8388
}
49
50
371493
OutputLanguage SetLanguage::getLanguage(std::ostream& out) {
51
371493
  long& l = out.iword(s_iosIndex);
52
371493
  if(l == 0) {
53
    // set the default language on this ostream
54
    // (offset by one to detect whether default has been set yet)
55
36716
    if(not Options::isCurrentNull()) {
56
36706
      l = options::outputLanguage() + 1;
57
    }
58
36716
    if(l <= 0 || l > language::output::LANG_MAX) {
59
      // if called from outside the library, we may not have options
60
      // available to us at this point (or perhaps the output language
61
      // is not set in Options).  Default to something reasonable, but
62
      // don't set "l" since that would make it "sticky" for this
63
      // stream.
64
2062
      return OutputLanguage(s_defaultOutputLanguage);
65
    }
66
  }
67
369431
  return OutputLanguage(l - 1);
68
}
69
70
184
void SetLanguage::setLanguage(std::ostream& out, OutputLanguage l) {
71
  // (offset by one to detect whether default has been set yet)
72
184
  out.iword(s_iosIndex) = int(l) + 1;
73
184
}
74
75
8388
std::ostream& operator<<(std::ostream& out, SetLanguage l) {
76
8388
  l.applyLanguage(out);
77
8388
  return out;
78
}
79
80
}  // namespace language
81
28194
}  // namespace cvc5