File: pp-macro.cpp

package info (click to toggle)
kdevelop 4%3A4.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,360 kB
  • ctags: 11,484
  • sloc: cpp: 105,371; python: 522; ansic: 488; lex: 422; sh: 139; ruby: 120; makefile: 51; xml: 42; php: 12
file content (153 lines) | stat: -rw-r--r-- 4,448 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
  Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
  Copyright 2006 Hamish Rodda <rodda@kde.org>
  Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>

  Permission to use, copy, modify, distribute, and sell this software and its
  documentation for any purpose is hereby granted without fee, provided that
  the above copyright notice appear in all copies and that both that
  copyright notice and this permission notice appear in supporting
  documentation.

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "pp-macro.h"
#include "chartools.h"
#include "macrorepository.h"
#include <util/kdevvarlengtharray.h>

using namespace rpp;

namespace rpp {
using namespace KDevelop;
DEFINE_LIST_MEMBER_HASH(pp_macro, definition, KDevelop::IndexedString)
DEFINE_LIST_MEMBER_HASH(pp_macro, formals, KDevelop::IndexedString)
}

pp_macro::~pp_macro() {
  freeAppendedLists();
}

bool pp_macro::operator==(const pp_macro& rhs) const {
  if(completeHash() != rhs.completeHash())
    return false;
  
  return name == rhs.name && file == rhs.file &&
         file == rhs.file &&
         sourceLine == rhs.sourceLine &&
         defined == rhs.defined &&
         hidden == rhs.hidden &&
         function_like == rhs.function_like &&
         variadics == rhs.variadics &&
         fixed == rhs.fixed &&
         defineOnOverride == rhs.defineOnOverride &&
         listsEqual(rhs);
}

void pp_macro::invalidateHash() {
  m_valueHashValid = false;
}

pp_macro::pp_macro(const KDevelop::IndexedString& nm) : name(nm)
  , sourceLine(-1)
  , defined(true)
  , hidden(false)
  , function_like(false)
  , variadics(false)
  , fixed(false)
  , defineOnOverride(false)
  , m_valueHashValid(false)
  , m_valueHash(0)
{
  initializeAppendedLists();
}

pp_macro::pp_macro(const pp_macro& rhs, bool dynamic) : 
   name(rhs.name),
   file(rhs.file),
   sourceLine(rhs.sourceLine),
   defined(rhs.defined),
   hidden(rhs.hidden),
   function_like(rhs.function_like),
   variadics(rhs.variadics),
   fixed(rhs.fixed),
   defineOnOverride(rhs.defineOnOverride),
   m_valueHashValid(true),
   m_valueHash(rhs.valueHash())
{
  initializeAppendedLists(dynamic);
  copyListsFrom(rhs);
}

pp_macro::pp_macro(const char* nm) : name(KDevelop::IndexedString(nm, strlen(nm)))
  , sourceLine(-1)
  , defined(true)
  , hidden(false)
  , function_like(false)
  , variadics(false)
  , fixed(false)
  , defineOnOverride(false)
  , m_valueHashValid(false)
  , m_valueHash(0)
{
  initializeAppendedLists();
}

QString pp_macro::toString() const {
  QString ret = name.str();
  if(!defined)
    ret = "undef " + ret;
  if(function_like) {
    ret += '(';
    bool first = true;
    for(uint a = 0; a < formalsSize(); ++a) {
      if(!first)
        ret += ", ";
      first = false;
      
      ret += formals()[a].str();
    }
    ret += ')';
  }
  ret += ' ' + QString::fromUtf8(stringFromContents((uint*)definition(), definitionSize()));
  
  return ret;
}

void pp_macro::setDefinitionText(QString definition) {
  setDefinitionText(definition.toUtf8());
}

void pp_macro::setDefinitionText(QByteArray definition) {
  definitionList().clear();
  foreach(uint i, convertFromByteArray(definition))
    definitionList().append(KDevelop::IndexedString::fromIndex(i));
}

void pp_macro::computeHash() const {
    if( m_valueHashValid )
      return;

    m_valueHash = 27 * ( 137 + (defined ? 1 : 0 ) );

    m_valueHash += 1741 * file.hash() + 238 * sourceLine + (hidden ? 19 : 0) + (function_like ? 811241 : 0) + (variadics ? 129119 : 0) + (fixed ? 1807 : 0) + (defineOnOverride ? 31621 : 0);
  
    FOREACH_FUNCTION(const IndexedString& definitionComponent, definition)
      m_valueHash = definitionComponent.hash() + 17 * m_valueHash;

    int a = 1;
    FOREACH_FUNCTION(const IndexedString& formal, formals) {
        a *= 19;
        m_valueHash += a * formal.hash();
    }
    m_valueHashValid = true;
}