coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
contradiction.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004, 2006 Symbian Software Ltd. *
3  * All rights reserved. *
4  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
5  * All rights reserved. *
6  * *
7  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
8  * *
9  * Redistribution and use in source and binary forms, with or without *
10  * modification, are permitted provided that the following conditions *
11  * are met: *
12  * *
13  * Redistributions of source code must retain the above copyright *
14  * notice, this list of conditions and the following disclaimer. *
15  * *
16  * Redistributions in binary form must reproduce the above copyright *
17  * notice, this list of conditions and the following disclaimer in the *
18  * documentation and/or other materials provided with the distribution. *
19  * *
20  * Neither the name of Symbian Software Ltd. nor the names of its *
21  * contributors may be used to endorse or promote products derived from *
22  * this software without specific prior written permission. *
23  * *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
31  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
34  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
35  * DAMAGE. *
36  * *
37  **************************************************************************/
38 
39 #include "contradiction.h"
40 #include "diagnostic.h"
41 #include "io.h"
42 #include "if_control.h"
43 #include "line_despatch.h"
44 #include "canonical.h"
45 
49 using namespace std;
50 
55 
56 
58 {
59  diagnostic_base::flush(_policy_code_);
60  _policy_code_ = 0;
61  if (_policy_ != CONTRADICTION_DELETE) {
62  line_despatch::substitute(_subst_text_);
63  if (_policy_ == CONTRADICTION_ERROR) {
64  warning_error_generated() << "An #error directive was generated "
65  " by the --conflict policy" << emit();
68  "An operative #error directive was outpout" << emit();
69  }
70  }
71  }
72 }
73 
74 void contradiction::insert(cause why,string const & symname)
75 {
76  contradiction::save(why,symname);
77  insert_pending();
78 }
79 
81 {
82  if (_policy_code_) {
83  insert_pending();
84  }
85 }
86 
88 {
89  diagnostic_base::discard(_policy_code_);
90  _policy_code_ = 0;
91 }
92 
93 void contradiction::save(cause why, string const & symname)
94 {
95  _last_conflicted_symbol_ = symname;
97  string gripe("\"");
98  switch(why) {
99  case cause::DIFFERENTLY_REDEFING_D:
100  gripe += line + "\" differently redefines -D symbol";
101  break;
102  case cause::DEFINING_U:
103  gripe += line + "\" contradicts -U or --implicit";
104  break;
105  case cause::UNDEFING_D:
106  gripe += line + "\" contradicts -D symbol";
107  break;
108  default:
109  assert(false);
110  }
111  stringstream ss("error : inserted by coan: ");
112  ss.seekp(0,ios_base::end);
113  ss << gripe << " at " << io::in_file_name() << '(' <<
114  line_despatch::cur_line().num() << ")\n";
115  switch(_policy_) {
116  case CONTRADICTION_DELETE: {
118  _policy_code_ = diagnostic.encode();
119  diagnostic << gripe << defer();
120  break;
121  }
122  case CONTRADICTION_COMMENT: {
124  _policy_code_ = diagnostic.encode();
125  diagnostic << gripe << defer();
126  _subst_text_ = string("// ") + ss.str();
127  break;
128  }
129  case CONTRADICTION_ERROR: {
131  _policy_code_ = diagnostic.encode();
132  diagnostic << gripe << defer();
133  _subst_text_ = string("#") + ss.str();
134  break;
135  }
136  default:
137  assert(false);
138  }
139 }
140 
141 /* EOF*/
static parsed_line & cur_line()
Get a reference to the current output line.
Definition: line_despatch.h:68
template struct warning_msg<Id> generically encapsulates a warning diagnostic.
Definition: diagnostic.h:473
static void forget()
Forget about an apparent contradiction.
static void save(cause why, std::string const &symname)
Save diagnostic details of a potential contradiction.
cause
Reasons for reporting a contadiction.
Definition: contradiction.h:63
static bool is_unconditional_line()
Is the current line outside any #if scope or in the scope of a satisfied #if?
Definition: if_control.cpp:326
Comment out a contradicted directive.
Definition: options.h:66
`template struct diagnostic<Level,Id> generically encapsulates a type of diagnostic.
Definition: diagnostic.h:386
static void substitute(std::string const &replacement)
Substitute a diagnostic insert for the line in the line-buffer and print it to output.
static std::string in_file_name()
Get the name of the current source file.
Definition: io.h:105
struct warning_unconditional_error_output encapsulates a diagnostic for an error directive output unc...
Definition: diagnostic.h:658
unsigned num() const
Get the greatest source line number spanned by this line.
Definition: parsed_line.h:70
static void insert_pending()
Insert a stored error diagnostic into output.
static contradiction_policy _policy_
The operative contradiction policy.
static size_t flush(unsigned reason)
Emit all queued diagnostics that match a reason-code.
Definition: diagnostic.h:237
Delete a contradicted directive.
Definition: options.h:64
The tag class is inserted in a diagnostic_base to tell it to emit itself.
Definition: diagnostic.h:77
contradiction_policy
Symbolic constants denoting policies for handling contradictions.
Definition: options.h:62
static void insert(cause why, std::string const &symname)
Insert an error diagnostic into the output as a error directive or comment.
The tag class is inserted in a diagnostic_base to tell it to defer itself.
Definition: diagnostic.h:82
static void flush()
Discharge any pending contradiction.
template class canonical<What> encapsulates the canonical representation of values of type What...
Definition: canonical.h:82
static std::string _last_conflicted_symbol_
The name of the latest #undef-ed symbol.
static size_t discard(unsigned reason)
Discard any queued diagnostics that match a reason-code, returning the number discarded.
Definition: diagnostic.h:217
Replace a contradicted directive with an #error
Definition: options.h:68
`template struct warning_error_generated_input' encapsulates a diagnostic for error directive output ...
Definition: diagnostic.h:646
static unsigned _policy_code_
The reason-code of diagnostics associated with the operative contradiction policy.
unsigned encode() const
Get the reason-code of the diagnostic.
Definition: diagnostic.h:138
static std::string _subst_text_
Text for substitution in output.
std::string & str()
Get a [const] reference to the underlying std::string.
Definition: parse_buffer.h:92