2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
7 #include "FileSourceCode.h"
11 #include "LocatableFile.h"
12 #include "SourceFile.h"
13 #include "SourceLanguage.h"
14 #include "SourceLocation.h"
17 FileSourceCode::FileSourceCode(LocatableFile
* file
, SourceFile
* sourceFile
,
18 SourceLanguage
* language
)
22 fSourceFile(sourceFile
),
25 fFile
->AcquireReference();
26 fSourceFile
->AcquireReference();
27 fLanguage
->AcquireReference();
31 FileSourceCode::~FileSourceCode()
33 fLanguage
->ReleaseReference();
34 fSourceFile
->ReleaseReference();
35 fFile
->ReleaseReference();
40 FileSourceCode::Init()
42 return fLock
.InitCheck();
47 FileSourceCode::AddSourceLocation(const SourceLocation
& location
)
49 // Find the insertion index; don't insert twice.
51 int32 index
= _FindSourceLocationIndex(location
, foundMatch
);
55 return fSourceLocations
.Insert(location
, index
) ? B_OK
: B_NO_MEMORY
;
60 FileSourceCode::Lock()
67 FileSourceCode::Unlock()
74 FileSourceCode::GetSourceLanguage() const
81 FileSourceCode::CountLines() const
83 return fSourceFile
->CountLines();
88 FileSourceCode::LineAt(int32 index
) const
90 return fSourceFile
->LineAt(index
);
95 FileSourceCode::LineLengthAt(int32 index
) const
97 return fSourceFile
->LineLengthAt(index
);
102 FileSourceCode::GetStatementLocationRange(const SourceLocation
& location
,
103 SourceLocation
& _start
, SourceLocation
& _end
) const
105 int32 lineCount
= CountLines();
106 if (location
.Line() >= lineCount
)
110 int32 index
= _FindSourceLocationIndex(location
, foundMatch
);
118 _start
= fSourceLocations
[index
];
119 _end
= index
+ 1 < lineCount
120 ? fSourceLocations
[index
+ 1] : SourceLocation(lineCount
);
126 FileSourceCode::GetSourceFile() const
133 FileSourceCode::_FindSourceLocationIndex(const SourceLocation
& location
,
134 bool& _foundMatch
) const
137 int32 upper
= fSourceLocations
.Size();
138 while (lower
< upper
) {
139 int32 mid
= (lower
+ upper
) / 2;
140 if (location
<= fSourceLocations
[mid
])
146 _foundMatch
= lower
< fSourceLocations
.Size()
147 && location
== fSourceLocations
[lower
];