File: environmentmanager.cpp

package info (click to toggle)
kdevelop 4%3A4.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 18,844 kB
  • sloc: cpp: 91,758; python: 1,095; lex: 422; ruby: 120; sh: 114; xml: 42; makefile: 38
file content (619 lines) | stat: -rw-r--r-- 23,729 bytes parent folder | download
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/***************************************************************************
   Copyright 2006 David Nolden <david.nolden.kdevelop@art-master.de>
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "environmentmanager.h"
#include <QFileInfo>
#include <kdebug.h>
#include "rpp/pp-macro.h"
#include "rpp/pp-environment.h"
#include <language/interfaces/iproblem.h>
#include "cpppreprocessenvironment.h"
#include <language/duchain/repositories/itemrepository.h>
#include <language/editor/modificationrevision.h>
#include "parser/rpp/macrorepository.h"
#include "cppdebughelper.h"
#include <language/duchain/duchainregister.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/duchain.h>
#include <language/duchain/referencecounting.h>
#include "cpppreprocessenvironment.h"

using namespace KDevelop;

#define ENSURE_FILE_READ_LOCKED(file)   if((file).indexedTopContext().isValid()) { ENSURE_CHAIN_READ_LOCKED }
#define ENSURE_READ_LOCKED   ENSURE_FILE_READ_LOCKED(*this)
#define ENSURE_WRITE_LOCKED   if(indexedTopContext().isValid()) { ENSURE_CHAIN_READ_LOCKED }

DEFINE_LIST_MEMBER_HASH(IncludePathListItem, m_includePaths, KDevelop::IndexedString)

struct IncludePathListItem {
  
    IncludePathListItem() {
      initializeAppendedLists(true);
      m_refCount = 0;
    }
    IncludePathListItem(const IncludePathListItem& rhs, bool dynamic) {
      initializeAppendedLists(dynamic);
      m_refCount = rhs.m_refCount;
      copyListsFrom(rhs);
    }
    ~IncludePathListItem() {
      freeAppendedLists();
    }
    
    bool persistent() const {
      return (bool)m_refCount;
    }
    
    bool operator==(const IncludePathListItem& rhs) const {
      return listsEqual(rhs);
    }
  
    uint hash() const {
      uint ret = 0;
      for(uint a = 0; a < m_includePathsSize(); ++a)
        ret = (m_includePaths()[a].hash() + ret) * 17;
      
      return ret;
    }
    
    uint itemSize() const {
      return dynamicSize();
    }
    
    uint classSize() const {
      return sizeof(*this);
    }
  
    uint m_refCount;
    START_APPENDED_LISTS(IncludePathListItem);
    
    APPENDED_LIST_FIRST(IncludePathListItem, IndexedString, m_includePaths);
    END_APPENDED_LISTS(IncludePathListItem, m_includePaths);
  private:
    IncludePathListItem& operator=(const IncludePathListItem&);
};

typedef AppendedListItemRequest<IncludePathListItem, 40*4> IncludePathsRequest;

typedef KDevelop::ItemRepository<IncludePathListItem, IncludePathsRequest> IncludePathsRepository;
IncludePathsRepository& includePathsRepository()
{
  static IncludePathsRepository repo("include path repository");
  return repo;
}

//If DYNAMIC_DEBUGGING is defined, debugging can be started at any point in runtime,
//by calling setIsDebugging(true) from within the debugger
// #define DYNAMIC_DEBUGGING

#ifdef DYNAMIC_DEBUGGING
volatile bool is_debugging = true;

bool debugging() {
  return is_debugging;
}

//Ment to be called from within dbg, to start doing debug output at a specific point
void setIsDebugging(bool is) {
  is_debugging = is;
}

#define DEBUG_LEXERCACHE
#define ifDebug(x) if(debugging()) {x;}
#else
inline bool debugging() {
  return false;
}
#endif

using namespace Cpp;

REGISTER_DUCHAIN_ITEM(EnvironmentFile);

namespace Cpp {
  Utils::BasicSetRepository* StaticStringSetRepository::repository() {
    return &Cpp::EnvironmentManager::self()->stringSetRepository();
  }
  Utils::BasicSetRepository* StaticMacroSetRepository::repository() {
    return &Cpp::EnvironmentManager::self()->macroSetRepository();
  }
  void MacroSetRepository::itemRemovedFromSets(uint index) {
    Cpp::EnvironmentManager::self()->macroDataRepository().deleteItem(index);
  }
}

const rpp::pp_macro& Cpp::MacroIndexConversion::toItem(uint index) const {
  return *EnvironmentManager::self()->macroDataRepository().itemFromIndex( index );
}

uint Cpp::MacroIndexConversion::toIndex(const rpp::pp_macro& macro) const {
  return EnvironmentManager::self()->macroDataRepository().index( MacroRepositoryItemRequest(macro) );
}

//For debugging
QString id(const EnvironmentFile* file) {
  return file->url().str() + QString(" %1").arg((size_t)file) ;
}

QString print(const Cpp::ReferenceCountedStringSet& set) {
  QString ret;
  bool first = true;
  Cpp::ReferenceCountedStringSet::Iterator it( set.iterator() );
  while(it) {
    if(!first)
      ret += ", ";
    first = false;
    
    ret += (*it).str();
    ++it;
  }
  return ret;
}

QString print(const Cpp::ReferenceCountedMacroSet& set) {
  QString ret;
  bool first = true;
  Cpp::ReferenceCountedMacroSet::Iterator it( set.iterator() );
  while(it) {
    if(!first)
      ret += ", ";
    first = false;
    
    ret += it.ref().toString();
    ++it;
  }
  return ret;
}

EnvironmentManager* EnvironmentManager::m_self = 0;

EnvironmentManager::EnvironmentManager()
  : m_matchingLevel(Full), m_simplifiedMatching(false),
    m_macroDataRepository("macro repository"), m_stringSetRepository("string sets"), m_macroSetRepository()
{
}

void EnvironmentManager::init()
{
  Q_ASSERT_X(!m_self, "EnvironmentManager::init()", "do not call init() twice");
  m_self = new EnvironmentManager();
  // also init the other getters
  includePathsRepository();
}

void EnvironmentManager::setSimplifiedMatching(bool simplified)
{
  m_simplifiedMatching = simplified;
}

void Cpp::EnvironmentManager::setMatchingLevel(Cpp::EnvironmentManager::MatchingLevel level) {
  m_matchingLevel = level;
}

bool EnvironmentFile::matchEnvironment(const ParsingEnvironment* _environment) const {
  ENSURE_READ_LOCKED
  const CppPreprocessEnvironment* cppEnvironment = dynamic_cast<const CppPreprocessEnvironment*>(_environment);
  if(!cppEnvironment)
    return false;

  if( cppEnvironment->identityOffsetRestrictionEnabled() && cppEnvironment->identityOffsetRestriction() != identityOffset() ) {
#ifdef DEBUG_LEXERCACHE
    kDebug( 9007 ) << "file" << url().str() << "does not match branching hash. Restriction:" << cppEnvironment->identityOffsetRestriction() << "Actual:" << identityOffset();
#endif
    return false;
  }
  
  if(EnvironmentManager::self()->matchingLevel() == EnvironmentManager::Disabled)
    return true;
  
  //Consider files that are out-guarded by the header-guard as a match, without looking into their content
  ///@todo Pick the version that is already in the environment if there is multiple
  if(EnvironmentManager::self()->matchingLevel() == EnvironmentManager::Naive)
    if(cppEnvironment->macroNameSet().contains(headerGuard())) {
#ifdef DEBUG_LEXERCACHE
      kDebug( 9007 ) << "file" << url().str() << "environment contains the header-guard, returning true";
#endif
      return true;
    }
  
  ReferenceCountedStringSet environmentMacroNames = cppEnvironment->macroNameSet();
  
  ReferenceCountedStringSet conflicts = (environmentMacroNames & strings()) - d_func()->m_usedMacroNames;

  for( ReferenceCountedStringSet::Iterator it(conflicts.iterator()); it; ++it ) {
    rpp::pp_macro* m = cppEnvironment->retrieveStoredMacro( *it );
    if(m && !m->isUndef()) {
      
#ifdef DEBUG_LEXERCACHE
      if(debugging()) {
        kDebug(9007) << "The environment contains a macro that can affect the cached file, but that should not exist:" << m->name.str();
      }
#endif
      return false;
    }
  }
  
  //Make sure that all external macros used by the file now exist too

  ///@todo find out why this assertion sometimes triggers, maybe different macros with the same name were used?
  //ifDebug( Q_ASSERT(m_usedMacros.set().count() == m_usedMacroNames.set().count()) );
  ifDebug( kDebug(9007) << "Count of used macros that need to be verified:" << d_func()->m_usedMacros.set().count() );

  for ( ReferenceCountedMacroSet::Iterator it( d_func()->m_usedMacros.iterator() ); it; ++it ) {
    rpp::pp_macro* m = cppEnvironment->retrieveStoredMacro( it.ref().name );
    if ( !m || !(*m == it.ref()) ) {
      if( !m && it.ref().isUndef() ) {
        ifDebug( kDebug( 9007 ) << "Undef-macro" << it.ref().name.str() << "is ok" << m );
        //It is okay, we did not find a macro, but the used macro is an undef macro
        //Q_ASSERT(0); //Undef-macros should not be marked as used
      } else {
        ifDebug( kDebug( 9007 ) << "The cached file " << url().str() << " used a macro called \"" << it.ref().name.str() << "\"(from" << it.ref().file.str() << "), but the environment" << (m ? "contains differing macro of that name" : "does not contain that macro") << ", the cached file is not used"  );
        ifDebug( if(m) { kDebug() << "Used macro: " << it.ref().toString()  << "from" << it.ref().file.str() << "found:" << m->toString() << "from" << m->file.str(); } );
        return false;
      }
    }else{
      ifDebug( kDebug( 9007 ) << it.ref().name.str() << "match" );
    }
  }

  ifDebug( kDebug( 9007 ) << "Using cached file " << url().str() );  
  return true;
}

const KDevelop::ModificationRevisionSet& Cpp::EnvironmentFile::includePathDependencies() const
{
  ENSURE_READ_LOCKED
  return d_func()->m_includePathDependencies;
}

void Cpp::EnvironmentFile::setIncludePathDependencies(const KDevelop::ModificationRevisionSet& set)
{
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_includePathDependencies = set;
}

bool EnvironmentFile::needsUpdate(const ParsingEnvironment* environment) const {
  ENSURE_READ_LOCKED
  const CppPreprocessEnvironment* cppEnvironment = dynamic_cast<const CppPreprocessEnvironment*>(environment);
  
  //When in naive matching mode, we even use the non-guarded version when inappropriate. We must make sure not to update it in such
  //a situation, else it will end up empty
  if(cppEnvironment && EnvironmentManager::self()->matchingLevel() <= EnvironmentManager::Naive && !headerGuard().isEmpty() && cppEnvironment->macroNameSet().contains(headerGuard()))
    return false;
  
  return ParsingEnvironmentFile::needsUpdate(environment) || d_func()->m_includePathDependencies.needsUpdate();
}

EnvironmentFile::EnvironmentFile( const IndexedString& url, TopDUContext* topContext ) : ParsingEnvironmentFile(*new EnvironmentFileData(), url) {

  d_func_dynamic()->setClassId(this);
  setLanguage(IndexedString("C++"));
  
  d_func_dynamic()->m_topContext = IndexedTopDUContext(topContext);
  
  d_func_dynamic()->m_url = url;

//   ifDebug( kDebug(9007) << "created for" << url.str() << "modification-time:" << d_func_dynamic()->m_modificationTime );
  
  clearModificationRevisions();
}

EnvironmentFile::EnvironmentFile( EnvironmentFileData& data ) : ParsingEnvironmentFile(data)
{
}

EnvironmentFile::~EnvironmentFile() {
}

void EnvironmentFile::setContentStartLine(int line) {
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_contentStartLine = line;
}

int EnvironmentFile::contentStartLine() const {
  ENSURE_READ_LOCKED
  return d_func()->m_contentStartLine;
}

void EnvironmentFile::addDefinedMacro( const rpp::pp_macro& macro, const rpp::pp_macro* previousOfSameName ) {
  ENSURE_WRITE_LOCKED
#ifdef DEBUG_LEXERCACHE
  if(debugging()) {
  kDebug( 9007 )  << id(this) << "defined macro" << macro.name.str();
  }
#endif
  if( previousOfSameName && d_func()->m_definedMacros.contains(*previousOfSameName) )
    d_func_dynamic()->m_definedMacros.remove( *previousOfSameName );
  else if( d_func()->m_definedMacroNames.contains(macro.name) ) {
    //Search if there is already a macro of the same name in the set, and remove it
    //This is slow, but should not happen too often
    ///@todo maybe give a warning, and find out how this can happen
    for( ReferenceCountedMacroSet::Iterator it( d_func()->m_definedMacros.iterator() ); it; ++it )
      if( macro.name == it.ref().name )
        d_func_dynamic()->m_definedMacros.remove(it.ref());
  }
  
  if(macro.isUndef()) {
    d_func_dynamic()->m_definedMacroNames.remove( macro.name );
    
    d_func_dynamic()->m_unDefinedMacroNames.insert( macro.name );
  } else {
    d_func_dynamic()->m_unDefinedMacroNames.remove( macro.name );
    d_func_dynamic()->m_definedMacroNames.insert( macro.name );
    
    d_func_dynamic()->m_definedMacros.insert( macro );
  }
  
  //State: If it is an undef macro, it is not in m_definedMacroNames not in m_definedMacros, and it is in m_unDefinedMacroNames
  //       If  it is a normal macro, it is in m_definedMacroNames, it is in m_definedMacros, and it is not in m_unDefinedMacroNames
}

void EnvironmentFile::usingMacro( const rpp::pp_macro& macro ) {
  ENSURE_WRITE_LOCKED
  if ( !d_func()->m_definedMacroNames.contains( macro.name ) && !d_func()->m_unDefinedMacroNames.contains( macro.name ) && !macro.isUndef() ) {
#ifdef DEBUG_LEXERCACHE
  if(debugging()) {
    kDebug( 9007 ) << id(this) << "used macro" << macro.name.str() << "from" << macro.file.str();
  }
#endif
    d_func_dynamic()->m_usedMacros.insert( macro );
  
    d_func_dynamic()->m_usedMacroNames.insert( macro.name );
  }
}

// const IndexedStringSet& EnvironmentFile::includeFiles() const {
//   return m_includeFiles;
// }

const ReferenceCountedStringSet& EnvironmentFile::strings() const {
  ENSURE_READ_LOCKED
  return d_func()->m_strings;
}


///Set of all defined macros, including those of all deeper included files
const ReferenceCountedMacroSet& EnvironmentFile::definedMacros() const {
  ENSURE_READ_LOCKED
  return d_func()->m_definedMacros;
}

///Set of all macros used from outside, including those used in deeper included files
const ReferenceCountedStringSet& EnvironmentFile::usedMacroNames() const {
  ENSURE_READ_LOCKED
  return d_func()->m_usedMacroNames;
}

///Set of all macros used from outside, including those used in deeper included files
const ReferenceCountedStringSet& EnvironmentFile::definedMacroNames() const {
  ENSURE_READ_LOCKED
  return d_func()->m_definedMacroNames;
}

const ReferenceCountedStringSet& EnvironmentFile::unDefinedMacroNames() const {
  ENSURE_READ_LOCKED
  return d_func()->m_unDefinedMacroNames;
}

///Set of all macros used from outside, including those used in deeper included files
const ReferenceCountedMacroSet& EnvironmentFile::usedMacros() const {
  ENSURE_READ_LOCKED
  return d_func()->m_usedMacros;
}

const QList<IndexedString> EnvironmentFile::includePaths() const {
  ENSURE_READ_LOCKED
  QList<IndexedString> ret;
  if(d_func()->m_includePaths) {
    const IncludePathListItem* item = includePathsRepository().itemFromIndex(d_func()->m_includePaths);
    
    FOREACH_FUNCTION(const IndexedString& include, item->m_includePaths)
      ret << include;
  }
  return ret;
}

void EnvironmentFile::setIncludePaths( const QList<IndexedString>& paths ) {
  ENSURE_WRITE_LOCKED
  
  QMutexLocker lock(includePathsRepository().mutex());
  
  if(d_func()->m_includePaths) {
    KDevelop::DynamicItem<IncludePathListItem, true> item = includePathsRepository().dynamicItemFromIndex(d_func()->m_includePaths);
    --item->m_refCount;
    if(!item->m_refCount)
      includePathsRepository().deleteItem(d_func()->m_includePaths);
    d_func_dynamic()->m_includePaths = 0;
  }
  if(!paths.isEmpty()) {
    IncludePathListItem item;
    foreach(const IndexedString &include, paths)
      item.m_includePathsList().append(include);
    d_func_dynamic()->m_includePaths = includePathsRepository().index(item);
    KDevelop::DynamicItem<IncludePathListItem, true> gotItem = includePathsRepository().dynamicItemFromIndex(d_func()->m_includePaths);
    ++gotItem->m_refCount;
  }
}

KDevelop::IndexedString Cpp::EnvironmentFile::headerGuard() const {
  ENSURE_READ_LOCKED
  return d_func()->m_guard;
}

void Cpp::EnvironmentFile::setHeaderGuard(KDevelop::IndexedString guardName) {
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_guard = guardName;
}

void EnvironmentFile::addMissingIncludeFile(const IndexedString& file)
{
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_missingIncludeFiles.insert(file);
}

const ReferenceCountedStringSet& EnvironmentFile::missingIncludeFiles() const
{
  ENSURE_READ_LOCKED
  return d_func()->m_missingIncludeFiles;
}

void EnvironmentFile::clearMissingIncludeFiles()
{
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_missingIncludeFiles = ReferenceCountedStringSet();
}

void EnvironmentFile::addIncludeFile( const IndexedString& file, const ModificationRevision& modificationTime ) {
//   m_includeFiles.insert(file);
  ENSURE_WRITE_LOCKED

  addModificationRevision(file, modificationTime);
}

void EnvironmentFile::addStrings( const std::set<Utils::BasicSetRepository::Index>& strings ) {
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_strings += ReferenceCountedStringSet( strings );
}

//The parameter should be a EnvironmentFile that was lexed AFTER the content of this file
void EnvironmentFile::merge( const EnvironmentFile& file, CppPreprocessEnvironment* env ) {
  ENSURE_WRITE_LOCKED
  //We have to read the other file
  ENSURE_FILE_READ_LOCKED(file)
  
#ifdef DEBUG_LEXERCACHE
  if(debugging()) {
  kDebug( 9007 ) <<  id(this) << ": merging" << id(&file)  << "defined in macros this:" << print(d_func()->m_definedMacroNames)  << "defined macros in other:" << print(file.d_func()->m_definedMacroNames) << "undefined macros in other:" << print(file.d_func()->m_unDefinedMacroNames) << "strings in other:" << print(file.strings());
  }
#endif
  d_func_dynamic()->m_strings = (d_func()->m_strings + (file.d_func()->m_strings - d_func()->m_definedMacroNames)) - d_func()->m_unDefinedMacroNames;
  
  ///@todo Probably it's more efficient having 2 sets m_changedMacroNames and m_unDefinedMacroNames, where m_unDefinedMacroNames is a subset of m_changedMacroNames.  
  //Only add macros to the usedMacros-set that were not defined locally
  d_func_dynamic()->m_usedMacroNames += (file.d_func()->m_usedMacroNames - d_func()->m_definedMacroNames) - d_func()->m_unDefinedMacroNames;

  ///Merge those used macros that were not defined within this environment
  //This is slightly inefficient, would be nicer to have a fast mechanism for this.
  //This is not tragic since usually only few macros are used, and thus few need to be iterated.
  
  {
    Utils::Set definedMacroNamesSet = d_func()->m_definedMacroNames.set();
    Utils::Set unDefinedMacroNamesSet = d_func()->m_unDefinedMacroNames.set();
    
    std::set<uint> addUsedMacros;
    
    ReferenceCountedMacroSet backup = file.d_func()->m_usedMacros;
    Q_ASSERT(backup.set().setIndex() == file.d_func()->m_usedMacros.set().setIndex());
    
    for(ReferenceCountedMacroSet::Iterator it( file.d_func()->m_usedMacros.iterator() ); it; ++it) {
      const rpp::pp_macro& macro(it.ref());
      if( !definedMacroNamesSet.contains(macro.name.index()) && !unDefinedMacroNamesSet.contains(macro.name.index()) )
        addUsedMacros.insert(it.index());
    }
    
    //Must not happen, since we hold the locks
    Q_ASSERT(backup.set().setIndex() == file.d_func()->m_usedMacros.set().setIndex());
    
    if(!addUsedMacros.empty())
      d_func_dynamic()->m_usedMacros += ReferenceCountedMacroSet( addUsedMacros );
  }
  
  ifDebug( Q_ASSERT(d_func()->m_usedMacroNames.set().count() == d_func()->m_usedMacros.set().count()) );
  
  ///Add defined macros from the merged file.

  {
    Utils::Set otherDefinedMacroNamesSet = file.d_func()->m_definedMacroNames.set();
    Utils::Set otherUnDefinedMacroNamesSet = file.d_func()->m_unDefinedMacroNames.set();
    //Since merged macros overrule already stored ones, first remove the ones of the same name.

    Cpp::ReferenceCountedStringSet affectedMacros = d_func()->m_definedMacroNames & (file.d_func()->m_definedMacroNames + file.d_func()->m_unDefinedMacroNames);
    ReferenceCountedMacroSet potentiallyRemoveMacros = d_func()->m_definedMacros - file.d_func()->m_definedMacros;

    std::set<uint> removeDefinedMacros;
    #if 0
    if(env && affectedMacros.count() < potentiallyRemoveMacros.count()) {
      //In the environment there is a map that maps from macro-names to macros, which allows us iterating through 'affectedMacros' directly
      for( Cpp::ReferenceCountedStringSet::Iterator it( affectedMacros.iterator() ); it; ++it ) {
        rpp::pp_macro* macro = env->retrieveStoredMacro(*it);
        if(macro)
        {
          uint macroIndex = EnvironmentManager::macroDataRepository.findIndex( MacroRepositoryItemRequest(*macro) );
          if(macroIndex && potentiallyRemoveMacros.containsIndex(macroIndex))
            removeDefinedMacros.insert(macroIndex);
        }
      }
    }else
    #endif
      
    if(!affectedMacros.isEmpty()) {
      //We have to iterate through all potentially removed macros
      for( ReferenceCountedMacroSet::Iterator it( potentiallyRemoveMacros.iterator() ); it; ++it ) {
        const rpp::pp_macro& macro(it.ref());
        if( affectedMacros.contains( macro.name ) )
          removeDefinedMacros.insert(it.index());
      }
    }
    
    if(!removeDefinedMacros.empty())
      d_func_dynamic()->m_definedMacros -= ReferenceCountedMacroSet( removeDefinedMacros );
  }

  //Now merge in the new defined macros
  
  d_func_dynamic()->m_unDefinedMacroNames += file.d_func()->m_unDefinedMacroNames;
  d_func_dynamic()->m_unDefinedMacroNames -= file.d_func()->m_definedMacroNames;
  d_func_dynamic()->m_definedMacroNames -= file.d_func()->m_unDefinedMacroNames;
  d_func_dynamic()->m_definedMacroNames += file.d_func()->m_definedMacroNames;
  d_func_dynamic()->m_definedMacros += file.d_func()->m_definedMacros;

  ///Merge include-files, problems and other stuff
//   m_includeFiles += file.m_includeFiles.set();
  d_func_dynamic()->m_missingIncludeFiles += file.d_func()->m_missingIncludeFiles;
  
  addModificationRevisions(file.allModificationRevisions());

#ifdef DEBUG_LEXERCACHE
  if(debugging()) {
  kDebug( 9007 ) << id(this) << ": defined macro names in this after merge:" << d_func()->m_definedMacroNames.set().count() << print(d_func()->m_definedMacroNames);
  kDebug( 9007 ) << id(this) << ": defined in this after merge:" << d_func()->m_definedMacros.set().count() << print(d_func()->m_definedMacros);
  ifDebug( Q_ASSERT(d_func()->m_definedMacros.set().count() == d_func()->m_definedMacroNames.set().count()) );
  kDebug( 9007 ) << id(this) << ": undefined in this after merge:" << print(d_func()->m_unDefinedMacroNames);
  kDebug( 9007 ) << id(this) << ": strings in this after merge:" << print(strings());
  kDebug( 9007 ) << id(this) << ": macros used in this after merge:" << print(d_func()->m_usedMacroNames);
  }
#endif
}

size_t EnvironmentFile::hash() const {
  ///@todo remove the (size_t)(this), it is just temporary to make them unique, but will not work with serialization to disk.
  ///Instead, create a hash over the contained strings, and make sure the other hashes work reliably.
  return (size_t)(this); //m_usedMacros.valueHash() + m_usedMacros.idHash() + m_definedMacros.idHash() + m_definedMacros.valueHash() + (size_t)(this)/*+ m_strings.hash()*/; ///@todo is the string-hash needed here?
}

uint EnvironmentFile::identityOffset() const {
  ENSURE_READ_LOCKED
  return d_func()->m_identityOffset;
}

void EnvironmentFile::setIdentityOffset(uint offset) {
  ENSURE_WRITE_LOCKED
  d_func_dynamic()->m_identityOffset = offset;
}


int EnvironmentFile::type() const {
  ENSURE_READ_LOCKED
  return CppParsingEnvironment;
}