Commit Graph

180 Commits

Author SHA1 Message Date
Argyrios Kyrtzidis 2403797eec For SourceManager::isBeforeInTranslationUnit(), have it consider macro arg expanded
token locations as coming before the closing ')' of a function macro expansion.

Include a unit test for SourceManager.

llvm-svn: 147056
2011-12-21 16:56:29 +00:00
Dylan Noblesmith 2a8bc15230 SourceManager: use getBufferSize()
Forming an out of bounds pointer to check if it's out
of bounds was undefined behavior.

llvm-svn: 146861
2011-12-19 08:51:05 +00:00
Argyrios Kyrtzidis af41b3f0f2 In SourceManager::~SourceManager do a sanity check to make sure we
don't try to destruct a null ContentCache.

rdar://10567159

llvm-svn: 146707
2011-12-15 23:37:55 +00:00
Argyrios Kyrtzidis cc6107df08 In ContentCache::replaceBuffer, add sanity check to make sure that we do not free a buffer
and then continue using it. rdar://10359140.

llvm-svn: 146308
2011-12-10 01:38:26 +00:00
Argyrios Kyrtzidis 6c8d29f7d1 Add a sanity check in SourceManager::getColumnNumber, make sure
we don't try to access beyond the buffer.

llvm-svn: 146305
2011-12-10 00:30:38 +00:00
Douglas Gregor 9dc3212f98 Implement (de-)serialization of the buffer contents for an overridden
file in the source manager. This allows us to properly create and use
modules described by module map files without umbrella headers (or
with incompletely umbrella headers). More generally, we can actually
build a PCH file that makes use of  file -> buffer remappings, which
could be useful in libclang in the future.

llvm-svn: 144830
2011-11-16 20:05:18 +00:00
Argyrios Kyrtzidis 8edffaa611 Use assert(0) instead of duplicating the check, suggestion by Anna.
llvm-svn: 142886
2011-10-25 00:29:44 +00:00
Argyrios Kyrtzidis 7f6b029c4e Introduce SourceManager::getFileLoc which returns a file location
taking into account macro arguments.

llvm-svn: 141771
2011-10-12 07:07:40 +00:00
Argyrios Kyrtzidis 25029d499e Make sure SourceManager::getFileIDLoaded doesn't hang in release build because of invalid passed parameter.
rdar://10210140

llvm-svn: 141048
2011-10-03 23:43:01 +00:00
Argyrios Kyrtzidis 04a6e5f82e Break SourceManager::translateFileLineCol into translateFile, no functionality change.
llvm-svn: 140610
2011-09-27 17:22:25 +00:00
Argyrios Kyrtzidis 4bdd6aa198 Associate the macro arguments location map with a FileID instead
of a ContentCache, since multiple FileIDs can have the same ContentCache
but the expanded macro arguments locations will be different.

llvm-svn: 140521
2011-09-26 08:01:50 +00:00
David Blaikie 9c902b5502 Rename Diagnostic to DiagnosticsEngine as per issue 5397
llvm-svn: 140478
2011-09-25 23:23:43 +00:00
Argyrios Kyrtzidis 7c2b28a16f In SourceManager::translateLineCol, handle the case where we are pointing
directly at the end of the source file.

llvm-svn: 140192
2011-09-20 22:14:54 +00:00
Argyrios Kyrtzidis 7c06d8666b [libclang] When getting a source location from a file:line:col triplet
check whether the requested location points inside the precompiled preamble,
in which case the returned source location will be a "loaded" one.

llvm-svn: 140060
2011-09-19 20:40:35 +00:00
Argyrios Kyrtzidis 532c5196b0 Break SourceManager::translateFileLineCol into translateLineCol that returns the
source location of line:col of a specific FileID.

llvm-svn: 140059
2011-09-19 20:40:29 +00:00
Argyrios Kyrtzidis e6e67deeed Rename SourceLocation::getFileLocWithOffset -> getLocWithOffset.
It already works (and is useful with) macro locs as well.

llvm-svn: 140057
2011-09-19 20:40:19 +00:00
Argyrios Kyrtzidis b4199b659b For SourceManager::isBeforeInTranslationUnit, a location pointing
inside a macro argument should be regarded as coming before
the location of the expanded tokens.

llvm-svn: 140053
2011-09-19 20:39:57 +00:00
Argyrios Kyrtzidis 18c88a96eb Remove one SourceManager::isInFileID overload and use isOffsetInFileID for the other.
llvm-svn: 138381
2011-08-23 21:02:45 +00:00
Argyrios Kyrtzidis 2797df6a24 Amend r138129 (reduction of SLocEntries) which introduced performance regression due
to increased calls to SourceManager::getFileID. (rdar://9992664)

Use a slightly different approach that is more efficient both in terms of speed
(no extra getFileID calls) and in SLocEntries reduction.

Comparing pre-r138129 and this patch we get:

For compiling SemaExpr.cpp reduction of SLocEntries by 26%.
For the boost enum library:
  -SLocEntries -34% (note that this was -5% for r138129)
  -Memory consumption -50%
  -PCH size -31%

Reduced SLocEntries also benefit the hot function SourceManager::getFileID,
evident by the reduced "FileID scans".

llvm-svn: 138380
2011-08-23 21:02:41 +00:00
Argyrios Kyrtzidis 296374b501 Move a couple of SourceManager functions out-of-line; no functionality change.
llvm-svn: 138375
2011-08-23 21:02:28 +00:00
Argyrios Kyrtzidis 61ef3db222 Boost the efficiency of SourceManager::getMacroArgExpandedLocation.
Currently getMacroArgExpandedLocation is very inefficient and for the case
of a location pointing at the main file it will end up checking almost all of
the SLocEntries. Make it faster:

-Use a map of macro argument chunks to their expanded source location. The map
 is for a single source file, it's stored in the file's ContentCache and lazily
 computed, like the source lines cache.
-In SLocEntry's FileInfo add an 'unsigned NumCreatedFIDs' field that keeps track
 of the number of FileIDs (files and macros) that were created during preprocessing
 of that particular file SLocEntry. This is useful when computing the macro argument
 map in skipping included files while scanning for macro arg FileIDs that lexed from
 a specific source file. Due to padding, the new field does not increase the size
 of SLocEntry.

llvm-svn: 138225
2011-08-21 23:33:04 +00:00
Argyrios Kyrtzidis 34d729d46d Refactor common functionality into SourceManager::getFileIDSize, no functionality change.
llvm-svn: 138127
2011-08-19 22:34:01 +00:00
Argyrios Kyrtzidis 92a47bd997 Introduce SourceManager::getMacroArgExpandedLocation function.
If we pass it a source location that points inside a function macro argument,
the returned location will be the macro location in which the argument was expanded.
If a macro argument is used multiple times, the expanded location will
be at the first expansion of the argument.
 e.g.
   MY_MACRO(foo);
             ^
Passing a file location pointing at 'foo', will yield a macro location
where 'foo' was expanded into.

Make SourceManager::getLocation call getMacroArgExpandedLocation as well.

llvm-svn: 137794
2011-08-17 00:31:20 +00:00
Argyrios Kyrtzidis ac199bffab Make SourceManager::isBeforeInTranslationUnit handle macro locations correctly.
llvm-svn: 137793
2011-08-17 00:31:18 +00:00
Ted Kremenek 43e0c4a887 Change SourceManager::getDataStructureSizes() to use llvm::capacity_in_bytes().
llvm-svn: 136236
2011-07-27 18:41:16 +00:00
Ted Kremenek 120992ad81 clang_getCXTUResourceUsage: Report memory used by data structures in SourceManager.
llvm-svn: 136189
2011-07-26 23:46:06 +00:00
Chandler Carruth 64ee782e2f Clean up as many of the comments in Basic I can find to talk in terms of
'expansion' rather than 'instantiation' for macro source locations.

llvm-svn: 136058
2011-07-26 05:17:23 +00:00
Chandler Carruth ee4c1d1298 Migrate 'Instantiation' data and API bits of SLocEntry to 'Expansion'
etc. With this I think essentially all of the SourceManager APIs are
converted. Comments and random other bits of cleanup should be all thats
left.

llvm-svn: 136057
2011-07-26 04:56:51 +00:00
Chandler Carruth 73ee5d7fae Convert InstantiationInfo and much of the related code to ExpansionInfo
and various other 'expansion' based terms. I've tried to reformat where
appropriate and catch as many references in comments but I'm going to do
several more passes. Also I've tried to expand parameter names to be
more clear where appropriate.

llvm-svn: 136056
2011-07-26 04:41:47 +00:00
Chandler Carruth 115b077f30 Rename create(MacroArg)InstantiationLoc to create(MacroArg)ExpansionLoc.
llvm-svn: 136054
2011-07-26 03:03:05 +00:00
Chandler Carruth aa63153ff7 Rename SourceManager (and InstantiationInfo) isMacroArgInstantiation API
to isMacroArgExpansion.

llvm-svn: 136053
2011-07-26 03:03:00 +00:00
Chandler Carruth d48db2115a Rename getInstantiationLineNumber to getExpansionLineNumber in both
SourceManager and FullSourceLoc.

llvm-svn: 135969
2011-07-25 21:09:52 +00:00
Chandler Carruth 42f35f9cd2 Rename getInstantiationColumnNumber to getExpansionColumnNumber in both
SourceManager and FullSourceLoc.

llvm-svn: 135965
2011-07-25 20:57:57 +00:00
Chandler Carruth c7ca5218b6 Rename getDecomposedInstantiationLoc to getDecomposedExpansionLoc.
llvm-svn: 135962
2011-07-25 20:52:32 +00:00
Chandler Carruth c84d769c68 getInstantiationLocSlowCase -> getExpansionLocSlowCase
llvm-svn: 135961
2011-07-25 20:52:26 +00:00
Chandler Carruth ca757587a3 Rename SourceManager::getImmediateInstantiationRange to
getImmediateExpansionRange.

llvm-svn: 135960
2011-07-25 20:52:21 +00:00
Chandler Carruth 6d28d7f2a3 Rename SourceManager::getInstantiationRange to getExpansionRange.
llvm-svn: 135915
2011-07-25 16:56:02 +00:00
Chris Lattner 0e62c1cc0b remove unneeded llvm:: namespace qualifiers on some core types now that LLVM.h imports
them into the clang namespace.

llvm-svn: 135852
2011-07-23 10:55:15 +00:00
Douglas Gregor 925296b4c2 Revamp the SourceManager to separate the representation of parsed
source locations from source locations loaded from an AST/PCH file.

Previously, loading an AST/PCH file involved carefully pre-allocating
space at the beginning of the source manager for the source locations
and FileIDs that correspond to the prefix, and then appending the
source locations/FileIDs used for parsing the remaining translation
unit. This design forced us into loading PCH files early, as a prefix,
whic has become a rather significant limitation.

This patch splits the SourceManager space into two parts: for source
location "addresses", the lower values (growing upward) are used to
describe parsed code, while upper values (growing downward) are used
for source locations loaded from AST/PCH files. Similarly, positive
FileIDs are used to describe parsed code while negative FileIDs are
used to file/macro locations loaded from AST/PCH files. As a result,
we can load PCH/AST files even during parsing, making various
improvemnts in the future possible, e.g., teaching #include <foo.h> to
look for and load <foo.h.gch> if it happens to be already available.

This patch was originally written by Sebastian Redl, then brought
forward to the modern age by Jonathan Turner, and finally
polished/finished by me to be committed.

llvm-svn: 135484
2011-07-19 16:10:42 +00:00
Chandler Carruth 402bb38823 Keep track of which source locations are part of a macro argument
instantiation and improve diagnostics which are stem from macro
arguments to trace the argument itself back through the layers of macro
expansion.

This requires some tricky handling of the source locations, as the
argument appears to be expanded in the opposite direction from the
surrounding macro. This patch provides helper routines that encapsulate
the logic and explain the reasoning behind how we step through macros
during diagnostic printing.

This fixes the rest of the test cases originially in PR9279, and later
split out into PR10214 and PR10215.

There is still some more work we can do here to improve the macro
backtrace, but those will follow as separate patches.

llvm-svn: 134660
2011-07-07 23:56:36 +00:00
Argyrios Kyrtzidis 61c58f7f43 Move SourceManager::isAt[Start/End]OfMacroInstantiation functions to the Lexer, since they depend on it now.
llvm-svn: 134644
2011-07-07 21:54:45 +00:00
Argyrios Kyrtzidis 41fb2d95a3 Make the Preprocessor more memory efficient and improve macro instantiation diagnostics.
When a macro instantiation occurs, reserve a SLocEntry chunk with length the
full length of the macro definition source. Set the spelling location of this chunk
to point to the start of the macro definition and any tokens that are lexed directly
from the macro definition will get a location from this chunk with the appropriate offset.

For any tokens that come from argument expansion, '##' paste operator, etc. have their
instantiation location point at the appropriate place in the instantiated macro definition
(the argument identifier and the '##' token respectively).
This improves macro instantiation diagnostics:

Before:

t.c:5:9: error: invalid operands to binary expression ('struct S' and 'int')
int y = M(/);
        ^~~~
t.c:5:11: note: instantiated from:
int y = M(/);
          ^

After:

t.c:5:9: error: invalid operands to binary expression ('struct S' and 'int')
int y = M(/);
        ^~~~
t.c:3:20: note: instantiated from:
\#define M(op) (foo op 3);
                ~~~ ^  ~
t.c:5:11: note: instantiated from:
int y = M(/);
          ^

The memory savings for a candidate boost library that abuses the preprocessor are:

- 32% less SLocEntries (37M -> 25M)
- 30% reduction in PCH file size (900M -> 635M)
- 50% reduction in memory usage for the SLocEntry table (1.6G -> 800M)

llvm-svn: 134587
2011-07-07 03:40:34 +00:00
Argyrios Kyrtzidis c8f7e213b9 Fix bug in SourceManager::getDecomposedInstantiationLocSlowCase.
It would add up relative (decomposed) offsets like in getDecomposedSpellingLocSlowCase, but while
it makes sense to preserve the offset among lexed spelling locations, it doesn't make
sense to add anything to the offset of the instantiation location. The instantiation
location will be the same regardless of the relative offset in the tokens that were
instantiated.

This bug didn't actually affect anything because, currently, in practice we never create macro
locations with relative offset greater than 0.

llvm-svn: 134586
2011-07-07 03:40:27 +00:00
Argyrios Kyrtzidis 2cc62093fc For -print-stats, add the number of bytes that SLocEntryTable consumes.
llvm-svn: 134585
2011-07-07 03:40:24 +00:00
Benjamin Kramer 37d30835f0 SmallVectorize a critical vector.
The small number of elements was determined by taking the median
file length in clang+llvm and /usr/include on OS X with xcode installed.

llvm-svn: 134496
2011-07-06 16:43:46 +00:00
Argyrios Kyrtzidis f096a6e754 SourceManager::isAtStartOfMacroInstantiation should check not only if the location
is at the first token but that the location's offset is not inside the token as well.

llvm-svn: 133800
2011-06-24 17:28:26 +00:00
Jay Foad 9a6b09874d Make more use of llvm::StringRef in various APIs. In particular, don't
use the deprecated forms of llvm::StringMap::GetOrCreateValue().

llvm-svn: 133515
2011-06-21 15:13:30 +00:00
Argyrios Kyrtzidis 814b2a6d6b Add a couple of helper methods in the SourceManager API, isAtStartOfMacroInstantiation/isAtEndOfMacroInstantiation
useful only for source locations that point at a macro token.

llvm-svn: 132247
2011-05-28 03:56:11 +00:00
Argyrios Kyrtzidis f15eac1110 Do some safety checks.
llvm-svn: 131491
2011-05-17 22:09:53 +00:00
Ted Kremenek 8d58790019 Enhance clang_getCXTUResourceUsage() to report how much memory is used by SourceManager's memory buffers.
llvm-svn: 130433
2011-04-28 20:36:42 +00:00