summaryrefslogtreecommitdiffstats
path: root/languages/cpp/kdevdriver.cpp
blob: 35d0a3eebc92468bb882042ba2d068178d7935f8 (plain)
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
#include "klocale.h" /* defines [function] i18n */
#include <lexer.h>

#include "kdevdriver.h"
#include "cppcodecompletionconfig.h"
#include "setuphelper.h"
#include <unistd.h>
#include "includepathresolver.h"


KDevDriver::KDevDriver( CppSupportPart* cppSupport, bool foreground )
: m_cppSupport( cppSupport ), m_includePathResolver(0), m_foreground(foreground), m_shouldParseIncludedFiles(true)
{
	//setupProject();
	setup();
	
}

KDevDriver::~KDevDriver() {
	delete m_includePathResolver;
}

CppSupportPart* KDevDriver::cppSupport() { return m_cppSupport; }

void KDevDriver::setupProject()
{
	TQMap<TQString, bool> map;
	
	TQStringList fileList = m_cppSupport->project() ->allFiles();
	TQStringList::ConstIterator it = fileList.begin();
	while ( it != fileList.end() )
	{
		TQFileInfo info( *it );
		++it;
		
		map.insert( info.dirPath( true ), true );
	}
	TQMap<TQString, bool>::Iterator mapit = map.begin();
	while ( mapit != map.end() )
	{
		addIncludePath( mapit.key() );
		++mapit;
	}
}

void KDevDriver::setupLexer( Lexer* lexer )
{
	Driver::setupLexer( lexer );
	lexer->setReportMessages( true );
	lexer->setReportWarnings( true );
}

//! setup the preprocessor
//! code provided by Reginald Stadlbauer <[email protected]>
void KDevDriver::setup()
{
	if( lexerCache() ) lexerCache()->clear(); ///Clear the lexer-cache so missing headers get a chance to be parsed
	clearMacros();
	clearIncludePaths();

	addMacro( Macro("KDEVELOP_PARSER", "3.4") );
	
	bool ok;
	TQString verboseGccOutput = SetupHelper::getVerboseGccIncludePath( &ok );
	TQStringList verboseGccLines = TQStringList::split( '\n', verboseGccOutput );
	if( verboseGccLines.count() > 3 ) {
		///Parse the output of gcc. It includes gcc's final include-path when parsing an empty c++-file(including dirs like /usr/include/c++/4.xx/...)
		//what about order?
		for( TQStringList::iterator it = verboseGccLines.begin(); it != verboseGccLines.end(); ++it ) {
			if( (*it).startsWith(" ") && (*it).length() > 2 && (*it)[1] != ' ' ) {
				//it is a potential include-file
				TQString path = (*it).stripWhiteSpace();
				TQFileInfo info( path );
				if( info.exists() ) {
					kdDebug( 9007 ) << "Adding include-path from gcc-output: \"" << path << "\" absolute: \"" << info.absFilePath() << "\"" <<  endl;
					addIncludePath(info.absFilePath());
				}
			}
		}
	} else {
		///Do some of the old stuff
		addIncludePath( "/include" );
		addIncludePath( "/usr/include" );
		addIncludePath( "/usr/local/include" );
        
		bool ok;
        TQString includePath = SetupHelper::getGccIncludePath(&ok);
		if (ok) {
			TQStringList ls = TQStringList::split( "\n", includePath );
			for( TQStringList::const_iterator it = ls.begin(); it != ls.end(); ++it ) {
			if( !(*it).isEmpty() )
				addIncludePath( *it );
			}
		}
    
        addIncludePath( includePath );
		addIncludePath( "/usr/include/g++-3" );
		addIncludePath( "/usr/include/g++" );
	}
	
	addMacro( Macro( "__cplusplus", "1" ) );

	///@todo maybe remove the following? Is there any normal user who has his environment set up correctly so this is of any use?
	TQString tdedir = getenv( "TDEDIR" );
	if( !tdedir.isNull() )
		addIncludePath( tdedir + "/include" );

	TQString qmakespec = getenv( "QMAKESPEC" );
	if ( qmakespec.isNull() )
	qmakespec = "linux-g++";
	
	TQString qtdir = getenv( "QTDIR" );
	if( !qtdir.isNull() ) {
		addIncludePath( qtdir + "/include" );

		// #### implement other mkspecs and find a better way to find the
		// #### proper mkspec (althoigh this will be no fun :-)

		addIncludePath( qtdir + "/mkspecs/" + qmakespec );
	}

	TQStringList lines = SetupHelper::getGccMacros(&ok);
	if (!ok) {
		for (TQStringList::ConstIterator it = lines.constBegin(); it != lines.constEnd(); ++it) {
			TQStringList lst = TQStringList::split( ' ', *it );
			if ( lst.count() != 3 )
				continue;
			addMacro( Macro( lst[1], lst[2] ) );
		}
	}
		
	addMacro( Macro( "__cplusplus", "1" ) );
	addMacro( Macro( "signals", "signals" ) );
	addMacro( Macro( "slots", "slots" ) );
	addMacro( Macro( "TQ_SCRIPTABLE", "" ) );

	CppCodeCompletionConfig* cfg = m_cppSupport->codeCompletionConfig();
	TQString str = cfg->customIncludePaths();
	int pos = 0;
	while( pos < str.length() ) {
		int end = str.find( ';', pos );
		if( end == -1 ) {
			end = str.length();
		}
		
		TQString s = str.mid( pos, end-pos ).stripWhiteSpace();
		if( !s.isEmpty() ) {
			if( !s.startsWith( "/" ) && m_cppSupport->project() ) {
				s = m_cppSupport->project()->projectDirectory() + "/" + s;
			}
			addIncludePath( s );
		}
		
		pos = end+1;
	}

	setResolveDependencesEnabled( cfg->preProcessAllHeaders() | cfg->parseMissingHeaders() );

	delete m_includePathResolver;
	if( cfg->resolveIncludePaths() ) {
		m_includePathResolver = new CppTools::IncludePathResolver( m_foreground );
		if( m_cppSupport && m_cppSupport->project() )
			m_includePathResolver->setOutOfSourceBuildSystem( m_cppSupport->project()->projectDirectory(), m_cppSupport->project()->buildDirectory() );
	} else
		m_includePathResolver = 0;
	
	m_shouldParseIncludedFiles = cfg->parseMissingHeaders();
}

TQStringList KDevDriver::getCustomIncludePath( const TQString& file ) {
	if( !file.startsWith("/") )
		kdDebug( 9007 ) << "KDevDriver::getCustomIncludePath(..): given file \"" << file << "\" is not absolute" << endl;
	if( !m_includePathResolver )
		return includePaths();
	CppTools::PathResolutionResult res = m_includePathResolver->resolveIncludePath( file );
	
	if( !res.success ) {
		Problem p( i18n( "%1. Message: %2" ).tqarg( res.errorMessage ).tqarg( res.longErrorMessage ), 0, 0, Problem::Level_Warning );
		p.setFileName( file );
		addProblem( file, p );
	}

	return res.path + includePaths();
}

bool KDevDriver::shouldParseIncludedFile( const ParsedFilePointer& file ) {
	TQString compoundString = file->fileName() + "||" + TQString("%1").tqarg(file->usedMacros().valueHash()) + "||" + TQString("%1").tqarg(file->usedMacros().idHash());

	if( !m_shouldParseIncludedFiles )
		return false;
	m_cppSupport->safeFileSet().contains( compoundString );

	if( m_cppSupport->safeFileSet().contains( file->fileName()) ){
		return false;
	} else if( m_cppSupport->safeFileSet().contains( compoundString ) ) {
		//kdDebug( 9007 ) << "ALREADY IN FILE-SET: " << compoundString << endl;
		return false;
	} else {
		m_cppSupport->safeFileSet().insert( compoundString ); //This is needed so the same instance of a file is not queued many times
		//kdDebug( 9007 ) << "NOT IN FILE-SET, PARSING: " << compoundString << endl;
		return true;
	}

}

//kate: indent-mode csands; tab-width 4; space-indent off;